Follower.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Follower : 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 (InstantFollowing)
  32. {
  33. InstantFollow();
  34. }
  35. else
  36. {
  37. if (IsFollower())
  38. {
  39. if (LinearFollowing)
  40. {
  41. LinearFollow();
  42. }
  43. else
  44. {
  45. Follow();
  46. }
  47. }
  48. }
  49. }
  50. }
  51. void InitTrans()
  52. {
  53. if (Camera.main != null)
  54. {
  55. _slamHead =Camera.main.transform;
  56. transform.position = CalculateWindowPosition(Camera.main.transform.transform);
  57. transform.rotation = CalculateWindowRotation(Camera.main.transform);
  58. initViewPoint =Camera.main.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));//相机画面中点坐标
  59. }
  60. }
  61. protected bool IsFollower()
  62. {
  63. viewPoint =Camera.main.WorldToViewportPoint(transform.position);// 面板在相机画面上的坐标
  64. 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())//边界判断
  65. {
  66. isFollower = true;
  67. }
  68. else if (Mathf.Abs(viewPoint.x - initViewPoint.x - CalculateWindowOffsetX()) < 0.03f && Mathf.Abs(viewPoint.y - initViewPoint.y - 2*CalculateWindowOffsetY()) < 0.06f)
  69. {
  70. isFollower = false;
  71. }
  72. return isFollower;
  73. }
  74. protected override void Follow()
  75. {
  76. transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  77. //transform.position = Vector3.SmoothDamp(transform.position, CalculateWindowPosition(Camera.main.transform), ref velocity, WindowFollowSpeed);
  78. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  79. }
  80. //Following camera without latency
  81. protected void InstantFollow()
  82. {
  83. transform.position = CalculateWindowPosition(Camera.main.transform);
  84. transform.rotation = CalculateWindowRotation(Camera.main.transform);
  85. }
  86. //For Linear Following, turn down the WindowFollowSpeed to around 0.5 at distance = 1 for best experience
  87. protected void LinearFollow()
  88. {
  89. originPos = transform.position;
  90. desPos = CalculateWindowPosition(Camera.main.transform);
  91. journeyLength = Vector3.Distance(originPos, desPos);
  92. transform.position = Vector3.Lerp(originPos, desPos, (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  93. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  94. }
  95. }