SafetyAreaFollower.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SafetyAreaFollower : FollowerBase
  5. {
  6. bool isInit = false;
  7. protected override void OnEnable()
  8. {
  9. base.OnEnable();
  10. isInit = false;
  11. }
  12. protected override void Follow()
  13. {
  14. //if (API_GSXR_Slam.SlamManager?.head == null)
  15. //{
  16. // return;
  17. //}
  18. if (isInit == false)
  19. {
  20. transform.position = CalculateWindowPosition(Camera.main.transform);
  21. transform.rotation = CalculateWindowRotation(Camera.main.transform);
  22. isInit = true;
  23. }
  24. transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  25. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  26. }
  27. protected override Vector3 CalculateWindowPosition(Transform cameraTransform)
  28. {
  29. Vector3 position = cameraTransform.position + (new Vector3(cameraTransform.forward.x, 0, cameraTransform.forward.z) * WindowDistance);
  30. Vector3 horizontalOffset = cameraTransform.right * windowOffset.x;
  31. Vector3 verticalOffset = cameraTransform.up * windowOffset.y;
  32. switch (windowAnchor)
  33. {
  34. case TextAnchor.UpperLeft: position += verticalOffset - horizontalOffset; break;
  35. case TextAnchor.UpperCenter: position += verticalOffset; break;
  36. case TextAnchor.UpperRight: position += verticalOffset + horizontalOffset; break;
  37. case TextAnchor.MiddleLeft: position -= horizontalOffset; break;
  38. case TextAnchor.MiddleRight: position += horizontalOffset; break;
  39. case TextAnchor.MiddleCenter: position += horizontalOffset + verticalOffset; break;
  40. case TextAnchor.LowerLeft: position -= verticalOffset + horizontalOffset; break;
  41. case TextAnchor.LowerCenter: position -= verticalOffset; break;
  42. case TextAnchor.LowerRight: position -= verticalOffset - horizontalOffset; break;
  43. }
  44. return position;
  45. }
  46. protected virtual Quaternion CalculateWindowRotation(Transform cameraTransform)
  47. {
  48. Quaternion rotation = Quaternion.Euler(0, cameraTransform.rotation.eulerAngles.y, 0);
  49. switch (windowAnchor)
  50. {
  51. case TextAnchor.UpperLeft: rotation *= windowHorizontalRotationInverse * windowVerticalRotationInverse; break;
  52. case TextAnchor.UpperCenter: rotation *= windowHorizontalRotationInverse; break;
  53. case TextAnchor.UpperRight: rotation *= windowHorizontalRotationInverse * windowVerticalRotation; break;
  54. case TextAnchor.MiddleLeft: rotation *= windowVerticalRotationInverse; break;
  55. case TextAnchor.MiddleRight: rotation *= windowVerticalRotation; break;
  56. case TextAnchor.LowerLeft: rotation *= windowHorizontalRotation * windowVerticalRotationInverse; break;
  57. case TextAnchor.LowerCenter: rotation *= windowHorizontalRotation; break;
  58. case TextAnchor.LowerRight: rotation *= windowHorizontalRotation * windowVerticalRotation; break;
  59. }
  60. return rotation;
  61. }
  62. }