Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Smooth Y-Axis object rotation in Unity3D

I turned on the Input System & refactored my code in my project and noticed that when I move the mouse my character "lags" when moving the mouse slowly – as if the camera jumps from one position to another instead of moving smoothly.

So I want to make it so that moving the camera and the character is smooth but I have a problem.

I managed to make the camera moving smoothly on the X-axis, but I can’t manage to make the character rotate smoothly on the Y-axis.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My current code responsible for rotating the camera and the character:

private void MovePlayerCamera()
    {
        // Get Y axis rotation and apply to capsule "player" (doesn't smooth rotation but i need to smooth it)
        float yRotation = playerMouseInput.x * sensitivity;
        transform.Rotate(0f, yRotation, 0f);

        // Apply Mouse Sensitivity & clamp xRotation so camera doesn't flip upside down
        xRotation -= playerMouseInput.y * sensitivity;
        xRotation = Mathf.Clamp(xRotation, -lowerMaxRotation, upperMaxRotation);

        // Setup a camera rotation goal and apply to player (this smoothing code works)
        Quaternion goal = Quaternion.Euler(xRotation, 0f, 0f);
        cameraPivot.transform.localRotation = Quaternion.Slerp(cameraPivot.localRotation, goal, cameraSmoothingStrength * Time.deltaTime);
    }

I have already tried:

        // Get Y axis rotation and apply to object (with this code player just rotates to right by itself)
        float yRotation = playerMouseInput.x * sensitivity;
        transform.Rotate(0f, Mathf.Lerp(transform.rotation.y, yRotation, cameraSmoothingStrength * Time.deltaTime), 0f);

But it doesn’t work – The character rotates to the right side by itself 🙁

Any help is welcome and thanks in advance.

>Solution :

Try this:

private float targetYRotation;

private void MovePlayerCamera()
{
    // Get Y axis rotation and store it as targetYRotation
    float yRotation = playerMouseInput.x * sensitivity;
    targetYRotation += yRotation;

    // Smoothly interpolate the current rotation to the targetYRotation
    Quaternion currentYRotation = transform.rotation;
    Quaternion targetYRotationQuat = Quaternion.Euler(0f, targetYRotation, 0f);
    transform.rotation = Quaternion.Slerp(currentYRotation, targetYRotationQuat, cameraSmoothingStrength * Time.deltaTime);

    // Apply Mouse Sensitivity & clamp xRotation so camera doesn't flip upside down
    xRotation -= playerMouseInput.y * sensitivity;
    xRotation = Mathf.Clamp(xRotation, -lowerMaxRotation, upperMaxRotation);

    // Setup a camera rotation goal and apply to player (this smoothing code works)
    Quaternion goal = Quaternion.Euler(xRotation, 0f, 0f);
    cameraPivot.transform.localRotation = Quaternion.Slerp(cameraPivot.localRotation, goal, cameraSmoothingStrength * Time.deltaTime);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading