how do i stop my camera from jittering?

For some reason the camera in my game is jittering - even though the player has almost the exact same code and doesn’t have any problems. I tried using interpolation on the camera but it didn’t help at all. It seems to only jitter when the player moves and turns at the same time (it’s a first person game so the camera does the same movements) or when it touches something other than the ground. I also just added a physics material with 0 bounciness and the camera problem is even worse now.
Here’s the code that i stole off some other people smarter than me and slightly modified

public class CameraFollow : MonoBehaviour
{
    public float mouseSensitivity = 800.0f;
    public float clampAngle = 90.0f;
    private float rotY = 0.0f;
    private float rotX = 0.0f;
    public Transform player;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Vector3 rot = transform.localRotation.eulerAngles;
        rotY = rot.y;
        rotX = rot.x;
    }

    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = -Input.GetAxis("Mouse Y");
        rotY += mouseX * mouseSensitivity * Time.deltaTime;
        rotX += mouseY * mouseSensitivity * Time.deltaTime;
        rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
        Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
        transform.rotation = localRotation;

        transform.position = player.position + new Vector3(0f, 0.75f, 0f);
    }
}

There are a few things wrong with your code that I would suggest fixing.


  1. If you are using physics, Use FixedUpdate(), otherwise use LateUpdate(). This likely will resolve your issue.

  2. Lerp between your camera’s current position and the player position.
    It doesnt have to be much:

transform.position = Vector3.Lerp(transform.position, player.position, Time.deltaTime * lerpSensitivity);

Play with the sensitivity a bit. A high value will be instantaneous. Start at 1 and go from there.


I hope this helps. If not, let me know and I can help you troubleshoot it some more.

~Andy.