Jittering rotation with Virtual Camera

Hi,

I’m using Unity 2023.2.18f1 with Cinemachine 2.9.7

I have a 3rd person controller with a character controller. Everything is set to FixedUpate but the camera rotation is jittering massively.

My Player Animator Setting:

My cinemachine brain:

Camera settings:

My camera script:

private void FixedUpdate()
        {
            // character aiming
            bool isAiming = Input.GetMouseButton(1);
            animator.SetBool(isAimingParam, isAiming);
      
            directRotation();
       }
        void directRotation()
        {
            x = Input.GetAxis("Mouse X") ;
            // invert Y Axis
            if(yAxis.m_InvertInput)
            {
                y += Input.GetAxis("Mouse Y") ;
            } else
            {
                y -= Input.GetAxis("Mouse Y") ;
            }
            y = 0;
    
            y = Mathf.Clamp(y, minYRotation, maxYRotation);
        
            cameraLookAt.localEulerAngles = new Vector3(y * turnSpeed * Time.fixedDeltaTime, x * turnSpeed * Time.fixedDeltaTime, 0);
            float yawCamera = mainCamera.transform.rotation.eulerAngles.y;
            if (!playerScript.dead)
            {
                transform.eulerAngles = new Vector3(x, yawCamera, 0);
            }
        }

cameraLookAt is a child of my player and it’s set as follow and look at in the camera.

Before I wrote this script I had a camera rotation with a Quaternion.Slerp for the player transform rotation, but then I had this kind of rubberband effect that I don’t want.

I like to move the mouse to rotate the view and the character for aiming. The character has a strafe animation.

Any idea about the problem?

thanks in advance
Frank

You should not use Input from FixedUpdate. You may be losing input that way because Input updates run with Update. From what I can tell it looks like movement isn’t smooth either.

The same goes for Camera updates. FixedUpdate camera mode exists for cases where it is essential for the camera to be updated at a constant speed - to be in sync with fixed update game logic and/or physics. Ever since Cinemachine existed I always wondered about the kind of scenario where FixedUpdate for the Camera makes sense. My guess is this only makes sense where the player isn’t directly controlling the camera movement and/or look at.

In all other cases (yours) use Update because your camera movement is not tied to the gameplay, in fact you want the camera to update the same frequency as the Input because the player is directly controlling the camera via mouselook. So this needs to be as responsive as possible. Therefore use Update().

Note that Input axis will always be jittery to some extend. You may be getting a sequence of 2,3,2,3,2,3,1,3,2,1,2,2,2 values where the jumps between 1 and 3 (pixels) in a relatively constant but slow mouse movement differ by factors 2 and 3. If you multiply that by 10 because otherwise the mouse aim would be far too slow the effect is compounded and you get movement that jumps between 10, 20, 30 pixels - enough to be noticeable. Most games will implement some form of mouse input smoothing to combat this.

Your Quaternion.Slerp felt bad because it’s the wrong kind of smoothing. I bet Lerp might feel a little better but both have the problem of interpolating towards a target, thus lagging (adding a delay to the movement). You also have movement (body) damping enabled which may compound the effect.

Anyhow, I’m actually surprised that you rotate the camera manually. And with euler angles no less, which always spells trouble to me - although you clamp them, but by doing so you introduce a brutal STOP to the mouselook.
You should instead set the Aim property to POV. Not only will the mouselook just happen to work (provided a CinemachineInputProvider exists). You also get additional properties to tweak the mouselook behaviour including the ability to invert mouselook for all those imbeciles who prefer to mouselook “the wrong way”. :wink:

Lastly, although it looks like the camera is already avoiding collisions I still want to mention the Cinemachine3rdPersonAim extension for that extra feel of triple-A camera quality - in case you were also implementing this collision avoidance yourself and aren’t already using this extension.

1 Like

thanks I will try to change to update.

Careful! If you change the camera to LateUpdate, then you will have to enable Interpolation on the objects that the camera is tracking (i.e. the player) otherwise it will jitter relative to the camera. That also implies running the animator in Update, not FixedUpdate.

In general, I always encourage people to run the camera in LateUpdate - it gives the smoothest results. But the catch is that you have to be diving your physics objects properly - with Interpolation. FixedUpdate is only there in the brain to support people who for whatever reason just can’t get Interpolation running properly.

If you’re using 3rdPerson in Body, you should not use POV in Aim.

Have you seen this tutorial?