How do I fix Camera stuttering in first person?

I have a simple scene with a player in it. The player is a Capsule with a rigidbody and holds all scripts concerning movement, Camera movement, etc. The player-GameObject is also the parent of a Camera- GameObject, which I use for the first person perspective. The problem is the following: When I move the rigidbody of the player using force, the camera stutters. The movement loop of the player is run in FixedUpdate and the Camera movement loop is run in LateUpdate.

Here is the script for the movement:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
    
     public class Walking : MonoBehaviour
     {
         public Rigidbody playerRigidb;
         public Camera firstPersonCam;
         public float moveSpeed;
         float hor;
         float ver;
    
         void getInput()
         {
             hor = Input.GetAxisRaw("Horizontal");
             ver = Input.GetAxisRaw("Vertical");
         }
    
         void Walk()
         {
             playerRigidb.AddForce(transform.forward * ver + transform.right * hor, ForceMode.VelocityChange);
         }
    
         void Update()
         {
             getInput();
         }
    
         private void FixedUpdate()
         {
             Walk();
         }
     }

And here is the script to move the Camera:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonCamera : MonoBehaviour
{
     public Rigidbody playerRigidb;
     public Camera firstPersonCam;
     float mouseX;
     float mouseY;
     float rotX;
     float rotY;
     public float mouseRotationSpeed;
     // Start is called before the first frame update
     void GetInput()
     {
         mouseX = Input.GetAxisRaw("Mouse X");
         mouseY = Input.GetAxisRaw("Mouse Y");
         rotX += mouseY * mouseRotationSpeed;
         rotY += mouseX * mouseRotationSpeed;
         rotX = Mathf.Clamp(rotX, -90, 90);
     }
     void LockCursor()
     {
         if (Input.GetKey(KeyCode.Escape))
             Cursor.lockState = CursorLockMode.None;
         else
             Cursor.lockState = CursorLockMode.Locked;
     }
     // Update is called once per frame
     void Update()
     {     
         GetInput();      
         LockCursor();
     }
     private void LateUpdate()
     {
         firstPersonCam.transform.localRotation = Quaternion.Euler(-rotX, 0, 0);
         transform.rotation = Quaternion.Euler(0, rotY, 0);
     }
}

I already tried:

  • Using different types of “Update”

  • Interpolation in rigidbody

  • Removing the Camera as child of the player

And I also tried searching for other answers on the internet, but nothing helped me.

Thank you in advance!

Your camera script doesn’t move the camera at all. I’d guess your camera is a child of the player? The camera script is basically irrelevant if the stuttering is caused by motion of the player (as opposed to rotation). Simply enabling interpolation on your Rigidbody should eliminate stuttering of this sort.

1 Like

Thank you for your answer. Yes, the Camera is a Child of the player and interpolation is already enabled.

Can you clarify if the stuttering is related to the character moving or the character rotating?

If your camera is stuttering when you both moving and rotating, then it’s quite common problem.
The only solution I found myself is detaching camera from your player and moving it with interpolation.
Your invisible player body will move in FixedUpdate and your camera will follow it with some interpolation in Update or LateUpdate.

The stuttering occurs when both moving and rotating at once

How would I add interpolation to the camera?

You simply write:

var lerpSpeed = 30;
camera.transform.position = Vector3.Lerp(camera.transform.position, player.transform.position, Time.deltaTime * lerpSpeed);

Then you can tweak lerpSpeed as you want.

1 Like

I tried and it improved the camera, but there is still some stutter

Try adding interpolation for rotation too.

1 Like

As an experiment, try putting GetInput() in LateUpdate, in your originally posted Camera rotation script.