[SOLVED]Camera jitter as soon as using Lerp

Hello everyone !

I checked a lot of existing posts, tried a lot of things but could not figure it out.

I am moving an object using character controller, my camera is following it and looking at it that way :

transform.position = player.position;
Quaternion lookRot = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = lookRot;

This is in late update, and I am moving my object in Update.

This is working fine, but as soon as I am trying to smooth out the movement using a Lerp, camera begins to jitter.
From what I understood it seems it is related to the Lerp function ( OR that we should not be using a point that is moving as the first parameter ? ), and since I am pretty new to dealing to camera movement, I am not sure how to do things right to fix it correctly without surcharging and adding non necessary stuff…

I tried using the lerp like this :

Vector3 smoothMov = Vector3.Lerp(transform.position, position, Time.deltaTime * 30f);
            transform.position = smoothMov;

I could try to use SmoothDamp, but I also saw that it was more processing time consuming than a Lerp

Thanks !

Hi!

Try using a value like 0.1f on the alpha parameter of the Lerp instead using Time.deltaTime * 30

Hope it helps, cheers!

Yeah your use of “Time.deltaTime * 30f” is a problem. It should be a percentage of the full movement expressed as a value between 0f and 1f.

Generally you’d have two points you are moving between over several frames as you increase that percent value each frame. Though changing those points every frame like it looks like you’re doing is going to make it difficult to smoothly lerp between them.

1 Like

ooh I see…

So basicly we should never use Lerp for camera interpolation, but rather smoothDamp ? because even if I am having a value going from 0 to 1, the initial position we are initiating the interpolation changeanyway, so it’s absolutely useless in that situation

I saw this in many tutorials being used :S

1 Like

Is your camera following an object that is moved with physics? If so, that is the source of the problem, and the camera should run in FixedUpdate. See last section here.

Hello !

No I am not using any rigidbodies or physics, just a character controller

I just did some tests with smoothDamp, does not looks like a viable alternative than Lerp, still doing some weird jitters somehow

5077961--499490--upload_2019-10-17_10-26-30.png

You’re using physics.

:open_mouth: but I thought FixedUpdate was only when using a rigidbody component and actually applying physics forces to make the object move :open_mouth:

Well try doing it in FixedUpdate (it’s a 5 second fix) and see if it works.

Putting the camera in Fixedupdate() and also the movement of the object in Fixedupdate(), seems it fixed the jitter indeed… I do need the object movement also in Fixedupdate right ?

In any way thanks a lot for the explanation, that makes more sense

FixedUpdate function is used to do something on the physics update. You can do whatever logic you want in there, it doesn’t have to be specifically physics operations with a rigidbody. But since we know that the character works with physics, then we also want to move the camera to face the character every time the physics system updates.

https://docs.unity3d.com/Manual/class-CharacterController.html

This means that the character controller does not use a Rigidbody to move, but still uses the physics system to constrain it and collide with objects.

You can move your CharacterController in Update with no problems at all if you are not moving it using forces but using his Move or SimpleMove functions.

Yes, you can use Lerp for camera interpolated movement. If you use your current position as start position of the lerp and a constant alpha value (like 0.1f) what you get is a smoothed movement with ease out. Thats because you are moving your camera to the target position a 10% of the distance on each call. So as you get closer to the target, that 10% becomes a smaller value resulting in that “ease out” curve.

So, if you move your character in Update (dont forget multiplying your speed value * Time.deltaTime!) and your camera in LateUpdate everything must look fine :slight_smile:

Hope it helps, ask me if you still get problems!

Just solved it by changing Update() to FixedUpdate(), and Time.deltatime to Time.fixedDeltatime

1 Like

Some other possible fixes include:

  1. Changing the CinemachineBrain’s update method from “SmartUpdate” to “FixedUpdate.”

  2. Changing the Damping settings on the CinemachineVirtualCamera.