In this very short Unity 2D programming tutorial, I will show you how to make the camera follow the main character with a slight delay instead of instantly, so you can create smoother transitions.



Video Transcript

Hello everyone! In this very short Unity 2D programming tutorial, I will show you how to make the camera follow the main character with a slight delay instead of instantly, so you can create smoother transitions.

This tutorial is intended for those who are already familiar with the concept of Delta Time in Unity and, possibly, with interpolations such as Slerp. I have already covered these concepts in other basic tutorials, which you can find as videos in English on the channel and as written versions, in several languages, on my website.

In the example I will show, I will write the code for movement along the X axis for simplicity, but it can easily be extended to vertical movement along the Y axis as well.

Let’s start from the base script, which constrains the camera position to the position of the character to follow. The script, here called Camera Controller, immediately includes a private variable of type Transform that must be assigned in the editor to the object to follow, so its position can be retrieved.

In the Update function, we have a variable of type float that retrieves the X coordinate of the player’s position and is used in the camera position in place of the camera’s own X position, in a value called Camera X, so the camera follows the character instantly.

The first variation we try uses Lerp, so I introduce a private variable called Follow Time, of type float, which can be adjusted in the editor for tuning. In the Update function, I use option number 2 to define the camera’s X coordinate, which is now given by the result of the Lerp function.

Lerp is a native Unity function that performs a linear interpolation between the object’s current position, given by Transform Position, and the target position, given by Player Position. The interpolation parameter is represented by the ratio between Delta Time and the Follow Time variable, which allows you to control the speed at which the camera progressively approaches the target position and makes it easy to adjust this behavior during execution in the editor as well.

With the Lerp function, at each frame the camera updates its position, progressively moving closer to the target. In this way, the camera movement appears less abrupt compared to an instant displacement. However, Lerp performs a linear interpolation without damping the speed near the destination, and this can make the movement less natural in the final phase of the approach. To obtain a smoother transition, the Smooth Damp method comes into play.

I then make two new variables available, called Smooth Time and Velocity. In particular, Smooth Time is a private float that is used to tune the interpolation, just like Follow Time in the case of Lerp. The Velocity variable is instead a float passed to the Smooth Damp method to store the current velocity during execution.

The native Smooth Damp method allows us to implement a less abrupt, damped movement, where the speed decreases as the camera approaches the destination position. The Velocity variable represents the camera’s current speed along the axis being considered. Velocity is passed to the method by reference and is automatically updated at each frame during the interpolation.

In the code, the camera’s X coordinate is now the result of an interpolation performed by Smooth Damp, conceptually similar to Lerp because it interpolates between two values, but producing a smoother transition and, in the method signature, also using the float variable Velocity.

Smooth Damp can be seen as an evolution of Lerp that introduces progressive speed damping, making the movement more natural.

Well, that’s all for this video! If you found it helpful, you can thank me with a Like and by subscribing to the channel. See you soon!

Search Tutorials

Enter a phrase or multiple comma-separated keywords to search.

This is a personal website and does not constitute a sales channel. The pages dedicated to 3D models document a selection of my work and do not contain offers, prices, purchasing procedures, or commercial links. I am not currently accepting requests for custom work, consulting, or other professional collaborations.


EXTENDED PRIVACY AND COOKIE USAGE POLICY