Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

hide everything outside spotlight (top-down unity2d)

Discussion in '2D' started by siba2893, Dec 10, 2014.

  1. siba2893

    siba2893

    Joined:
    Apr 18, 2013
    Posts:
    10
    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 :(

    any idea?

    Best,
    Daniel S.
     
    Last edited: Dec 10, 2014
  2. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    322
    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)

    Code (csharp):
    1.  
    2. SpriteRenderer enemySprite;
    3.  
    4. void Start() {
    5. enemySprite= GetComponent<SpriteRenderer>();
    6. }
    7.  
    8.  
    9. void OnTriggerEnter2D() {
    10.      enemySprite.enabled = true;
    11. }
    12.  
    13. void OnTriggerExit2D() {
    14.      enemySprite.enabled = false;
    15. }
    16.  
    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.
     
  3. siba2893

    siba2893

    Joined:
    Apr 18, 2013
    Posts:
    10
    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.
     
  4. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    322
    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.
     
  5. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    322
    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.