PlatformAccountFollower.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlatformAccountFollower : FollowerBase
  5. {
  6. [Header("Following Settings")]
  7. public bool InstantFollowing = false;//是否无延迟跟随
  8. public bool LinearFollowing;//是否线性移动
  9. public Vector2 border_x; //x轴边界
  10. public Vector2 border_y; //y轴边界
  11. private Transform _slamHead;
  12. Vector2 initViewPoint;//相机画面的中点,坐标为(0.5f,0.5f)
  13. Vector3 viewPoint = Vector3.zero;// 面板中心坐标相对于相机画面的坐标。
  14. private Vector3 velocity = Vector3.zero;
  15. bool isFollower = false;
  16. Vector3 originPos = Vector3.zero;
  17. Vector3 desPos = Vector3.zero;
  18. float journeyLength = 0f;
  19. protected override void OnEnable()
  20. {
  21. base.OnEnable();
  22. }
  23. private void Start()
  24. {
  25. InitTrans();
  26. }
  27. protected override void LateUpdate()
  28. {
  29. if (StopFollower == false)
  30. {
  31. if (IsFollower())
  32. {
  33. if (LinearFollowing)
  34. {
  35. LinearFollow();
  36. }
  37. else
  38. {
  39. Follow();
  40. }
  41. }
  42. }
  43. }
  44. void InitTrans()
  45. {
  46. if (Camera.main != null)
  47. {
  48. _slamHead = Camera.main.transform;
  49. transform.position = CalculateWindowPosition(Camera.main.transform);
  50. transform.rotation = CalculateWindowRotation(Camera.main.transform);
  51. initViewPoint = Camera.main.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));//相机画面中点坐标
  52. }
  53. }
  54. protected bool IsFollower()
  55. {
  56. if ( Camera.main == null)
  57. {
  58. return false;
  59. }
  60. viewPoint = Camera.main.WorldToViewportPoint(transform.position);// 面板在相机画面上的坐标
  61. if (viewPoint.x > initViewPoint.x + border_x.x + CalculateWindowOffsetX() || viewPoint.x < initViewPoint.x + border_x.y + CalculateWindowOffsetX() || viewPoint.y < initViewPoint.y + border_y.y + CalculateWindowOffsetY() || viewPoint.y > initViewPoint.y + border_y.x + CalculateWindowOffsetY())//边界判断
  62. {
  63. isFollower = true;
  64. }
  65. else if (Mathf.Abs(viewPoint.x - initViewPoint.x - CalculateWindowOffsetX()) < 0.03f && Mathf.Abs(viewPoint.y - initViewPoint.y - CalculateWindowOffsetY()) < 0.06f)
  66. {
  67. isFollower = false;
  68. }
  69. return isFollower;
  70. }
  71. //Nonlinear Following by default
  72. protected override void Follow()
  73. {
  74. transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  75. //transform.position = Vector3.SmoothDamp(transform.position, CalculateWindowPosition(Camera.main.transform), ref velocity, WindowFollowSpeed);
  76. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  77. }
  78. //For Linear Following, turn down the WindowFollowSpeed to around 0.5 at distance = 1 for best experience
  79. protected void LinearFollow()
  80. {
  81. originPos = transform.position;
  82. desPos = CalculateWindowPosition(Camera.main.transform);
  83. journeyLength = Vector3.Distance(originPos, desPos);
  84. transform.position = Vector3.Lerp(originPos, desPos, (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  85. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  86. }
  87. protected override Vector3 CalculateWindowPosition(Transform cameraTransform)
  88. {
  89. Vector3 position = cameraTransform.position + (Vector3.ProjectOnPlane(cameraTransform.forward,Vector3.up).normalized * WindowDistance);
  90. Vector3 horizontalOffset = cameraTransform.right * windowOffset.x;
  91. Vector3 verticalOffset = cameraTransform.up * windowOffset.y;
  92. switch (windowAnchor)
  93. {
  94. case TextAnchor.UpperLeft: position += verticalOffset - horizontalOffset; break;
  95. case TextAnchor.UpperCenter: position += verticalOffset; break;
  96. case TextAnchor.UpperRight: position += verticalOffset + horizontalOffset; break;
  97. case TextAnchor.MiddleLeft: position -= horizontalOffset; break;
  98. case TextAnchor.MiddleRight: position += horizontalOffset; break;
  99. case TextAnchor.MiddleCenter: position += horizontalOffset + verticalOffset; break;
  100. case TextAnchor.LowerLeft: position -= verticalOffset + horizontalOffset; break;
  101. case TextAnchor.LowerCenter: position -= verticalOffset; break;
  102. case TextAnchor.LowerRight: position -= verticalOffset - horizontalOffset; break;
  103. }
  104. return position;
  105. }
  106. protected override Quaternion CalculateWindowRotation(Transform cameraTransform)
  107. {
  108. Quaternion rotation = Quaternion.Euler(0, cameraTransform.rotation.eulerAngles.y, 0);
  109. switch (windowAnchor)
  110. {
  111. case TextAnchor.UpperLeft: rotation *= windowHorizontalRotationInverse * windowVerticalRotationInverse; break;
  112. case TextAnchor.UpperCenter: rotation *= windowHorizontalRotationInverse; break;
  113. case TextAnchor.UpperRight: rotation *= windowHorizontalRotationInverse * windowVerticalRotation; break;
  114. case TextAnchor.MiddleLeft: rotation *= windowVerticalRotationInverse; break;
  115. case TextAnchor.MiddleRight: rotation *= windowVerticalRotation; break;
  116. case TextAnchor.LowerLeft: rotation *= windowHorizontalRotation * windowVerticalRotationInverse; break;
  117. case TextAnchor.LowerCenter: rotation *= windowHorizontalRotation; break;
  118. case TextAnchor.LowerRight: rotation *= windowHorizontalRotation * windowVerticalRotation; break;
  119. }
  120. return rotation;
  121. }
  122. }