CameraBird.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using DG.Tweening;
  2. using System;
  3. using Unity.Mathematics;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. public class CameraBird : MonoBehaviour
  8. {
  9. public Transform target; // 目标对象,摄像头将朝向此对象
  10. public float smoothSpeed = 0.125f; // 摄像头平滑移动的速度
  11. public float scrollSensitivity = 5f; // 鼠标滚轮的灵敏度
  12. public float rotateSpeed = 0.25f;
  13. public float translateSpeed = 3f;
  14. public float minDistance = 5f; // 摄像头与目标对象的最小距离
  15. public float maxDistance = 50f; // 摄像头与目标对象的最大距离
  16. public float currentDistance; // 当前摄像头与目标对象的距离
  17. private Vector3 offset; // 摄像头与目标对象的偏移量
  18. private Vector3 velocity = Vector3.zero; // 摄像头移动的速度
  19. private bool isDragging = false; // 是否正在拖拽
  20. private Vector3 lastMousePosition; // 上一帧鼠标位置
  21. public float rotateYAngle = 0.0f;
  22. public float rotateXAngle = 0.0f;
  23. bool isRotate = false;
  24. bool onUI = false;
  25. public Action OnCameraBeginChange;
  26. public Action OnCameraEndChange;
  27. private bool isChanging = false;
  28. public bool onScroll;
  29. float scrollCountDown = 0.5f;
  30. void Start()
  31. {
  32. // 初始化当前距离为初始偏移量的距离
  33. currentDistance = Vector3.Distance(transform.position, target.position);
  34. // 确保初始距离在允许的范围内
  35. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  36. // 计算偏移量
  37. offset = transform.position - target.position;
  38. }
  39. private bool IsPointerOverUIElement()
  40. {
  41. // 检查当前鼠标位置是否在 UI 上
  42. return EventSystem.current.IsPointerOverGameObject();
  43. }
  44. void LateUpdate()
  45. {
  46. onUI = false;
  47. if (IsPointerOverUIElement()) onUI = true;
  48. this.GetComponent<Camera>().nearClipPlane = currentDistance / 100.0f;
  49. // 鼠标滚轮控制摄像头远近
  50. if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)
  51. {
  52. // 更新当前距离
  53. currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
  54. // 确保距离在允许的范围内
  55. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  56. scrollCountDown = 0.5f;
  57. onScroll = true;
  58. //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
  59. }
  60. else {
  61. scrollCountDown -= Time.deltaTime;
  62. if (scrollCountDown < 0) {
  63. onScroll = false;
  64. }
  65. }
  66. translateSpeed = currentDistance / 600f;
  67. scrollSensitivity = currentDistance / 16.0f * currentDistance / 16.0f;
  68. scrollSensitivity = Mathf.Clamp(scrollSensitivity, 10, 4000);
  69. if (Input.GetMouseButton(2))
  70. {
  71. rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
  72. rotateYAngle += Input.GetAxis("Mouse X") * rotateSpeed;
  73. rotateXAngle = Math.Clamp(rotateXAngle, 2, 1000);
  74. isRotate = true;
  75. }
  76. else {
  77. isRotate = false;
  78. }
  79. // 鼠标左键拖拽平移摄像头
  80. if (Input.GetMouseButtonDown(1) && !onUI)
  81. {
  82. isDragging = true;
  83. lastMousePosition = Input.mousePosition;
  84. }
  85. else if (Input.GetMouseButtonUp(1))
  86. {
  87. isDragging = false;
  88. }
  89. if (isDragging)
  90. {
  91. // 计算鼠标移动的偏移量
  92. Vector3 mouseOffset = Input.mousePosition - lastMousePosition;
  93. // 根据偏移量计算摄像头的目标位置
  94. Vector3 moveDirection = new Vector3(transform.forward.x, 0, transform.forward.z) * -mouseOffset.y * translateSpeed + new Vector3(transform.right.x, 0, transform.right.z) * -mouseOffset.x * translateSpeed;
  95. // 平滑移动摄像头到目标位置
  96. target.position += moveDirection;
  97. // 计算目标位置
  98. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  99. // 摄像头平滑移动到目标位置
  100. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed * 2);
  101. }
  102. else
  103. {
  104. if (isRotate)
  105. {
  106. Blink();
  107. }
  108. else
  109. {
  110. // 计算目标位置
  111. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  112. // 摄像头平滑移动到目标位置
  113. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  114. }
  115. }
  116. lastMousePosition = Input.mousePosition;
  117. }
  118. public void Blink()
  119. {
  120. // 计算目标位置
  121. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  122. // 摄像头平滑移动到目标位置
  123. transform.position = targetPosition;
  124. //摄像头朝向目标对象
  125. transform.rotation = Quaternion.LookRotation(target.position - this.transform.position, Vector3.up);
  126. }
  127. public void SetCameraToCenterFade(Vector3 centerPos,float distance = 300)
  128. {
  129. if (onUI) {
  130. return;
  131. }
  132. MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();
  133. fader.gameObject.SetActive(true);
  134. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  135. {
  136. fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>
  137. {
  138. fader.gameObject.SetActive(false);
  139. };
  140. };
  141. this.target.localPosition = centerPos;
  142. DOTween.To(()=>this.currentDistance,x=>this.currentDistance=x, distance,0.5f);
  143. this.Blink();
  144. }
  145. void Update()
  146. {
  147. if (isRotate || isDragging || onScroll) {
  148. if (!isChanging) {
  149. isChanging = true;
  150. OnCameraBeginChange?.Invoke();
  151. }
  152. }
  153. else{
  154. if (isChanging) {
  155. isChanging = false;
  156. OnCameraEndChange?.Invoke();
  157. }
  158. }
  159. }
  160. }