use bounds center if ClosestPoint() function does not work

This commit is contained in:
in0finite 2020-06-28 20:38:23 +02:00
parent 47c44355ba
commit 7da990e105
3 changed files with 13 additions and 2 deletions

View file

@ -79,7 +79,7 @@ namespace SanAndreasUnity.Utilities
foreach (var collider in pair.Value)
{
Vector3 closestPointOnCollider = collider.ClosestPoint(center);
Vector3 closestPointOnCollider = collider.ClosestPointOrBoundsCenter(center);
float distanceToPointOnCollider = Vector3.Distance(center, closestPointOnCollider);
float distanceToColliderTransform = Vector3.Distance(center, collider.transform.position);

View file

@ -50,7 +50,7 @@ namespace SanAndreasUnity.Utilities
foreach (var collider in colliders)
{
Vector3 closestPointOnCollider = collider.ClosestPoint(this.transform.position);
Vector3 closestPointOnCollider = collider.ClosestPointOrBoundsCenter(this.transform.position);
Vector3 diff = closestPointOnCollider - this.transform.position;
float distance = diff.magnitude;

View file

@ -66,6 +66,17 @@ namespace SanAndreasUnity.Utilities
}
}
public static Vector3 ClosestPointOrBoundsCenter(this Collider collider, Vector3 position)
{
if (collider is MeshCollider && !((MeshCollider)collider).convex)
{
// ClosestPoint() function is not supported on non-convex mesh colliders
return collider.bounds.center;
}
return collider.ClosestPoint(position);
}
public static bool BetweenInclusive(this float v, float min, float max)
{
return v >= min && v <= max;