Camera used Quaternion.Lerp or Slerp to Rotate can't work well when t is not 1

I use mouse LeftButton to rotate camera. But when I run the program, I find that if Quaternion.Lerp(loaclRotation, newRotation, t) t !=1, the camera.transform.localRotation and parent.transform will not equal to xR and yR. After rotating, if clicked mouse leftbutton, camera will rotate a little angle.
I think the reason will be that when t < 1 , the final rotation not equal xR or yR.so when I clicked , it will execute Quaternion.Lerp again, until the camera.localRotation equal xR or yR.

I make the t value is 1, so the Camera localRotation will be equal to xR or yR.
Is there any other solution to solve this problem?

`

if ((mouseState == MouseState.None ||mouseState == MouseState.LeftMouseBtn) && mouseLeftDown) {
            float mouseX = Input.GetAxis("Mouse X");
            float mouseY = Input.GetAxis("Mouse Y");
            smoothMouseX = mouseX;
            smoothMouseY = mouseY;

        
        var x = smoothMouseX * xRSpeed * Time.deltaTime;
        var y = smoothMouseY * yRSpeed * Time.deltaTime;

        xR -= x ;
        yR += y ;
        //Debug.Log("xR:" + xR + "yR:" + yR);
        xR = ClampValue(xR, -360, 360);
        yR = ClampValue(yR, yRMin, yRMax);

        var rotation_x = Quaternion.Euler(yR, 0, 0);
        var rotation_y = Quaternion.Euler(0, xR, 0);

        //camera's parent component rotate the Y    
        transform.localRotation = Quaternion.Lerp(transform.localRotation, rotation_y , 1f);
        //camera transform rotate the X

        camera.transform.localRotation = Quaternion.Lerp(camera.transform.localRotation, rotation_x, 1f);

`

Hi @Jaquior_qiao ,

you problem exist because Quaternion.Lerp is called only when (mouseState == MouseState.None ||mouseState == MouseState.LeftMouseBtn) && mouseLeftDown)…
so in order to achieve position t needs to be 1.

but if you change approach, maybe you will get expected result, so you will need to cache Quaternions outside of Update:

Quaternion m_parentDesiredQuaternion;
Quaternion m_cameraDesiredQuaternion;

    void Update()
    {
        if (( mouseState == MouseState.LeftMouseBtn) && mouseLeftDown)
        {
            float mouseX = Input.GetAxis("Mouse X");
            float mouseY = Input.GetAxis("Mouse Y");
            smoothMouseX = mouseX;
            smoothMouseY = mouseY;


            var x = smoothMouseX * xRSpeed * Time.deltaTime;
            var y = smoothMouseY * yRSpeed * Time.deltaTime;

            xR -= x;
            yR += y;

            xR = ClampValue(xR, -360, 360);
            yR = ClampValue(yR, yRMin, yRMax);

            m_cameraDesiredQuaternion = Quaternion.Euler(yR, 0, 0);
            m_parentDesiredQuaternion = Quaternion.Euler(0, xR, 0);
        }

        //camera's parent component rotate the Y    
        transform.localRotation = Quaternion.Lerp(transform.localRotation, m_parentDesiredQuaternion, .3f);
        //camera transform rotate the X
        camera.transform.localRotation = Quaternion.Lerp(camera.transform.localRotation, m_cameraDesiredQuaternion, .3f);
    }

so Lerp is called outside of If Mouse Input Check, and values like .3f instead of 1, can make sense…

Hope will do! Let me know how it went.

thank you very much! It works. I should update the Rotation Quaternion in If sentence, and update the camera Rotation out of it. Then the Camera will set the right Rotation. :smiling_face_with_three_hearts:

1 Like