mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-14 16:27:19 +00:00
use bounds center if ClosestPoint() function does not work
This commit is contained in:
parent
47c44355ba
commit
7da990e105
3 changed files with 13 additions and 2 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue