FreeCameraController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class FreeCameraController : MonoBehaviour
  5. {
  6. [Header("移动")]
  7. public float moveSpeed = 10f;
  8. public float fastSpeedMultiplier = 2f;
  9. [Range(0f, 1f)] public float moveSmooth = 0.15f; // 0=无平滑, 1=很丝滑
  10. [Header("旋转")]
  11. public float rotateSpeed = 120f; // 角速度(度/秒),右键拖动
  12. [Range(0f, 1f)] public float rotateSmooth = 0.12f; // 0=无平滑
  13. public float minPitch = -80f, maxPitch = 80f;
  14. [Header("缩放/推进(滚轮)")]
  15. public float zoomSpeed = 8f;
  16. [Range(0f, 1f)] public float zoomSmooth = 0.15f; // 滚轮惯性
  17. [Header("拖拽")]
  18. public float dragSpeed = 0.5f; // 中键左右拖动速度
  19. [Header("按键")]
  20. public KeyCode fastKey = KeyCode.LeftShift;
  21. [Header("调试")]
  22. public bool showDebug = false;
  23. float yaw, pitch;
  24. Vector3 targetPos;
  25. Quaternion targetRot;
  26. // 滚轮惯性速度
  27. float zoomVel = 0f;
  28. // 拖拽用
  29. Vector3 lastMousePos;
  30. void Awake()
  31. {
  32. var e = transform.eulerAngles;
  33. yaw = e.y; pitch = e.x;
  34. targetPos = transform.position;
  35. targetRot = transform.rotation;
  36. }
  37. void Update()
  38. {
  39. ReadInput();
  40. // 位置插值(moveSmooth=0 -> 立即到位)
  41. float mt = LerpFactor(moveSmooth);
  42. transform.position = Vector3.Lerp(transform.position, targetPos, mt);
  43. // 旋转插值(rotateSmooth=0 -> 立即到位)
  44. float rt = LerpFactor(rotateSmooth);
  45. transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rt);
  46. }
  47. void ReadInput()
  48. {
  49. // —— 移动 —— //
  50. float speed = moveSpeed * (Input.GetKey(fastKey) ? fastSpeedMultiplier : 1f);
  51. Vector3 move = Vector3.zero;
  52. if (Input.GetKey(KeyCode.W)) move += transform.forward;
  53. if (Input.GetKey(KeyCode.S)) move -= transform.forward;
  54. if (Input.GetKey(KeyCode.D)) move += transform.right;
  55. if (Input.GetKey(KeyCode.A)) move -= transform.right;
  56. if (Input.GetKey(KeyCode.E)) move += transform.up;
  57. if (Input.GetKey(KeyCode.Q)) move -= transform.up;
  58. if (move.sqrMagnitude > 0f)
  59. targetPos += move.normalized * speed * Time.unscaledDeltaTime;
  60. // —— 旋转(右键)—— //
  61. if (Input.GetMouseButton(1))
  62. {
  63. yaw += Input.GetAxis("Mouse X") * rotateSpeed * Time.unscaledDeltaTime;
  64. pitch -= Input.GetAxis("Mouse Y") * rotateSpeed * Time.unscaledDeltaTime;
  65. pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
  66. targetRot = Quaternion.Euler(pitch, yaw, 0f);
  67. }
  68. // —— 滚轮推进(带惯性)—— //
  69. float scroll = Input.mouseScrollDelta.y;
  70. if (Mathf.Abs(scroll) > 0.001f)
  71. zoomVel += scroll * zoomSpeed;
  72. zoomVel = Mathf.Lerp(zoomVel, 0f, LerpFactor(zoomSmooth));
  73. if (Mathf.Abs(zoomVel) > 0.0001f)
  74. targetPos += transform.forward * zoomVel * Time.unscaledDeltaTime;
  75. // —— 中键左右拖动 —— //
  76. if (Input.GetMouseButtonDown(2))
  77. {
  78. lastMousePos = Input.mousePosition;
  79. }
  80. if (Input.GetMouseButton(2))
  81. {
  82. Vector3 delta = Input.mousePosition - lastMousePos;
  83. lastMousePos = Input.mousePosition;
  84. // 只取 x 分量(左右拖动),用相机的 right 向量
  85. Vector3 right = transform.right;
  86. targetPos -= right * delta.x * dragSpeed * Time.unscaledDeltaTime;
  87. }
  88. if (showDebug)
  89. Debug.Log($"move:{move} speed:{speed} scroll:{scroll} zoomVel:{zoomVel} pos:{transform.position}");
  90. }
  91. float LerpFactor(float smooth)
  92. {
  93. if (smooth <= 0f) return 1f;
  94. return 1f - Mathf.Pow(1f - Mathf.Clamp01(smooth), Time.unscaledDeltaTime * 60f);
  95. }
  96. public void SnapTo(Transform target)
  97. {
  98. transform.position = target.position;
  99. transform.rotation = target.rotation;
  100. targetPos = target.position;
  101. targetRot = target.rotation;
  102. Vector3 euler = target.rotation.eulerAngles;
  103. yaw = euler.y;
  104. pitch = euler.x;
  105. }
  106. }