Search Unity

ComputePenetration for SphereCollider inside MeshCollider

Discussion in 'Physics' started by Imapler7, May 4, 2020.

  1. Imapler7

    Imapler7

    Joined:
    Jun 16, 2013
    Posts:
    3
    I am using the code below to move a camera out of objects and because i have the position of the camera separated into an ideal position where the player wants it and an actual position where the camera is, sometimes it is entirely inside objects.

    The camera has a small sphere collider and the environment has a mix of other colliders. Against SphereCollider/BoxCollider/... it works as i intend and against MeshCollider it works correctly on the border but returns zero penetration if the cameras sphere collider is entirely inside the mesh. This seem reasonable for concave mesh colliders but i expected it to work normally for convex ones. Is this the intended behavior or am i doing something wrong?

    Code (CSharp):
    1. int hitCount = Physics.OverlapSphereNonAlloc(currentPosition, myCol.radius, colBuffer, def.collisionData.layerMask);
    2.  
    3. for(int i = 0; i < hitCount; i++)
    4. {
    5.     Collider col = colBuffer[i];
    6.  
    7.     Physics.ComputePenetration(
    8.         myCol, currentPosition, Quaternion.identity,
    9.         col, col.bounds.center, Quaternion.identity,
    10.         out Vector3 dir, out float penetration
    11.     );
    12.  
    13.     currentPosition += dir * penetration;
    14. }
    15.