Search Unity

Question How to find the way out of a collider?

Discussion in 'Physics for ECS' started by HeyZoos, Aug 24, 2021.

  1. HeyZoos

    HeyZoos

    Joined:
    Jul 31, 2016
    Posts:
    50
    I want to find my way out of a collider, but only on the sides - not up or down. This solution *almost* works. It fails when the collider bounds are long and flat, resulting in the closest point being a spot above or below the obstacle, instead of out to the side.

    Code (CSharp):
    1.  
    2. // If we hit an obstacle...
    3. if (collisionWorld.CastCollider(colliderCastInput, out
    4.         var closestHitValidityCheck)) {
    5.     // Get the obstacle bounding volume.
    6.     var aabb = collisionWorld.Bodies[closestHitValidityCheck.RigidBodyIndex].CalculateAabb();
    7.     var midHeightFormationPosition = sampledFormationPosition;
    8.     midHeightFormationPosition.y = aabb.Extents.y / 2;
    9.     var closestPoint = aabb.ClosestPoint(midHeightFormationPosition);
    10.     // If the desired formation spot is inside the obstacle, we want the direction OUT.
    11.     // If the formation spot is close to but not inside, we want the direction AWAY.
    12.     var direction = aabb.Contains(midHeightFormationPosition) ?
    13.         midHeightFormationPosition - closestPoint :
    14.         closestPoint - midHeightFormationPosition;
    15.     // Move the target position.
    16.     sampledFormationPosition = midHeightFormationPosition + math.normalize(direction) * 3 f;
    17. }
    18.