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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Physics.OverlapSphere doubt!

Discussion in 'Scripting' started by busterlock, Mar 8, 2018.

  1. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    I'm having a hard time understanding and coding the Physics.overlapSphere command. My intention was to make my player emit a circular raycast around himself and detect if there are any colliders around him, like a sonar detects objects it hits. So I wrote the following code:

    Code (CSharp):
    1.     public Collider[] hitCollider;
    2.     public float radius;
    3.  
    4.     void DetectSonar(Vector3 center, float radius)
    5.     {
    6.         Collider[] hitCollider = Physics.OverlapSphere(center, radius);
    7.         Debug.Log("It hit something");
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         if(Input.GetKeyDown(KeyCode.Y))
    13.         {
    14.             DetectSonar(transform.position, radius);
    15.         }
    16.     }
    17. }
    The Debug.Log counter increases regularly if I press the Y key, so that makes me think that the code is actually working. The big question is: am I being fooled by my inability to program and the code is actually not working?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    So first off, your code calls OverlapSphere but never does anything with that information.

    OverlapSphere is going to return an array of all colliders that were hit. Then your next line of code should do something with that array. You just call 'Debug.Log'. You should have something like:

    Code (csharp):
    1.  
    2. Collider[] hitColliders = Physics.OverlapSphere(center, radius);
    3. if(hitColliders != null && hitColliders.Length > 0)
    4. {
    5.     //hit occurred, do something with that information
    6.     Debug.Log("It hit something.");
    7. }
    8.  
    Note, if you don't need to know what colliders were hit, you can also do 'CheckSphere':
    https://docs.unity3d.com/ScriptReference/Physics.CheckSphere.html

    Which returns true if something was found in the sphere. This can be more efficient if all you need to do is determine IF something was in the sphere, rather than WHAT was in the sphere.

    Do note though... if you have a collider on your player. This might pick up those colliders too. You can avoid this by checking the 'hitColliders' for your own collider and ruling it out. Or passing in the layers to test for, and don't include the player's layer.
     
  3. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    Thaaaaaaanks!!!!