链接:yisu.com/zixun/317311.html

    1. #if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
    2. #define USE_INPUT_SYSTEM
    3. using UnityEngine.InputSystem;
    4. using UnityEngine.InputSystem.Controls;
    5. #endif
    6. using UnityEngine;
    7. public class SimpleCameraController : MonoBehaviour
    8. {
    9. #region 相机状态
    10. /// <summary>
    11. /// 相机状态
    12. /// </summary>
    13. class CameraState
    14. {
    15. public float yaw;
    16. public float pitch;
    17. public float roll;
    18. public float x;
    19. public float y;
    20. public float z;
    21. public void SetFromTransform(Transform t)
    22. {
    23. pitch = t.eulerAngles.x;
    24. yaw = t.eulerAngles.y;
    25. roll = t.eulerAngles.z;
    26. x = t.position.x;
    27. y = t.position.y;
    28. z = t.position.z;
    29. }
    30. public void Translate(Vector3 translation)
    31. {
    32. Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;
    33. x += rotatedTranslation.x;
    34. y += rotatedTranslation.y;
    35. z += rotatedTranslation.z;
    36. }
    37. public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
    38. {
    39. yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
    40. pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
    41. roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
    42. x = Mathf.Lerp(x, target.x, positionLerpPct);
    43. y = Mathf.Lerp(y, target.y, positionLerpPct);
    44. z = Mathf.Lerp(z, target.z, positionLerpPct);
    45. }
    46. public void UpdateTransform(Transform t)
    47. {
    48. t.eulerAngles = new Vector3(pitch, yaw, roll);
    49. t.position = new Vector3(x, y, z);
    50. }
    51. }
    52. #endregion
    53. CameraState m_TargetCameraState = new CameraState();
    54. CameraState m_InterpolatingCameraState = new CameraState();
    55. [Header("Movement Settings 移动设置")]
    56. [Tooltip("Exponential boost factor on translation, controllable by mouse wheel. 平移的指数增强因子,可通过鼠标滚轮控制。")]
    57. public float boost = 3.5f;
    58. [Tooltip("Time it takes to interpolate camera position 99% of the way to the target. 将相机位置插值到目标位置99%所需的时间。"), Range(0.001f, 1f)]
    59. public float positionLerpTime = 0.2f;
    60. [Header("Rotation Settings 旋转设定")]
    61. [Tooltip("X = Change in mouse position. 改变鼠标位置。\nY = Multiplicative factor for camera rotation. 相机旋转的乘性因子。")]
    62. public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));
    63. [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target. 插值相机旋转99%到目标所需的时间。"), Range(0.001f, 1f)]
    64. public float rotationLerpTime = 0.01f;
    65. [Tooltip("Whether or not to invert our Y axis for mouse input to rotation. 是否将鼠标输入的Y轴反转为旋转。")]
    66. public bool invertY = false;
    67. void OnEnable()
    68. {
    69. m_TargetCameraState.SetFromTransform(transform);
    70. m_InterpolatingCameraState.SetFromTransform(transform);
    71. }
    72. Vector3 GetInputTranslationDirection()
    73. {
    74. Vector3 direction = new Vector3();
    75. if (Input.GetKey(KeyCode.W))
    76. {
    77. direction += Vector3.forward;
    78. }
    79. if (Input.GetKey(KeyCode.S))
    80. {
    81. direction += Vector3.back;
    82. }
    83. if (Input.GetKey(KeyCode.A))
    84. {
    85. direction += Vector3.left;
    86. }
    87. if (Input.GetKey(KeyCode.D))
    88. {
    89. direction += Vector3.right;
    90. }
    91. if (Input.GetKey(KeyCode.Q))
    92. {
    93. direction += Vector3.down;
    94. }
    95. if (Input.GetKey(KeyCode.E))
    96. {
    97. direction += Vector3.up;
    98. }
    99. return direction;
    100. }
    101. void Update()
    102. {
    103. Vector3 translation = Vector3.zero;
    104. #if ENABLE_LEGACY_INPUT_MANAGER
    105. // Exit Sample 按下Esc键退出游戏
    106. if (Input.GetKey(KeyCode.Escape))
    107. {
    108. Application.Quit();
    109. #if UNITY_EDITOR
    110. UnityEditor.EditorApplication.isPlaying = false;
    111. #endif
    112. }
    113. // Hide and lock cursor when right mouse button pressed 按下鼠标右键时隐藏并锁定光标
    114. if (Input.GetMouseButtonDown(1))
    115. {
    116. Cursor.lockState = CursorLockMode.Locked;
    117. }
    118. // Unlock and show cursor when right mouse button released 松开鼠标右键时解锁并显示光标
    119. if (Input.GetMouseButtonUp(1))
    120. {
    121. Cursor.visible = true;
    122. Cursor.lockState = CursorLockMode.None;
    123. }
    124. // Rotation 旋转
    125. if (Input.GetMouseButton(1))
    126. {
    127. var mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY &#63; 1 : -1));
    128. var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);
    129. m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
    130. m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
    131. }
    132. // Translation 移动
    133. translation = GetInputTranslationDirection() * Time.deltaTime;
    134. // Speed up movement when shift key held 按住shift键时加速移动
    135. if (Input.GetKey(KeyCode.LeftShift))
    136. {
    137. //原速度*10为按下Shift后的速度
    138. translation *= 10.0f;
    139. }
    140. // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel) 通过增强因子修改移动(在检查器中定义,通过鼠标滚轮在播放模式下修改)
    141. boost += Input.mouseScrollDelta.y * 0.2f;
    142. translation *= Mathf.Pow(2.0f, boost);
    143. #elif USE_INPUT_SYSTEM
    144. // TODO: make the new input system work 使新的输入系统正常工作
    145. #endif
    146. m_TargetCameraState.Translate(translation);
    147. // Framerate-independent interpolation 帧率无关插值
    148. // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time 计算lerp的数量,这样我们就可以在指定的时间内到达目标的99%
    149. var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
    150. var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
    151. m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);
    152. m_InterpolatingCameraState.UpdateTransform(transform);
    153. }
    154. }