hide everything outside spotlight (top-down unity2d)

Hi Guys,

I’m trying to build a top-down game where I have this flashlight, and well I need all the room to be visible but for example the enemies should be hidden but they can emit sound so you are like where they are? and you have to look with the flashlight, the thing is that I don’t know how to make them invisible until the light hits them :frowning:

any idea?

Best,
Daniel S.

Make a simple polygon trigger and set the enemies to enable their renderer when the enemies enter the trigger and disable it when they leave the trigger. For example (on the enemy script)

SpriteRenderer enemySprite;

void Start() {
enemySprite= GetComponent<SpriteRenderer>();
}


void OnTriggerEnter2D() {
     enemySprite.enabled = true;
}

void OnTriggerExit2D() {
     enemySprite.enabled = false;
}

If you have multiple triggers around the world you should probably have a check to see if they entered the correct trigger but yeah, you get the idea.

Thanks a lot bro I figured this out time ago xD, now I’m fighting agains how to make a field view, for the enemies.

I made field of view using a Polygon collider shaped as a cone and simply alerted the enemy if the player entered the trigger and started a cool down timer if the player left the collider.

So after 15 seconds the enemies stop chasing the player anymore and switch to a search mode where they simply are alerted and search around for the player.

And I switched between three different cones depending on the AI state of the enemy, a smaller one for idle/unaware, a medium one for searching and a huge one for alerted/chasing.

I forgot a step of my line of sight. When the player enters the collider I send a raycast every 0.5s to see if the enemy can see the player. Otherwise they will be able to detect the player through walls.