ModelCameraCtrl.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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,Vector2 rota)
  41. {
  42. currentDistance = distance;
  43. this.transform.position = pos.position;
  44. rotateXAngle = rota.x;
  45. rotateYAngle = rota.y;
  46. }
  47. void LateUpdate()
  48. {
  49. onUI = false;
  50. if (IsPointerOverUIElement()) onUI = true;
  51. // 鼠标滚轮控制摄像头远近
  52. if (Input.GetAxis("Mouse ScrollWheel") != 0 && onUI)
  53. {
  54. // 更新当前距离
  55. currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
  56. // 确保距离在允许的范围内
  57. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  58. //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
  59. }
  60. translateSpeed = currentDistance / 600f;
  61. scrollSensitivity = currentDistance / 16.0f * currentDistance / 16.0f;
  62. scrollSensitivity = Mathf.Clamp(scrollSensitivity, 10, 4000);
  63. if (Input.GetMouseButton(0)&& onUI)
  64. {
  65. rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
  66. rotateYAngle += Input.GetAxis("Mouse X") * rotateSpeed;
  67. isRotate = true;
  68. }
  69. else
  70. {
  71. isRotate = false;
  72. }
  73. if (isRotate)
  74. {
  75. Blink();
  76. }
  77. else
  78. {
  79. // 计算目标位置
  80. Vector3 targetPosition = this.transform.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) *
  81. offset.normalized * currentDistance;
  82. // 摄像头平滑移动到目标位置
  83. _camera.transform.position = Vector3.Lerp(_camera.transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  84. }
  85. }
  86. private bool IsPointerOverUIElement()
  87. {
  88. // 检查当前鼠标位置是否在 UI 上
  89. return EventSystem.current.IsPointerOverGameObject();
  90. }
  91. public void Blink()
  92. {
  93. // 计算目标位置
  94. Vector3 targetPosition = this.transform.position +
  95. Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized *
  96. currentDistance;
  97. // 摄像头平滑移动到目标位置
  98. _camera.transform.position = targetPosition;
  99. //摄像头朝向目标对象
  100. _camera.transform.transform.rotation =
  101. Quaternion.LookRotation(this.transform.position - _camera.transform.position, Vector3.up);
  102. }
  103. }