unity 相机旋转

从官方文档Copy出来的相机角度控制脚本:

public class FreeLookCamTest : MonoBehaviour
{
    [SerializeField] private float m_MoveSpeed = 1f;                      // How fast the rig will move to keep up with the target's position.
    [Range(0f, 10f)] [SerializeField] private float m_TurnSpeed = 1.5f;   // How fast the rig will rotate from user input.
    [SerializeField] private float m_TurnSmoothing = 0.0f;                // How much smoothing to apply to the turn input, to reduce mouse-turn jerkiness
    [SerializeField] private float m_TiltMax = 75f;                       // The maximum value of the x axis rotation of the pivot.
    [SerializeField] private float m_TiltMin = 45f;                       // The minimum value of the x axis rotation of the pivot.
    [SerializeField] private bool m_LockCursor = false;                   // Whether the cursor should be hidden and locked.
    [SerializeField] private bool m_VerticalAutoReturn = false;           // set wether or not the vertical axis should auto return
    private float m_LookAngle;                    // The rig's y axis rotation.
    private float m_TiltAngle;                    // The pivot's x axis rotation.
    private const float k_LookDistance = 100f;    // How far in front of the pivot the character's look target is.
    private Vector3 m_PivotEulers;
    private Quaternion m_PivotTargetRot;
    private Quaternion m_TransformTargetRot;
    Transform m_Cam;
    Transform m_Pivot;
    void Awake()
    {
        m_Cam = GetComponentInChildren<Camera>().transform;
        m_Pivot = m_Cam.parent;
        m_PivotEulers = m_Pivot.rotation.eulerAngles;
        m_PivotTargetRot = m_Pivot.transform.localRotation;
        m_TransformTargetRot = transform.localRotation;
    }
    protected void Update()
    {
        if (Input.GetMouseButton (0))
        {
              HandleRotationMovement();
        }    
    }
    private void HandleRotationMovement()
    {
        if (Time.timeScale < float.Epsilon)
            return;
        var x = Input.GetAxis("Mouse X");
        var y = Input.GetAxis("Mouse Y");     
        m_LookAngle += x * m_TurnSpeed;
        m_TransformTargetRot = Quaternion.Euler(0f, m_LookAngle, 0f);
            m_TiltAngle -= y * m_TurnSpeed;
            m_TiltAngle = Mathf.Clamp(m_TiltAngle, -m_TiltMin, m_TiltMax); 
        m_PivotTargetRot = Quaternion.Euler(m_TiltAngle, m_PivotEulers.y, m_PivotEulers.z);
        if (m_TurnSmoothing > 0)
        {
            m_Pivot.localRotation = Quaternion.Slerp(m_Pivot.localRotation, m_PivotTargetRot, m_TurnSmoothing * Time.deltaTime);
            transform.localRotation = Quaternion.Slerp(transform.localRotation, m_TransformTargetRot, m_TurnSmoothing * Time.deltaTime);
        }
        else
        {
            m_Pivot.localRotation = m_PivotTargetRot;
            transform.localRotation = m_TransformTargetRot;
        }
    }

//以下是原方法,以及解释
 //private void HandleRotationMovement()
    //{
    //    if (Time.timeScale < float.Epsilon)
    //        return;

    
    //    var x = Input .GetAxis("Mouse X");
    //    var y = Input .GetAxis("Mouse Y");

    //    // Adjust the look angle by an amount proportional to the turn speed and horizontal input.
    //    m_LookAngle += x * m_TurnSpeed;

    //    // Rotate the rig (the root object) around Y axis only:
    //    m_TransformTargetRot = Quaternion.Euler(0f, m_LookAngle, 0f);

    //    if (m_VerticalAutoReturn)
    //    {
    //        // For tilt input, we need to behave differently depending on whether we're using mouse or touch input:
    //        // on mobile, vertical input is directly mapped to tilt value, so it springs back automatically when the look input is released
    //        // we have to test whether above or below zero because we want to auto-return to zero even if min and max are not symmetrical.
    //        m_TiltAngle = y > 0 ? Mathf.Lerp(0, -m_TiltMin, y) : Mathf.Lerp(0, m_TiltMax, -y);
    //    }
    //    else
    //    {
    //        // on platforms with a mouse, we adjust the current angle based on Y mouse input and turn speed
    //        m_TiltAngle -= y * m_TurnSpeed;
    //        // and make sure the new value is within the tilt range
    //        m_TiltAngle = Mathf.Clamp(m_TiltAngle, -m_TiltMin, m_TiltMax);
    //    }

    //    // Tilt input around X is applied to the pivot (the child of this object)
    //    m_PivotTargetRot = Quaternion.Euler(m_TiltAngle, m_PivotEulers.y, m_PivotEulers.z);

    //    if (m_TurnSmoothing > 0)
    //    {
    //        m_Pivot.localRotation = Quaternion.Slerp(m_Pivot.localRotation, m_PivotTargetRot, m_TurnSmoothing * Time.deltaTime);
    //        transform.localRotation = Quaternion.Slerp(transform.localRotation, m_TransformTargetRot, m_TurnSmoothing * Time.deltaTime);
    //    }
    //    else
    //    {
    //        m_Pivot.localRotation = m_PivotTargetRot;
    //        transform.localRotation = m_TransformTargetRot;
    //    }
    //}
}

物体层级关系:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值