CameraBirdSec.cs 6.2 KB

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