ColliderSurface.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using Vector3 = UnityEngine.Vector3;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. public class ColliderSurface : MonoBehaviour, ISurface, IBounds
  7. {
  8. [SerializeField]
  9. private Collider _collider;
  10. protected virtual void Start()
  11. {
  12. Assert.IsNotNull(_collider);
  13. }
  14. public Transform Transform => transform;
  15. public Bounds Bounds => _collider.bounds;
  16. public bool Raycast(in Ray ray, out SurfaceHit hit, float maxDistance)
  17. {
  18. hit = new SurfaceHit();
  19. RaycastHit hitInfo;
  20. if (_collider.Raycast(ray, out hitInfo, maxDistance))
  21. {
  22. hit.Point = hitInfo.point;
  23. hit.Normal = hitInfo.normal;
  24. hit.Distance = hitInfo.distance;
  25. return true;
  26. }
  27. return false;
  28. }
  29. public bool ClosestSurfacePoint(in Vector3 point, out SurfaceHit hit, float maxDistance = 0)
  30. {
  31. Vector3 closest = _collider.ClosestPoint(point);
  32. RKLog.Info($"ColliderSurface ClosestSurfacePoint: {closest},{point}");
  33. return Raycast(new Ray(point, closest - point), out hit, maxDistance);
  34. }
  35. }
  36. }