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


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.


  2 comments for “Devblog #3: Scripting A Dynamic Camera Based On Player’s Speed

  1. Gene
    November 8, 2014 at 1:55 am

    I like your camera! Will you please do a tutorial on something like a wall jump and wall slide… there’s not much online with setting those up!

    • Toni
      November 9, 2014 at 8:19 pm

      Hi Gene! Glad you liked it :) We might do a tutorial on those mechanics as they are interesting and would be a cool challenge to implement, stay tuned!

Leave a Reply

Your email address will not be published. Required fields are marked *

four − 1 =