Devblog #3: Scripting A Dynamic Camera Based On Player’s Speed

tausta

One way to make a game feel more alive and responsive – especially a side-scroller like our game – is through the camera, in particular how it reacts to the player’s movements. In addition to the camera following the player in a certain way (more on that in another blogpost), we wanted the camera’s size to be relative to the player’s speed. The idea is that when the player is moving faster, the camera’s size increases to show more of the level and vice versa. Implementing this turned out to be a pretty easy task, after some quick brainstorming.

Here’s the script:

We first clamp the velocity value coming from the rigidbody by using the Mathf.Clamp method so it doesn’t go over or under the given values (here they are 10 and 18). This will give us the target value of the camera’s size. Then we use Mathf.SmoothDamp method to smoothen the transition from the camera’s current size to the target size at any given time. The first parameter is the camera’s current size, second is the target size so we use the CameraSizeInput variable for that, the third is a velocity value that the method modifies every frame, and the fourth is a smoothing value; a lower value will reach the given target faster so it’s useful to put this in a public variable and test what value is suitable by changing it and testing it in the inspector. Then we just set the camera.orthographicSize to the cameraSizeChanger.

And now the camera’s size is in relation to the player’s speed.

veee

<