Jitter while moving and looking

Hi I am currently making a first person controller. Everything looks smooth when i walk or look around but only when i do one at the time. When i move and look around everything jitters.

Here is the code below:

CameraMouseFirstPerson.cs

public class CameraMouseFirstPerson : MonoBehaviour
{

    public float moveSpeedX = 10;
    public float moveSpeedY = 10;

    void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        float pitch = Input.GetAxis("Mouse Y") * -moveSpeedY;
        float yaw = Input.GetAxis("Mouse X") * moveSpeedX;

        transform.Rotate(0, yaw, 0, Space.World);
        transform.Rotate(pitch, 0, 0, Space.Self);
    }
}

PlayerWalker.cs

public class PlayerWalker : MonoBehaviour
{
    [SerializeField]
    private float moveSpeed = 10;

    [SerializeField]
    private float moveCap = 10;

    [SerializeField]
    private Rigidbody rigidbody = null;

    void Update()
    {
        float deltaForward = Input.GetAxis("Vertical") * moveSpeed;
        float deltaSide = Input.GetAxis("Horizontal") * moveSpeed;

        Vector3 dir = (transform.rotation * Vector3.forward * deltaForward) + (transform.rotation * Vector3.right * deltaSide);
        dir.y = 0;


        if(rigidbody.velocity.magnitude < moveCap)
            rigidbody.velocity += dir * Time.deltaTime;

    }
}

Player entity

Any help would be appreciated :smile:

Camera in LateUpdate, player control FixedUpdate

1 Like

This

All physics manipulations should be done in FixedUpdate because FixedUpdate is called immediately before the physics update, and both are called independently of the frame rate. So in Update you want to take the input, and then apply the input that affects physics in FixedUpdate.

You move the camera moved after the player’s character has moved each frame, and so long as you aren’t moving the character in LateUpdate as well, then LateUpdate is a good place to move the camera.