ModelCameraCtrl.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. private void Start()
  28. {
  29. // 初始化当前距离为初始偏移量的距离
  30. currentDistance = Vector3.Distance(_camera.transform.position, this.transform.position);
  31. // 确保初始距离在允许的范围内
  32. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  33. offset = _camera.transform.position - this.transform.position;
  34. }
  35. public void SetCameraPos(Transform pos,float distance)
  36. {
  37. currentDistance = distance;
  38. this.transform.position = pos.position;
  39. }
  40. void LateUpdate()
  41. {
  42. onUI = false;
  43. if (IsPointerOverUIElement()) onUI = true;
  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(0)&& onUI)
  57. {
  58. rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
  59. rotateYAngle += Input.GetAxis("Mouse X") * rotateSpeed;
  60. isRotate = true;
  61. }
  62. else
  63. {
  64. isRotate = false;
  65. }
  66. if (isRotate)
  67. {
  68. Blink();
  69. }
  70. else
  71. {
  72. // 计算目标位置
  73. Vector3 targetPosition = this.transform.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) *
  74. offset.normalized * currentDistance;
  75. // 摄像头平滑移动到目标位置
  76. _camera.transform.position = Vector3.Lerp(_camera.transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  77. }
  78. }
  79. private bool IsPointerOverUIElement()
  80. {
  81. // 检查当前鼠标位置是否在 UI 上
  82. return EventSystem.current.IsPointerOverGameObject();
  83. }
  84. public void Blink()
  85. {
  86. // 计算目标位置
  87. Vector3 targetPosition = this.transform.position +
  88. Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized *
  89. currentDistance;
  90. // 摄像头平滑移动到目标位置
  91. _camera.transform.position = targetPosition;
  92. //摄像头朝向目标对象
  93. _camera.transform.transform.rotation =
  94. Quaternion.LookRotation(this.transform.position - _camera.transform.position, Vector3.up);
  95. }
  96. }