CameraBird.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using DG.Tweening;
  2. using Unity.Mathematics;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class CameraBird : 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. bool isRotate = false;
  23. bool onUI = false;
  24. void Start()
  25. {
  26. // 初始化当前距离为初始偏移量的距离
  27. currentDistance = Vector3.Distance(transform.position, target.position);
  28. // 确保初始距离在允许的范围内
  29. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  30. // 计算偏移量
  31. offset = transform.position - target.position;
  32. }
  33. private bool IsPointerOverUIElement()
  34. {
  35. // 检查当前鼠标位置是否在 UI 上
  36. return EventSystem.current.IsPointerOverGameObject();
  37. }
  38. void LateUpdate()
  39. {
  40. onUI = false;
  41. if (IsPointerOverUIElement()) onUI = true;
  42. // 鼠标滚轮控制摄像头远近
  43. if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)
  44. {
  45. // 更新当前距离
  46. currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
  47. // 确保距离在允许的范围内
  48. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  49. //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
  50. }
  51. translateSpeed = currentDistance / 600f;
  52. scrollSensitivity = currentDistance / 16.0f * currentDistance / 16.0f;
  53. scrollSensitivity = Mathf.Clamp(scrollSensitivity, 10, 4000);
  54. if (Input.GetMouseButton(2))
  55. {
  56. rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
  57. rotateYAngle += Input.GetAxis("Mouse X") * rotateSpeed;
  58. isRotate = true;
  59. }
  60. else {
  61. isRotate = false;
  62. }
  63. // 鼠标左键拖拽平移摄像头
  64. if (Input.GetMouseButtonDown(0) && !onUI)
  65. {
  66. isDragging = true;
  67. lastMousePosition = Input.mousePosition;
  68. }
  69. else if (Input.GetMouseButtonUp(0))
  70. {
  71. isDragging = false;
  72. }
  73. if (isDragging)
  74. {
  75. // 计算鼠标移动的偏移量
  76. Vector3 mouseOffset = Input.mousePosition - lastMousePosition;
  77. // 根据偏移量计算摄像头的目标位置
  78. 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;
  79. // 平滑移动摄像头到目标位置
  80. target.position += moveDirection;
  81. // 计算目标位置
  82. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  83. // 摄像头平滑移动到目标位置
  84. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed * 2);
  85. }
  86. else
  87. {
  88. if (isRotate)
  89. {
  90. Blink();
  91. }
  92. else
  93. {
  94. // 计算目标位置
  95. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  96. // 摄像头平滑移动到目标位置
  97. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  98. }
  99. }
  100. lastMousePosition = Input.mousePosition;
  101. }
  102. public void Blink()
  103. {
  104. // 计算目标位置
  105. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  106. // 摄像头平滑移动到目标位置
  107. transform.position = targetPosition;
  108. //摄像头朝向目标对象
  109. transform.rotation = Quaternion.LookRotation(target.position - this.transform.position, Vector3.up);
  110. }
  111. public void SetCameraToCenterFade(Vector3 centerPos,float distance = 300)
  112. {
  113. MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();
  114. fader.gameObject.SetActive(true);
  115. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  116. {
  117. fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>
  118. {
  119. fader.gameObject.SetActive(false);
  120. };
  121. };
  122. this.target.localPosition = centerPos;
  123. DOTween.To(()=>this.currentDistance,x=>this.currentDistance=x, distance,0.5f);
  124. this.Blink();
  125. }
  126. void Update()
  127. {
  128. }
  129. }