2d Camera following player: shaking problem

Hi all,

I searched in all of question in this section about the shaking camera problem but I didn’t found something that can resolve my problem.

I’ve got this code attached to the player:
Vector3 v3 = this.transform.position;
v3.z = mainCamera.transform.position.z;

		if (Vector3.Distance(v3, this.transform.position) > deadZone)
			mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, v3, cameraSpeed * Time.deltaTime);

If cameraSpeed is = 1f all is perfect: smooth movement, stable.
If I put something like 10f, 20f, 50f the camera shakes. Even at the beginning of the test when the player is stopped (no movement taken) the camera shakes alot.

I don’t understand: what I’m missing?

Thanks in advance.

Y.

Do you understand what this line of code does?

Vector3.Lerp(mainCamera.transform.position, v3, cameraSpeed * Time.deltaTime);

Time.deltaTime is the number of seconds that the previous time step (frame, physics step, etc.) took. Depending on how fast your computer is, this number can be high or low, and it will fluctuate between time steps.

Vector3.Lerp(A, B, t) is essentially this code:

float t2 = Mathf.Clamp01(t);
return (1-t2)*A + t2*B;

where Clamp01(t) gives you 0 if t < 0, 1 if t > 1, and t if 0 < t < 1. You’re putting a fluctuating value in for your t value, which is the most likely cause for your shaking camera.

The following code should work better:

mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, v3, 0.1f);

I found the problem: the player model has got a RigidBody.
Without it the code works well