ModelCameraCtrl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class ModelCameraCtrl : MonoBehaviour
  7. {
  8. public static ModelCameraCtrl _Instance;
  9. public Camera _camera;
  10. public float smoothSpeed = 5f; // 摄像头平滑移动的速度
  11. public float scrollSensitivity = 65f; // 鼠标滚轮的灵敏度
  12. public float rotateSpeed =3f;
  13. public float translateSpeed = 0.8f;
  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. public float rotateYAngle = 0.0f;
  20. public float rotateXAngle = 0.0f;
  21. bool isRotate = false;
  22. bool onUI = false;
  23. private void Awake()
  24. {
  25. _Instance = this;
  26. }
  27. public void SetCameraActive(bool flag)
  28. {
  29. _camera.enabled = flag;
  30. }
  31. private void Start()
  32. {
  33. SetCameraActive(false);
  34. // 初始化当前距离为初始偏移量的距离
  35. currentDistance = Vector3.Distance(_camera.transform.position, this.transform.position);
  36. // 确保初始距离在允许的范围内
  37. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  38. offset = _camera.transform.position - this.transform.position;
  39. }
  40. public void SetCameraPos(Transform pos,float distance)
  41. {
  42. currentDistance = distance;
  43. this.transform.position = pos.position;
  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. }
  58. translateSpeed = currentDistance / 600f;
  59. scrollSensitivity = currentDistance / 16.0f * currentDistance / 16.0f;
  60. scrollSensitivity = Mathf.Clamp(scrollSensitivity, 10, 4000);
  61. if (Input.GetMouseButton(0)&& onUI)
  62. {
  63. rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
  64. rotateYAngle += Input.GetAxis("Mouse X") * rotateSpeed;
  65. isRotate = true;
  66. }
  67. else
  68. {
  69. isRotate = false;
  70. }
  71. if (isRotate)
  72. {
  73. Blink();
  74. }
  75. else
  76. {
  77. // 计算目标位置
  78. Vector3 targetPosition = this.transform.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) *
  79. offset.normalized * currentDistance;
  80. // 摄像头平滑移动到目标位置
  81. _camera.transform.position = Vector3.Lerp(_camera.transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  82. }
  83. }
  84. private bool IsPointerOverUIElement()
  85. {
  86. // 检查当前鼠标位置是否在 UI 上
  87. return EventSystem.current.IsPointerOverGameObject();
  88. }
  89. public void Blink()
  90. {
  91. // 计算目标位置
  92. Vector3 targetPosition = this.transform.position +
  93. Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized *
  94. currentDistance;
  95. // 摄像头平滑移动到目标位置
  96. _camera.transform.position = targetPosition;
  97. //摄像头朝向目标对象
  98. _camera.transform.transform.rotation =
  99. Quaternion.LookRotation(this.transform.position - _camera.transform.position, Vector3.up);
  100. }
  101. }