Character Controller Jittering while moving

I am building a game, where I have two characters that can be controlled independently. I am using Unity’s input system with character controllers. The camera is overhead looking down, and focuses on a point that is between the two characters, and will zoom in and out depending on how close they are. Everything is working fine until I try moving my characters in the same direction. The characters will jitter while moving. I’m not sure if it is an issue with the character movement or the camera movement. I have provided the code for how I move my characters and camera below.

Camera

transform.position = Vector3.SmoothDamp(transform.position, desiredPosition, ref moveVelocity, dampTime);

Character Controller

        Vector3 movement = new Vector3();
        movement.x = stateMachine.gameInputReader.movementValue1.x;
        movement.y = 0;
        movement.z = stateMachine.gameInputReader.movementValue1.y;
        character1controller.Move(movement * stateMachine.speed * deltaTime);
        if (movement != Vector3.zero)
        {
            character1.transform.rotation = Quaternion.Slerp(character1.transform.rotation, Quaternion.LookRotation(movement), 0.15f);
        }


        Vector3 movement2 = new Vector3();
        movement2.x = stateMachine.gameInputReader.movementValue2.x;
        movement2.y = 0;
        movement2.z = stateMachine.gameInputReader.movementValue2.y;
        character2controller.Move(movement2 * stateMachine.speed * deltaTime);
        if (movement2 != Vector3.zero)
        {
            character2.transform.rotation = Quaternion.Slerp(character2.transform.rotation, Quaternion.LookRotation(movement2), 0.15f);
        }
1 Like

The jittering issue could be due to a couple of reasons, most notably: frame-dependent movement and inconsistent updates. Your character controller movement seems to be frame rate dependent due to multiplying with deltaTime. If the frame rate fluctuates, it could result in jittery movements. Moreover, if updates for the character and the camera aren’t well synchronized, it could lead to a similar jittering effect.

Suggestions:

1. Frame-Independent Movement

Ensure your movements aren’t frame-dependent, especially if your game might be played on devices with different performance characteristics. To address this, you could apply fixed timestep for your physics-related actions.

2. Synchronized Updates

Like @MUG806 suggested in the comments, try to make sure your camera updates and character movements are synchronized. If you are calling Move() in the Update() function, it might be more suitable to move it to FixedUpdate() as it is more reliable when dealing with physics and movements.

You can try something like this (Warning: untested)

Camera:

 void FixedUpdate(){
     transform.position = Vector3.SmoothDamp(transform.position, desiredPosition, ref moveVelocity, dampTime);
 }

Character Controller:

 void FixedUpdate(){
     Vector3 movement = new Vector3();
     movement.x = stateMachine.gameInputReader.movementValue1.x;
     movement.y = 0;
     movement.z = stateMachine.gameInputReader.movementValue1.y;
     character1controller.Move(movement * stateMachine.speed * Time.fixedDeltaTime);
     if (movement != Vector3.zero)
     {
         character1.transform.rotation = Quaternion.Slerp(character1.transform.rotation, Quaternion.LookRotation(movement), 0.15f);
     }

     Vector3 movement2 = new Vector3();
     movement2.x = stateMachine.gameInputReader.movementValue2.x;
     movement2.y = 0;
     movement2.z = stateMachine.gameInputReader.movementValue2.y;
     character2controller.Move(movement2 * stateMachine.speed * Time.fixedDeltaTime);
     if (movement2 != Vector3.zero)
     {
         character2.transform.rotation = Quaternion.Slerp(character2.transform.rotation, Quaternion.LookRotation(movement2), 0.15f);
     }
 }

Try out these suggestions and let me know if you see any improvements or face any issues.