ClosestPointToColliders.cs 808 B

1234567891011121314151617181920212223242526272829
  1. using UnityEngine;
  2. namespace Rokid.UXR.Interaction {
  3. public static partial class Collisions
  4. {
  5. public static Vector3 ClosestPointToColliders(Vector3 point, Collider[] colliders)
  6. {
  7. Vector3 closestPoint = point;
  8. float closestDistance = float.MaxValue;
  9. foreach (Collider collider in colliders)
  10. {
  11. if (Collisions.IsPointWithinCollider(point, collider))
  12. {
  13. return point;
  14. }
  15. Vector3 closest = collider.ClosestPoint(point);
  16. float distance = (closest - point).magnitude;
  17. if (distance < closestDistance)
  18. {
  19. closestDistance = distance;
  20. closestPoint = closest;
  21. }
  22. }
  23. return closestPoint;
  24. }
  25. }
  26. }