FollowerBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = 0.5f;
  8. public float WindowDistance {
  9. get { return windowDistance; }
  10. set { windowDistance = Mathf.Abs(value); }
  11. }
  12. [SerializeField, Range(0.0f, 100.0f), Tooltip("How quickly to interpolate the window towards its target position and rotation.")]
  13. private float windowFollowSpeed = 1.2f;
  14. public float WindowFollowSpeed {
  15. get { return windowFollowSpeed; }
  16. set { windowFollowSpeed = Mathf.Abs(value); }
  17. }
  18. [Header("Window Settings")]
  19. [SerializeField, Tooltip("What part of the view port to anchor the window to.")]
  20. private TextAnchor windowAnchor = TextAnchor.LowerCenter;
  21. public TextAnchor WindowAnchor {
  22. get { return windowAnchor; }
  23. set { windowAnchor = value; }
  24. }
  25. [SerializeField, Tooltip("The offset from the view port center applied based on the window anchor selection.")]
  26. private Vector2 windowOffset = new Vector2(0.1f, 0.1f);
  27. public Vector2 WindowOffset {
  28. get { return windowOffset; }
  29. set { windowOffset = value; }
  30. }
  31. [SerializeField]
  32. private Vector2 defaultWindowRotation = new Vector2(10.0f, 20.0f);
  33. public Vector2 DefaultWindowRotation {
  34. get { return defaultWindowRotation; }
  35. set { defaultWindowRotation = value; }
  36. }
  37. private Quaternion windowHorizontalRotation;
  38. private Quaternion windowHorizontalRotationInverse;
  39. private Quaternion windowVerticalRotation;
  40. private Quaternion windowVerticalRotationInverse;
  41. protected virtual void OnEnable() {
  42. windowHorizontalRotation = Quaternion.AngleAxis(defaultWindowRotation.y, Vector3.right);
  43. windowHorizontalRotationInverse = Quaternion.Inverse(windowHorizontalRotation);
  44. windowVerticalRotation = Quaternion.AngleAxis(defaultWindowRotation.x, Vector3.up);
  45. windowVerticalRotationInverse = Quaternion.Inverse(windowVerticalRotation);
  46. }
  47. protected virtual Vector3 CalculateWindowPosition(Transform cameraTransform) {
  48. Vector3 position = cameraTransform.position + (cameraTransform.forward * WindowDistance);
  49. Vector3 horizontalOffset = cameraTransform.right * windowOffset.x;
  50. Vector3 verticalOffset = cameraTransform.up * windowOffset.y;
  51. switch (windowAnchor) {
  52. case TextAnchor.UpperLeft: position += verticalOffset - horizontalOffset; break;
  53. case TextAnchor.UpperCenter: position += verticalOffset; break;
  54. case TextAnchor.UpperRight: position += verticalOffset + horizontalOffset; break;
  55. case TextAnchor.MiddleLeft: position -= horizontalOffset; break;
  56. case TextAnchor.MiddleRight: position += horizontalOffset; break;
  57. case TextAnchor.MiddleCenter: position += horizontalOffset + verticalOffset; break;
  58. case TextAnchor.LowerLeft: position -= verticalOffset + horizontalOffset; break;
  59. case TextAnchor.LowerCenter: position -= verticalOffset; break;
  60. case TextAnchor.LowerRight: position -= verticalOffset - horizontalOffset; break;
  61. }
  62. return position;
  63. }
  64. protected virtual Quaternion CalculateWindowRotation(Transform cameraTransform) {
  65. Quaternion rotation = cameraTransform.rotation;
  66. switch (windowAnchor) {
  67. case TextAnchor.UpperLeft: rotation *= windowHorizontalRotationInverse * windowVerticalRotationInverse; break;
  68. case TextAnchor.UpperCenter: rotation *= windowHorizontalRotationInverse; break;
  69. case TextAnchor.UpperRight: rotation *= windowHorizontalRotationInverse * windowVerticalRotation; break;
  70. case TextAnchor.MiddleLeft: rotation *= windowVerticalRotationInverse; break;
  71. case TextAnchor.MiddleRight: rotation *= windowVerticalRotation; break;
  72. case TextAnchor.LowerLeft: rotation *= windowHorizontalRotation * windowVerticalRotationInverse; break;
  73. case TextAnchor.LowerCenter: rotation *= windowHorizontalRotation; break;
  74. case TextAnchor.LowerRight: rotation *= windowHorizontalRotation * windowVerticalRotation; break;
  75. }
  76. return rotation;
  77. }
  78. protected virtual void LateUpdate() {
  79. if (StopFollower == false) {
  80. Follow();
  81. }
  82. }
  83. protected abstract void Follow();
  84. }