Search Unity

OverlapSphereNonAlloc returns all colliders null some frames and others not with SteamVR hand hover

Discussion in 'AR/VR (XR) Discussion' started by NewMagic-Studio, Feb 21, 2020.

  1. NewMagic-Studio

    NewMagic-Studio

    Joined:
    Feb 25, 2015
    Posts:
    454
    I realized using Steamvr hover material appeared intermittently when hand approached the object with the interactable component and i realized that it was cause OverlapSphereNonAlloc some frames returns all colliders values as null but other frames gives the right result, any ideas?

    This is the code

    Code (CSharp):
    1.  
    2.         protected virtual bool CheckHoveringForTransform(Vector3 hoverPosition, float hoverRadius, ref float closestDistance, ref Interactable closestInteractable, Color debugColor)
    3.         {
    4.             bool foundCloser = false;
    5.  
    6.             // null out old vals
    7.             for (int i = 0; i < overlappingColliders.Length; ++i)
    8.             {
    9.                 overlappingColliders[i] = null;
    10.             }
    11.  
    12.             int numColliding = Physics.OverlapSphereNonAlloc(hoverPosition, hoverRadius, overlappingColliders, hoverLayerMask.value);
    13.  
    14.             // DebugVar
    15.             int iActualColliderCount = 0;
    16.  
    17.             // Pick the closest hovering
    18.             for (int colliderIndex = 0; colliderIndex < overlappingColliders.Length; colliderIndex++)
    19.             {
    20.                 Collider collider = overlappingColliders[colliderIndex];
    21.  
    22.                 if (collider == null)
    23.                 {
    24.                     continue;
    25.                 }
    26.  
    27.                 // Yeah, it's null, skip
    28.                 if (collider.GetComponent<Interactable>() == null)
    29.                 {
    30.                     continue;
    31.                 }
    32.                 Interactable contacting = collider.GetComponent<Interactable>();
    33.  
    34.                 // Best candidate so far...
    35.                 float distance = Vector3.Distance(contacting.transform.position, hoverPosition);
    36.                 bool isCloser = (distance < closestDistance);
    37.                 bool lowerPriority = false;
    38.                 if (closestInteractable != null)
    39.                 { // compare to closest interactable to check priority
    40.                     lowerPriority = contacting.hoverPriority < closestInteractable.hoverPriority;
    41.                     //if (contacting.name == closestInteractable.name) isCloser = true;
    42.                 }
    43.                 if (isCloser && !lowerPriority)
    44.                 {
    45.                     closestDistance = distance;
    46.                     closestInteractable = contacting;
    47.                     foundCloser = true;
    48.                 }
    49.                 iActualColliderCount++;
    50.             }
    51. }