Search Unity

GameObject in area detection - Best Practice

Discussion in 'Scripting' started by NamelessGames_, May 1, 2020.

  1. NamelessGames_

    NamelessGames_

    Joined:
    Jun 4, 2019
    Posts:
    43
    Hi guys,
    I'ld like make a gameobject detect if an other gameobject is in its visual range and if yes, attack it.

    I thought to make Larger Collider to detect if another GO is in area and a Smaller Collider to detect if it's hit.
    The problem is Collision detection, I can't identify which Collider is hit, I should do a "no trigger" Collider but then it would block my projectiles.

    Is there an alternative method?
    Thank you!
     
  2. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    Try something like this:

    make an empty gameobject with a collider (isTrigger enabled) and put this script on it.

    Code (CSharp):
    1. public bool isInBox;
    2.  
    3. void Update(){
    4.     if(isInBox){
    5.         Debug.Log("Found in box!");
    6.     } else {
    7.         Debug.Log("Not in box!");
    8.     }
    9. }
    10.  
    11. void OnTriggerStay(Collider other){
    12.     if(other.CompareTag("Player"){
    13.         isInBox = true;
    14.     }
    15. }
    16. void OnTriggerExit(Collider other){
    17.     if(other.CompareTag("Player"){
    18.         isInBox = false;
    19.     }
    20. }
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I assume this is about a player being seen by enemies, implying that there is only one object 'being detected', ie the player. If so, you could just do the following for each enemy in some distance around the player. Cast a ray to check if the line between the enemy and the player is blocked by something. If not, check if that direction is covered by the field of view of the enemy. An enemy which looks in the direction of the player and is not blocked by an object, sees the player and would thus attempt to attack it.
    To get the enemies in some distance around the player you could either use a huge collider and keep track of the list of enemies that enteres but did not exist yet. Or you could keep a list of all enemies (which you should anyways somewhere) and sort it over their distance to the player once at the beginning of each frame, then do the above for each enemy until you reached one which is too far away. Or you could use a sphere cast to get the list of enemies some distance around the player and then do the above. The best option depends on the type of game you are making, how many enemies there are and so on.
     
  4. NamelessGames_

    NamelessGames_

    Joined:
    Jun 4, 2019
    Posts:
    43
    The real problem is that having 2 colliders, I can't say which collider is triggering :/
    Spherecast is performant on Update?
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Honestly, my first approach would be the sorted list, or a spatial partitioning in case of many enemies. Spherecast is not exactly performant, but one hardly matters, unless you have tons of colliders it hits. I'd just test it out and look at the profiler for a noticable difference, then test it incrementally with more and more objects to see what the limit of each approach would be. Only if you think you will run into any performance problems due to the amount of enemies tho.