Saturday, March 8, 2014

Use the KGFMapSystem for Fog of War

As promised yesterday, here a little explanation of how to use the Fog of War from the KGFMapSystem (which is on sale currently) that is only displayed on the MiniMap, in your game.
This is extremly usefull for TopDown-View games, like PocketCrusades.

The first thing you need to do is set up the KGFMapSystem. If you do not know how to do so, take a look here: KGF-Support.

Now, when you have everything set up, be sure to check the Fog Of War option in the settings.
Thats how it looks for me:

You can change the resolution depending on the size of your world.

Now, create a new Script called FowCreator.cs! Thats where we will instantiate layers that are visible for the "Game"- Camera. These will get the same texture assigned as the FogOfWar plane for the minimap.

Here's the script:

Thanks to Brainswitch Machina and Alexandre Ouellet for pointing out some improvements!

using UnityEngine;
using System.Collections;

public class FowCreator : MonoBehaviour {
 
 private GameObject[] layers;
 public float[] heights;
 private bool isCreated = false;
 // Use this for initialization
 void DoCreate () {
  isCreated = true;
  int layers_to_use = heights.Length;
  layers = new GameObject[layers_to_use];
  GameObject pre = GameObject.Find("fog_of_war");
  Vector3 apos = pre.transform.position;
  //apos.y = heights[0];
  //pre.transform.position = apos;
  //pre.layer = 0;
  for(int i = 0; i < layers_to_use; i++)
  {
   layers[i] = (GameObject)GameObject.Instantiate(pre);
   apos = layers[i].transform.position;
   apos.y = heights[i];
   layers[i].transform.position = apos;
   layers[i].layer = 11;
  }
 }
 void Awake() {
  StartCoroutine(findLayer());
 }
 IEnumerator findLayer()
 {
  GameObject ag = GameObject.Find("fog_of_war");
  while(ag == null)
  {
   //Dont call find with string too often, this will slow the rest down
   yield return new WaitForSeconds(0.1f);
   ag = GameObject.Find("fog_of_war");
  }
  DoCreate();
 }
}
  


Now, in the editor assign some testing values. Depending on your "world" you'll need different ones.


Choose how many layers you want. The higher, the better it looks, however, at great computing costs. Since I develop for mobile devices, I just use one. The element values themselves are the height of the layers. The highest should be higher than anything visible for the camera.

That's it. Tomorrow I'll make a brief demo on the Warriors, be sure to check it out.

Thanks for reading.

4 comments:

  1. Just a tip, since you are developing for mobile: Avoid unnecessary Update (& FixedUpdate, OnGUI etc) methods. Even empty ones will be called by the Unity engine.
    Why not move the Update method code to an Awake or Start method instead?

    ReplyDelete
    Replies
    1. Thanks for the tip, however, since in this case the fog_of_war plane of the KGFMapSystem is created some time during startup, and not necessarily right at start, I have to do it this way. Maybe I'll find a way to call it from somewhere else at a later point, to remove this update.

      Anyway, thanks for the input!

      Delete
  2. The other commenter is right though. At awake, start a coroutine that yield return null while it can't find the gameobject. Once found, call DoCreate. That should limit how often update is called, and should have the same result

    ReplyDelete