using System.Collections; using System.Collections.Generic; using UnityEngine; /******************************************************************************** *Create By CG *Function *********************************************************************************/ namespace ZXK.UTility { public class CameraControl : MonoBehaviour { #region 相机状态 /// /// 相机状态 /// class CameraState { public float yaw; public float pitch; public float roll; public float x; public float y; public float z; public void SetFromTransform(Transform t) { pitch = t.eulerAngles.x; yaw = t.eulerAngles.y; roll = t.eulerAngles.z; x = t.position.x; y = t.position.y; z = t.position.z; } public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct) { yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct); pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct); roll = Mathf.Lerp(roll, target.roll, rotationLerpPct); x = Mathf.Lerp(x, target.x, positionLerpPct); y = Mathf.Lerp(y, target.y, positionLerpPct); z = Mathf.Lerp(z, target.z, positionLerpPct); } public void UpdateTransform(Transform t,Transform camera) { camera.eulerAngles = new Vector3(pitch, yaw, roll); } } #endregion CameraState m_TargetCameraState = new CameraState(); CameraState m_InterpolatingCameraState = new CameraState(); [Header("Movement Settings 移动设置")] [Tooltip("Exponential boost factor on translation, controllable by mouse wheel. 平移的指数增强因子,可通过鼠标滚轮控制。")] public float boost = 3.5f; [Tooltip("Time it takes to interpolate camera position 99% of the way to the target. 将相机位置插值到目标位置99%所需的时间。"), Range(0.001f, 1f)] public float positionLerpTime = 0.2f; [Header("Rotation Settings 旋转设定")] [Tooltip("X = Change in mouse position. 改变鼠标位置。\nY = Multiplicative factor for camera rotation. 相机旋转的乘性因子。")] public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f)); [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target. 插值相机旋转99%到目标所需的时间。"), Range(0.001f, 1f)] public float rotationLerpTime = 0.01f; [Tooltip("Whether or not to invert our Y axis for mouse input to rotation. 是否将鼠标输入的Y轴反转为旋转。")] public bool invertY = false; void OnEnable() { m_TargetCameraState.SetFromTransform(transform); m_InterpolatingCameraState.SetFromTransform(transform); } void Update() { // Rotation 旋转 if (Input.GetMouseButton(1)) { var mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1)); var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude); m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor; m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor; } //// Speed up movement when shift key held 按住shift键时加速移动 //if (Input.GetKey(KeyCode.LeftShift)) //{ // //原速度*10为按下Shift后的速度 // translation *= 10.0f; //} // Framerate-independent interpolation 帧率无关插值 // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time 计算lerp的数量,这样我们就可以在指定的时间内到达目标的99% var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime); var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime); m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct); m_InterpolatingCameraState.UpdateTransform(transform,transform.GetComponentInChildren().transform); } } }