Camera Zoom Depending on Object Speed

Hi community, I recently have stumbled upon a problem which I cant seem to find a solution to. Namely, the game I create is an endless runner with perspective camera, and I want the camera to zoom out slightly when the player is using a booster. I only managed to get a camera thats constantly zooming out with this code, how to make it stop zooming on specific position depending on player speed once the player reaches his max speed? The camera transition has to be smooth and manupulating with camera FOV doesn`t count, it would create some unwanted behavior in rendering, for my camera has some rotation on every axis.

You can try using Vector3.Lerp(), Mathf.InverseLerp(), Mathf.SmoothStep().

float t = Mathf.InverseLerp( normalSpeed, boostingSpeedMax, currentSpeed ); // returns a value between 0-1.
t = Mathf.SmoothStep( 0, 1, t ); // smooth out the t value so that the camera will ease in and out nicely.
Vector3 cameraPosition = Vector3.Lerp( nearPosition, farPosition, t ); // blend between these two points

Look up these functions in the Unity Reference to get a better understanding of what they do.

Thank you very much, linear interpolation was the solution I’ve been trying to find

X

can you share the zoom out code as I’m creating a toptodown racing game i want the camera to zoom out according to the players speed

Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

Please look at the dates of posts. This was posted nearly 7 years ago and it was the devs last post.