Issue with the "fog of war" aspect of my game

Hi,im currently working on a rts strategy game and im having an issue with the “fog of war” aspect of the game.
I added a compoent with a bool variable which determines if the current troop is visible to the player or not.

i wanted to make so all enemy troops within a determined units radius from any player troop where marked as visible = true and all the others where marked as visible = false

i tried geting all enemy divisions on a list and determining if they where within radius of the current troop with a foreach loop and vector3.distance then geting the component of the units in range and setting then to visible an doing the oposite with the rest.

but when i did that a bug which the enemy divisions where non stop changing betwen the 2 states because they where beeing set to visible by nearby troops but also as invisible by far aways troops and now im stuck.

i also thoutgh that maybe i could stop seting the visible = false from the player troop script and instead making a the enemy division script change its status to invisible every x seconds but i dont know if that is feasible.

I feel like you’ve drastically over complicated this, is there any kind of special reason you’re using lists for a fog of war shader? Is this because of you wanting to set up some kind of NPC behaviour? Either way I did manage to find a mini unity tutorial on the topic.

It seems you’ve got two things going on here, the fog of war shader itself and for some reason you’re trying to combine the script behaviour. As someone who has been dealing with this problem myself regarding shaders I promise you, you are better off keeping the script behaviour unrelated to the fog of war entirely separate. I would recommend making values match your colliders and so on then that way the player will barely notice the difference and it will save you a lot of headaches, I’ve done thing with my own shader problems regarding creating ring effects.

Also I’m kind of glad I found this tutorial because I love the fog of war effect myself. I would recommend making use of this kind of effect then using a simple OnTriggerEnter that matches the range of the effect you’re doing. Don’t try doing anything too complicated trying to merge the fog of war maths with collision and mesh visibility.

Edit: I just re-read your post to make sure I wasn’t misunderstanding too much, you should probably post your code up so we can see what you’re up to.

I saw the tutorial and it was actually useful in the visual manner but it does not solve my problems because they are a bit more complex

This is my code and it is on every player troop gameobject see if you can understand what i was aiming towards

 public void ReconVision()
    {
        List<GameObject> AllUnits = UnitManagerComponent.ReconManagerComponent.AllUnits;

        foreach(GameObject Unit in AllUnits)
        {
            if (Unit.GetComponent<UnitStats>().UnitFaction != StatsComponent.UnitFaction)
            {
                float Distance = Vector2.Distance(transform.position, Unit.transform.position);


                if (Distance <= StatsComponent.Recon && !Physics2D.Linecast(transform.position, Unit.transform.position, UnitManagerComponent.ObstacleMask))
                {
                    if (!UnitManagerComponent.ReconManagerComponent.VisibleUnits.Contains(Unit))
                    {
                        UnitManagerComponent.ReconManagerComponent.VisibleUnits.Add(Unit);
                    }
                }

            }
        }       
    }

the bug was after the Distance and line cast if statement when i had a else leading to this piece of code:

else
                {
                    if (UnitManagerComponent.ReconManagerComponent.VisibleUnits.Contains(Unit))
                    {
                        UnitManagerComponent.ReconManagerComponent.VisibleUnits.Remove(Unit);
                    }
                }

As @Lethn says, do not use lists.

That approach requires inhumanly perfect bookkeeping and when you have a bug, it may prove EXTREMELY hard to figure out.

Instead, each time you update visibility (once per frame or less often if you prefer), do this:

  • iterate all enemies:
    — for each enemy:
    ------ assume they are invisible
    ------ iterate each friendly
    ------ as soon as an enemy is within view of a given friendly:
    ---------- mark the enemy visible
    ---------- move onto considering the next enemy.
1 Like

Yes, I’m with Kurt on this one, I think you’ve done the classic thing of overcomplicating your code for what you’re trying to do. Also in terms of ranged based detection there are far more efficient and simple methods like colliders and raycasts which don’t really require a vector3.distance check unless you’re trying to return a vector3.distance for a specific reason I wouldn’t do that. I had almost gone down this rabbit hole myself with another mathematical problem that somebody helped me implement in a better way which is what these forums are for.

To simplify my rambling a bit, you’re doing a mathematical check on something which unity already does for you automatically and it’s bad practice to ignore that. I could understand potentially why you’d want to do distance checks between NPCs etc. within a radius if you’re doing formations for example but even with that there are more straightforward built in ways to do that. Hope this helps you code more in the right direction for what you’re trying to do. Game engines are built to make life easier for programmers but programmers seem to have a weird habit of wanting to do more complicated things than necessary for what should be simple tasks.

A simple foreach loop should work fine, it will also mean you’re not constantly updating lists and making unnecessary calculations in your code.

Could you write an example for me?

You mean you want me to make a new project (or buy one), make two RTS unit armies with multiple units, make a fog of war handling system, get all of that working on my own time, then move on and write you the simple single-scan code that I described above with seven lines of pseudocode??!!

No, sorry.

Jump in, take a shot at it, post here when you have issues.

calm down i didn’t meant asking for you to build a entire game i just wanted you to write the code u mentioned above in C# because didn’t understood it well the way you wrote it did you meant like using 2 foreach loops inside each other?

You don’t ask programmers and potentially professional programmers at that to write code for you, you can ask about examples or pseudo code, but programming is your job. It’s amazing how people don’t understand this and how it’s considered very cheeky. You wouldn’t ask a plumber or an electrician to a job for you without payment especially and then steal the credit for the work as if you did it yourself, it’s the same principle here.

If you don’t understand the explanation, ask him to elaborate further, or it might be that you need to start studying foreach loops specifically so you understand what he’s on about. One thing I’ve noticed it seems that people seem to think they can just pressure somebody into doing work for them rather than admit they don’t know something, really don’t understand the mentality behind that.

an example was exactly what i asked for but now i just got it working so thanks for the help guys

1 Like