Search Unity

Bug The first call to OverlapSphereNonAlloc finds non-overlapping colliders

Discussion in 'Physics' started by the80srobot, Jun 29, 2020.

  1. the80srobot

    the80srobot

    Joined:
    Apr 5, 2020
    Posts:
    47
    Hello,

    I'm hitting a strange bug - I have a scene with about 1,000 spheres in it, and I'm using Physics.OverlapSphereNonAlloc to detect nearby spheres. This works as expected, except for the very first call after launch, which fills the provided array with non-overlapping colliders.

    This is in Unity 2020.1.0b12.3931.

    Here's some elided code to show what happens:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         Collider[] nearby = new Collider[4];
    4.         int n = Physics.OverlapSphereNonAlloc(Vector3.zero, 1f, nearby);
    5.         for (int i = 0; i < n; i++)
    6.         {
    7.             Debug.Log(
    8.                 "Nearby collider " + (i + 1) + "/" + n + ": " + nearby[i].transform.name +
    9.                   " at " + nearby[i].transform.position + " r=" + nearby[i].GetComponent<SphereCollider>().radius);
    10.         }
    11.     }
    On the first call to update, this is the output:

    Code (csharp):
    1.  
    2. Nearby collider 1/4: Sphere1 at (0.0, 0.0, 0.0) r=2.56
    3. UnityEngine.Debug:Log(Object)
    4. Galaxy:Update() (at Assets/FEO/Common/Scripts/Simulation/POC.cs:27)
    5.  
    6. Nearby collider 2/4: Sphere2 at (255.8, 5.8, 263.0) r=2.56
    7. UnityEngine.Debug:Log(Object)
    8. Galaxy:Update() (at Assets/FEO/Common/Scripts/Simulation/POC.cs:27)
    9.  
    10. Nearby collider 3/4: Sphere3 at (257.0, 6.4, 264.4) r=2.56
    11. UnityEngine.Debug:Log(Object)
    12. Galaxy:Update() (at Assets/FEO/Common/Scripts/Simulation/POC.cs:27)
    13.  
    14. Nearby collider 4/4: Sphere4 at (83.9, 6.7, 81.3) r=2.56
    15. UnityEngine.Debug:Log(Object)
    16. Galaxy:Update() (at Assets/FEO/Common/Scripts/Simulation/POC.cs:27)
    17.  
    On every subsequent frame, only 1 collider is detected.
     
  2. the80srobot

    the80srobot

    Joined:
    Apr 5, 2020
    Posts:
    47
    Well, anyway, I just have an extra check that validates each overlap by checking the distance.

    This seems buggy as hell - I'm guessing the first call to the function returns a value of `n` that's too high, but because this part of the engine is closed-source I can't really debug it without delving deeper into C# than I am willing to do.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    Are you sure you're not simply instantiating these spheres then changing the Transform on them after rather than passing in the Transform pose to the instantiate method? Transform changes are not processed by physics until the simulation steps. Until then, they're just transform changes.

    Try dumping the Rigidbody.position and not the Transform as the two are not the same.

    Note I'm not a 3D physics dev so the above is just a guess.
     
  4. the80srobot

    the80srobot

    Joined:
    Apr 5, 2020
    Posts:
    47
    The spheres are spawned in Start, and their position is set in Instantiate, but the very next line (still in Start) adjusts the position. I guess that explains it then, thanks. This was really bugging me.
     
    MelvMay likes this.