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!

If you’re still struggling with this, or if anyone else with a similar problem comes across this, one possible solution is to have a non-child camera following using a tightened up smoothing function. The problem results from discrepancy between the physics timestep (targeting 50 Hz) and the framerate (highly variable). Basically, every once in a while the camera updates its transform more than once before the rigidbody catches up. You can mitigate this a bit by turning on interpolation on the rigidbody, but you will still get stutter. You can find an in-depth take on the problem here. Instead, I’ve found it helpful to keep the camera separate and tell it to follow the player very tightly with Vector3.SmoothDamp.


For example, in this exerpt I have the player controller keep track of the camera’s transform and smoothly pull it along:

public float cameraFollowSmoothTime = 0.05f; // Tweak in inspector.
public Vector3 cameraOffset; // Offset of the camera from the player.

private Vector3 cameraVelocity;

private void CameraFollow()
{
    Vector3 targetCamPos = transform.TransformPoint(cameraOffset);
    cameraTransform.position = Vector3.SmoothDamp(cameraTransform.position,
                                                  targetCamPos,
                                                  ref cameraVelocity, cameraFollowSmoothTime);
}

If you wanted, you could do this from a script on the camera as well, keeping track of the player’s transform. You would also want to make sure to set the correct initial position for the camera in Awake() or Start(). It’s similar to how a lot of 3rd person cameras work, but tightened up so that the camera is real close.


One disadvantage of this approach is that you have to move the player and camera independently in scene view. Also, any kind of teleportation would have to involve separately updating both the player’s and the camera’s positions.