CameraFollowerLangChao.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public class CameraFollowerLangChao : FollowerBase
  7. {
  8. [Header("Following Settings")]
  9. public bool InstantFollowing;//是否无延迟跟随
  10. public bool LinearFollowing;//是否线性移动
  11. public Vector2 menu_size;//面板的长宽,默认面板的position在中心。
  12. private Transform _slamHead;
  13. Vector2 initViewPoint;//相机画面的中点,坐标为(0.5f,0.5f)
  14. Vector3 viewPoint = Vector3.zero;// 面板坐标相对于相机画面的坐标。
  15. Vector3 viewPoint_right = Vector3.zero;// 面板的上下左右端相对于相机画面的坐标。
  16. Vector3 viewPoint_left = Vector3.zero;
  17. Vector3 viewPoint_top = Vector3.zero;
  18. Vector3 viewPoint_bot = Vector3.zero;
  19. bool isFollower = false;
  20. bool isFollower2 = false;
  21. Vector3 originPos = Vector3.zero;
  22. Vector3 desPos = Vector3.zero;
  23. float journeyLength = 0f;
  24. void Start()
  25. {
  26. _slamHead = Camera.main.transform;
  27. initViewPoint = Camera.main.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));
  28. InstantFollow();
  29. }
  30. public Vector3 nowV3;
  31. //Nonlinear Following by default
  32. protected override void Follow()
  33. {
  34. if (Camera.main == null)
  35. {
  36. return;
  37. }
  38. Vector3 v3 = CalculateWindowPosition(Camera.main.transform);
  39. nowV3 = new Vector3(v3.x, Camera.main.transform.position.y, v3.z);
  40. transform.position = Vector3.Lerp(transform.position,new Vector3(v3.x, Camera.main.transform.position.y, v3.z) , WindowFollowSpeed * Time.deltaTime);
  41. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler( new Vector3(0, CalculateWindowRotation(Camera.main.transform).eulerAngles.y, 0)), WindowFollowSpeed * Time.deltaTime);
  42. }
  43. //Following camera without latency
  44. protected void InstantFollow()
  45. {
  46. Vector3 v3 = CalculateWindowPosition(Camera.main.transform);
  47. transform.position = new Vector3(v3.x, Camera.main.transform.position.y, v3.z);
  48. transform.eulerAngles =new Vector3(0, CalculateWindowRotation(Camera.main.transform).eulerAngles.y,0);
  49. }
  50. //For Linear Following, turn down the WindowFollowSpeed to around 0.5 at distance = 1 for best experience
  51. protected void LinearFollow()
  52. {
  53. originPos = transform.position;
  54. desPos = CalculateWindowPosition(Camera.main.transform);
  55. journeyLength = Vector3.Distance(originPos, desPos);
  56. transform.position = Vector3.Lerp(originPos, new Vector3(desPos.x, Camera.main.transform.position.y, desPos.z), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  57. transform.position = new Vector3(transform.position.x, Camera.main.transform.position.y, transform.position.z);
  58. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(0, CalculateWindowRotation(Camera.main.transform).eulerAngles.y, 0)), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  59. }
  60. protected override void LateUpdate()
  61. {
  62. if ((StopFollower == false)&& !InstantFollowing)
  63. {
  64. if ( !isFollower2&& IsFollower())
  65. {
  66. StartCoroutine(startyidong());
  67. }
  68. }
  69. }
  70. IEnumerator startyidong()
  71. {
  72. while (IsFollower())
  73. {
  74. Follow();
  75. yield return new WaitForSeconds(0.2f);
  76. }
  77. isFollower2 = false;
  78. }
  79. protected bool IsFollower2()
  80. {
  81. if (Camera.main == null || Camera.main == null)
  82. {
  83. return false;
  84. }
  85. viewPoint = Camera.main.WorldToViewportPoint(transform.position);// 面板在相机画面上的坐标
  86. viewPoint_right = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(menu_size.x / 2, 0, 0)));// 面板边界在相机画面上的坐标
  87. viewPoint_left = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(-menu_size.x / 2, 0, 0)));
  88. viewPoint_top = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, menu_size.y / 2, 0)));
  89. viewPoint_bot = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, -menu_size.y / 2, 0)));
  90. if (viewPoint_right.x > 1 || viewPoint_left.x < 0 )//边界判断
  91. {
  92. isFollower2 = true;
  93. }
  94. else if (Mathf.Abs(viewPoint.x - initViewPoint.x) < 0.05f )//停止跟随的边界判断
  95. {
  96. isFollower2 = false;
  97. }
  98. return isFollower;
  99. }
  100. protected bool IsFollower()
  101. {
  102. if (Camera.main == null || Camera.main == null)
  103. {
  104. return false;
  105. }
  106. viewPoint = Camera.main.WorldToViewportPoint(transform.position);// 面板在相机画面上的坐标
  107. viewPoint_right = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(menu_size.x/2, 0, 0)));// 面板边界在相机画面上的坐标
  108. viewPoint_left = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(-menu_size.x/2, 0, 0)));
  109. viewPoint_top = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, menu_size.y/2, 0)));
  110. viewPoint_bot = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, -menu_size.y/2, 0)));
  111. if (viewPoint_right.x > 1.5f || viewPoint_left.x < -0.5f)//边界判断
  112. {
  113. isFollower = true;
  114. }
  115. else if (Mathf.Abs(viewPoint.x - initViewPoint.x) < 0.05f )//停止跟随的边界判断
  116. {
  117. isFollower = false;
  118. }
  119. return isFollower;
  120. }
  121. }
  122. }