FollowerBase.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public abstract class FollowerBase : MonoBehaviour {
  5. public bool StopFollower = false;
  6. [SerializeField, Range(0.1f, 100.0f)]
  7. private float windowDistance = 1.5f;
  8. public float WindowDistance
  9. {
  10. get { return windowDistance; }
  11. set { windowDistance = Mathf.Abs(value); }
  12. }
  13. [SerializeField, Range(0.0f, 100.0f), Tooltip("How quickly to interpolate the window towards its target position and rotation.")]
  14. private float windowFollowSpeed = 2f;
  15. public float WindowFollowSpeed
  16. {
  17. get { return windowFollowSpeed; }
  18. set { windowFollowSpeed = Mathf.Abs(value); }
  19. }
  20. [Header("Window Settings")]
  21. [SerializeField, Tooltip("What part of the view port to anchor the window to.")]
  22. protected TextAnchor windowAnchor = TextAnchor.LowerCenter;
  23. public TextAnchor WindowAnchor
  24. {
  25. get { return windowAnchor; }
  26. set { windowAnchor = value; }
  27. }
  28. [SerializeField, Tooltip("The offset from the view port center applied based on the window anchor selection.")]
  29. protected Vector2 windowOffset = new Vector2(0f, 0f);
  30. public Vector2 WindowOffset
  31. {
  32. get { return windowOffset; }
  33. set { windowOffset = value; }
  34. }
  35. [SerializeField]
  36. private Vector2 defaultWindowRotation = new Vector2(0f, 0f);
  37. public Vector2 DefaultWindowRotation
  38. {
  39. get { return defaultWindowRotation; }
  40. set { defaultWindowRotation = value; }
  41. }
  42. protected Quaternion windowHorizontalRotation;
  43. protected Quaternion windowHorizontalRotationInverse;
  44. protected Quaternion windowVerticalRotation;
  45. protected Quaternion windowVerticalRotationInverse;
  46. protected virtual void OnEnable()
  47. {
  48. windowHorizontalRotation = Quaternion.AngleAxis(defaultWindowRotation.y, Vector3.right);
  49. windowHorizontalRotationInverse = Quaternion.Inverse(windowHorizontalRotation);
  50. windowVerticalRotation = Quaternion.AngleAxis(defaultWindowRotation.x, Vector3.up);
  51. windowVerticalRotationInverse = Quaternion.Inverse(windowVerticalRotation);
  52. }
  53. protected virtual Vector3 CalculateWindowPosition(Transform cameraTransform)
  54. {
  55. Vector3 position = cameraTransform.position + (cameraTransform.forward * WindowDistance);
  56. Vector3 horizontalOffset = cameraTransform.right * windowOffset.x;
  57. Vector3 verticalOffset = cameraTransform.up * windowOffset.y;
  58. switch (windowAnchor)
  59. {
  60. case TextAnchor.UpperLeft: position += verticalOffset - horizontalOffset; break;
  61. case TextAnchor.UpperCenter: position += verticalOffset; break;
  62. case TextAnchor.UpperRight: position += verticalOffset + horizontalOffset; break;
  63. case TextAnchor.MiddleLeft: position -= horizontalOffset; break;
  64. case TextAnchor.MiddleRight: position += horizontalOffset; break;
  65. case TextAnchor.MiddleCenter: position += horizontalOffset + verticalOffset; break;
  66. case TextAnchor.LowerLeft: position -= verticalOffset + horizontalOffset; break;
  67. case TextAnchor.LowerCenter: position -= verticalOffset; break;
  68. case TextAnchor.LowerRight: position -= verticalOffset - horizontalOffset; break;
  69. }
  70. return position;
  71. }
  72. protected virtual Quaternion CalculateWindowRotation(Transform cameraTransform)
  73. {
  74. Quaternion rotation = cameraTransform.rotation;
  75. switch (windowAnchor)
  76. {
  77. case TextAnchor.UpperLeft: rotation *= windowHorizontalRotationInverse * windowVerticalRotationInverse; break;
  78. case TextAnchor.UpperCenter: rotation *= windowHorizontalRotationInverse; break;
  79. case TextAnchor.UpperRight: rotation *= windowHorizontalRotationInverse * windowVerticalRotation; break;
  80. case TextAnchor.MiddleLeft: rotation *= windowVerticalRotationInverse; break;
  81. case TextAnchor.MiddleRight: rotation *= windowVerticalRotation; break;
  82. case TextAnchor.LowerLeft: rotation *= windowHorizontalRotation * windowVerticalRotationInverse; break;
  83. case TextAnchor.LowerCenter: rotation *= windowHorizontalRotation; break;
  84. case TextAnchor.LowerRight: rotation *= windowHorizontalRotation * windowVerticalRotation; break;
  85. }
  86. return rotation;
  87. }
  88. protected virtual float CalculateWindowOffsetX()
  89. {
  90. float _offsetX = 0;
  91. switch (windowAnchor)
  92. {
  93. case TextAnchor.UpperLeft: _offsetX += -windowOffset.x; break;
  94. case TextAnchor.UpperCenter: _offsetX += 0; break;
  95. case TextAnchor.UpperRight: _offsetX += windowOffset.x; break;
  96. case TextAnchor.MiddleLeft: _offsetX += -windowOffset.x; break;
  97. case TextAnchor.MiddleRight: _offsetX += windowOffset.x; break;
  98. case TextAnchor.MiddleCenter: _offsetX += 0; break;
  99. case TextAnchor.LowerLeft: _offsetX += -windowOffset.x; break;
  100. case TextAnchor.LowerCenter: _offsetX += 0; break;
  101. case TextAnchor.LowerRight: _offsetX += windowOffset.x; break;
  102. }
  103. return _offsetX / 2;
  104. }
  105. protected virtual float CalculateWindowOffsetY()
  106. {
  107. float _offsetY = 0;
  108. switch (windowAnchor)
  109. {
  110. case TextAnchor.UpperLeft: _offsetY += windowOffset.y; break;
  111. case TextAnchor.UpperCenter: _offsetY += windowOffset.y; break;
  112. case TextAnchor.UpperRight: _offsetY += windowOffset.y; break;
  113. case TextAnchor.MiddleLeft: _offsetY += 0; break;
  114. case TextAnchor.MiddleRight: _offsetY += 0; break;
  115. case TextAnchor.MiddleCenter: _offsetY += 0; break;
  116. case TextAnchor.LowerLeft: _offsetY += -windowOffset.y; break;
  117. case TextAnchor.LowerCenter: _offsetY += -windowOffset.y; break;
  118. case TextAnchor.LowerRight: _offsetY += -windowOffset.y; break;
  119. }
  120. return _offsetY;
  121. }
  122. protected virtual void LateUpdate()
  123. {
  124. if (StopFollower == false)
  125. {
  126. Follow();
  127. }
  128. }
  129. protected abstract void Follow();
  130. public virtual void StopFollow()
  131. {
  132. StopFollower = false;
  133. }
  134. }