IsCapsuleWithinColliderApprox.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. namespace Rokid.UXR.Interaction
  2. {
  3. using UnityEngine;
  4. public static partial class Collisions
  5. {
  6. /// <summary>
  7. /// Approximate capsule collision by doing sphere collisions down the capsule length
  8. /// </summary>
  9. /// <param name="p0">Capsule Start</param>
  10. /// <param name="p1">Capsule End</param>
  11. /// <param name="radius">Capsule Radius</param>
  12. /// <param name="collider">Collider to check against</param>
  13. /// <returns>Whether or not an approximate collision occured.</returns>
  14. public static bool IsCapsuleWithinColliderApprox(Vector3 p0, Vector3 p1, float radius, Collider collider)
  15. {
  16. int divisions = Mathf.CeilToInt((p1 - p0).magnitude / radius) * 2;
  17. if (divisions == 0)
  18. {
  19. return IsSphereWithinCollider(p0, radius, collider);
  20. }
  21. float tStep = 1f / divisions;
  22. for (int i = 0; i <= divisions; i++)
  23. {
  24. Vector3 point = Vector3.Lerp(p0, p1, tStep * i);
  25. if (IsSphereWithinCollider(point, radius, collider))
  26. {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. }
  33. }