Hiding enemies that are outside Line of Sight

Another problem popped up in my isometric RPG game. I want to hide enemies if they are outside of my party members’ field of view (behind walls etc.).

I know how to do the basic field of view and check if one of my characters can see another one, but not how to hide the enemy if no one can see him.

I guess one solution would be to continuosly check line of sight from each party member to each enemy in the scene, then add each aprty member to a "Seen By…) list the enemy keeps, and if that list if 0, just disable the renderer. Is there another method?

Your idea is probably the simplest way to achieve this. What you’re trying to achieve here is called Occlusion Culling, and there are several readymade solutions in the Asset store. Unity Pro has an in-built occlusion culling system.

If you want to do it yourself, I would start with all enemies renderers disabled, and only turn them on if a ray hits them.

A few things to think about:

  • Have a sphere trigger around each player to determine which enemies are close enough to even be seen, then do a raycast against each of these.
  • Don’t do raycasts in Update. Have a coroutine that casts rays, say, every 0.1 sec. In turn based games the interval could be longer.
  • If turning renderers on a ray-hit, remember to have them turned on for at least as long as your raycast interval.
  • Spread out the raycasts over a few frames.