Home > OS >  Is there a simple way of creating a spring-like camera rotation out of this script? (Unity C#)
Is there a simple way of creating a spring-like camera rotation out of this script? (Unity C#)

Time:11-30

This is the script im using. This is the result im looking for ex: https://imgur.com/a/1L2wV52

    void Update()
    {
        float mouseX = Input.GetAxisRaw("Mouse X") * Time.fixedDeltaTime * _sensitivityX;
        float mouseY = Input.GetAxisRaw("Mouse Y") * Time.fixedDeltaTime * _sensitivityY;

        yRotation  = mouseX;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        camHolder.rotation = Quaternion.Euler(xRotation, yRotation, 0);
        orientation.rotation = Quaternion.Euler(0, yRotation, 0);
    }

The result doesn't have to be exact but somewhat similar, I have tried using lerp but it didn't quite work.. If you have any answers to this, I would really appreciate it, thanks!

CodePudding user response:

To achieve this effect you want to calculate the target rotation from the mouse input and then increase / decrease the rotation of the camera based on it. But you can not dirrectly link the mouse movement to the camera as you did, because it will not look smooth. You have to do rotate it over multiple frames. You also need to set a maximum delta between last frame cam angle and current frame cam angle.

If you also want to have an overshooting effect, then adding a velocity to the camera movement is the best solution in my opinion.

  • Related