Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question OverlapSphere in specific area, not whole world

Discussion in 'Editor & General Support' started by LemonPi314, Mar 29, 2023.

  1. LemonPi314

    LemonPi314

    Joined:
    Jan 17, 2022
    Posts:
    5
    Heyo, so i've been trying to create a system that rewards the player once a room has been cleared of all enemies.
    So far i've got a physics OverlapSphere that gets every collider inside its area. And a line of code that gets all GameObjects with the "Enemy" tag
    How can I get it so only colliders in the OverlapSphere, with the GameObject tag of "Enemy" are counted?

    Here's le code

    Code (CSharp):
    1. private void GetEnemiesInRoom()
    2.     {
    3.         //overlapRadius currently set to 25f
    4.         enemies = Physics.OverlapSphere(transform.position, overlapRadius).ToList();
    5.  
    6.         //enemies is the list of every collider in the OverlapSphere
    7.         foreach(Collider enemy in enemies)
    8.         {
    9.             //find GameObjects with the "Enemy" tag
    10.             //I thought this would iterate through the enemies list, but i guess not?
    11.             enemiesInRoom = GameObject.FindGameObjectsWithTag("Enemy").Length;
    12.         }
    13.     }
    Thanks ^_^
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    You're almost there... you just need to do the counting yourself.

    - BEFORE the for loop, set
    enemiesInRoom
    to zero

    Code (csharp):
    1. enemiesInRoom = 0;
    - now inside your for loop you have to ask if each enemy has that tag, something like:

    Code (csharp):
    1. if (enemy.CompareTag( "Enemy"))
    2. {
    3.   enemiesInRoom++;
    4. }
    And after the loop your
    enemiesInRoom
    will be correct!

    Final observation:

    You have all colliders in the variable you called
    enemies
    ... great! But I would rename that to
    overlappingColliders
    (for instance). This is just for sanity, because obviously they might NOT be enemies, and that could be misleading to you in the future. I am personally really good at confusing myself so I try to minimize it. :)

    By that same logic I would rename the
    enemy
    variable in your foreach() loop to be perhaps
    enemyCandidate
    because well, he might not even BE an enemy after all!
     
    LemonPi314 likes this.
  3. LemonPi314

    LemonPi314

    Joined:
    Jan 17, 2022
    Posts:
    5
    This works perfectly, thankyou so much! :D
    And good point about the variable names, I forgot to rename them after trying previous things but they've been renamed now ^_^
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    I call it "code gardening," as in just going through and straightening up little things like that... snip off the dead leaves, make sure everything is well-watered, etc.

    While you're at it, looking at your method,
    GetEnemiesInRoom()
    , usually when the word "get" is used, you are saying "give me back" something, eg, return the count that you compute.

    Since your method is void and only mutates the
    enemiesInRoom
    instance variable, you may wish to call it something like:

    void UpdateEnemiesInRoom()


    or

    void CountEnemiesInRoom()