In my Unity 2D platformer, I'm trying to move the camera down a bit when the character is crouching but not moving. I've done this with the following:
cameraPosition = mainCamera.transform.position.y;
cameraPosition = Mathf.Lerp(cameraPosition, cameraPosition - 0.8f, 3 * Time.deltaTime);
mainCamera.transform.position = new Vector3(mainCamera.transform.position.x, cameraPosition, mainCamera.transform.position.z);
This works, but it's causing the camera's y value to decrease continuously rather than just 0.8f, because cameraPosition
is assigned a new value every frame. But cameraPosition
has to be relative because its location depends on the player's location, so I can't figure out how else to write it.
Lerp(A,B,C)
returns the interpolated value placed at(C*100)
percents between A and B (in that order) If you use what you wrote every frame this is what happen : CameraPosition gets incremented, and then that incremented value is passed (in the next frame) as parameter to the next frame incrementation. Lets admit that instead of Time.time you used 1, you would get :CameraPosition = CameraPosition +0.8F;
every frame (Looks familiar?) Whatever the value of C becauseA = Lerp(A,A+x,C)
, A always increase \$\endgroup\$