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

Physics.OverlapSphere looks like it is picking up incorrect game objects

Discussion in 'Scripting' started by Phaedridge, Feb 1, 2021.

  1. Phaedridge

    Phaedridge

    Joined:
    Nov 9, 2018
    Posts:
    4
    Code (CSharp):
    1.         bool retry = true;
    2.         int retryCount = 0;
    3.         while(retry)
    4.         {
    5.             retryCount++;
    6.             retry = false;
    7.             newPosition = new Vector3(((-(float)width / 2) + 0.5f) * passageSize, 0.5f,
    8.                     ((-(float)width / 2) + 0.5f + Random.Range(0, width - 1)) * passageSize);
    9.             Collider[] objects = Physics.OverlapSphere(newPosition, 1.0f);
    10.  
    11.             if (objects.Length != 0)
    12.             {
    13.                 foreach (var item in objects)
    14.                 {
    15.                     if (!item.gameObject.CompareTag("Boundary"))
    16.                     {
    17.                         retry = true;
    18.                     }
    19.                 }
    20.             }
    21.             if (retryCount > 9)
    22.             {
    23.                 retry = false;
    24.             }
    25.         }
    26.         playerGO.transform.position = newPosition;
    27.  
    The above code is supposed to check that the spawnPoint doesn't have anything already located there - possibilities should be Chest or Enemy. However it sometimes doesn't pick up that there is a Chest at the location and picks up spurious boundary objects which when I debug don't make sense... the location of the object picked up is too far away from the sphere and the name is incorrect (it picks up walls, that are located there but the names of the game objects are wrong).
    I tested by trying to delete all overlapped game objects and only the floor gets removed, none of the listed walls. It also seems to miss the Enemies....
     
  2. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    Have you tried using Gizmos to help see where your Physics.OverlapSphere might actually be?
     
    Bunny83 likes this.
  3. Phaedridge

    Phaedridge

    Joined:
    Nov 9, 2018
    Posts:
    4
    Yep, exactly where I expect it.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    Looks fine to me. I shorten that retry code to:
    while(retry && retryCount++<9)
    . Using ++ like that is abusive, but it's one of those "oh, we're doing that thing" lines.

    An advantage is you can check
    retry
    for true after the loop, to know whether it failed. Or, just change the last part to
    if(retryCount>9) break;
    .
     
    Bunny83 likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,530
    So what else have you done to debug your issue? You know that you can add a context object reference to a Debug.Log statement. When you click on the log message in the console, the Unity editor will "ping" / highlight that object in the hierarchy / project panel. This usually helps to figure out which objects are involved. So just add a Debug.Log and add a log for every object that is overlapped with your overlapsphere call.

    Note your "objects.Length != 0" check is unnecessary. A foreach loop won't execute at all if you hand it an empty collection. Yes it would choke on a null reference but so would your length check.

    Finally keep in mind that if you move collider arround they have to have a kinetic rigidbody attached. Also note if you move an object with a collider depending on your physics transform sync settings, the collider may not be where you think. If you changed the position of an object this frame, you may need to call Physics.SyncTransforms manually to ensure the physics system is in sync with your objects.