FPS-Camera bouncing around on the Y-Axis

Hello,

I am new at Unity and after watching multiple tutorials, I created this script to make a smooth FPS-Cam:

         public class CamMovement : MonoBehaviour
     {
         private float MouseSensivity;
         public float MaxY;
         public float MinY;
         public Transform Bodytrans;
         float xRotation;
         float yRotation;
         public Slider SensivitySlider;
         public float SpinSpeed;
    
     private void Start()
         {
             Cursor.lockState = CursorLockMode.Locked;
             SensivitySlider.value = 100;
         }
    
     void SetSliderValues(){...}
    
     void CursorLock(){...}
    
     void CamRotate()
       {
     float mouseX = Input.GetAxis("Mouse X") * MouseSensivity *
     Time.deltaTime;
     float mouseY = Input.GetAxis("Mouse Y") * MouseSensivity *
     Time.deltaTime;
    
             xRotation -= mouseY;
             yRotation += mouseX;
             xRotation = Mathf.Clamp(xRotation, MinY, MaxY);
             Quaternion RotationY = Quaternion.Euler(xRotation,0, 0);
             Quaternion RotationX = Quaternion.Euler(0, yRotation, 0);
             transform.localRotation = Quaternion.Lerp(transform.localRotation, RotationY, SpinSpeed * Time.deltaTime);
             Bodytrans.rotation = Quaternion.Lerp(Bodytrans.rotation, RotationX, SpinSpeed * Time.deltaTime);
      }
    
     void Update()
        {
             CamRotate();
             CursorLock();
             SetSliderValues();
         }
     }

The Problem: The Cam keeps bouncing on the Y-Axis when moving at fast speed. Example: I move the Cam to the right with high MouseSensivity. The mouse now bounces back to the left. This occurs at high speed.

I have tried using Bodytrans.Rotate() instead of Quaternion.Lerp() first; This does remove the bouncing, but it also causes a weird stuttering, like the Camera is skipping frames. If Bodytrans.Rotate() is the right choice, how do you remove the stuttering?

Thanks in advance!

Not sure about the bouncing back part, but as for weird stuttering:

float mouseX = Input.GetAxis("Mouse X") * MouseSensivity *
     Time.deltaTime;
     float mouseY = Input.GetAxis("Mouse Y") * MouseSensivity *
     Time.deltaTime;

You generally do NOT want to incorporate Time.deltaTime into calculations involving mouse deltas in Unity. The mouse delta is an absolute delta value that isn’t really affected by framerate because it’s automatically “the delta for THIS frame”. Think about this:

You move your mouse 1 inch in 1 second. If the game is running at 1 frame per second, the mouse delta for that frame will be 1 inch. If the game is running at 2 frames per second, the mouse delta will be half an inch, for each of two frames. In neither case do you want to incorporate the frame duration in that calculation.

TL;DR: remove Time.deltaTime from when you’re reading the mouse axes, it’s hurting you.

To further support this, see here:

1 Like

I actually have a similar script for a fps cam. I am currently not at my pc but if I can I will try to post it as an exsample. You probably took the fps cam script from brackeys right? :wink: