FollowerBase.cs 4.3 KB

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