CameraBird.cs 5.6 KB

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