Platformer: Cinemachine Camera Follow
In this article, I will cover how to get the camera to follow the player using a package by Unity called Cinemachine.
Unity has a great package called Cinemachine that allows for more dynamic cameras and we will be using this to implement our camera follow the player. In downloading the package, select Window in the toolbar and click on Package Manager. Search of Cinemachine and install.
Once installed Cinemachine will appear in the tool bar and we are going to create a 2D camera.
A CM vcam1 is created and we will parent it to the Main Camera. For the Main Camera we want the projection to be Perspective to keep our 2.5D feel, but if you were doing a 2D you would want to choose Orthographic.
In the Cinemachine camera, we have the option to insert a Transform for the camera to follow. We can simply drag the Player game object and we have our camera following the player.
In the Body settings of the Cinemachine camera, we are going to 0 out the Damping and Dead Zones.
This is the most basic way of setting up the camera to follow the player. It is a bit snappy, especially when we move left and right.
The Camera Distance will determine how far from the game object the camera will be placed. The screen Y will determine the position of the camera on the Y-axis and the screen X will determine the position of the camera on the X-axis.
The Dead Zone width determines how far the player can move before the camera will start to follow.
You can see the greater the Dead Zone width, the more the player can move left and right before the camera starts to follow.
The Dead Zone Height is determines the how much the Player can move up and down before having the camera will start to follow.
The higher Dead Zone height the less the camera moves when jumping.
The Damping will help with smoothing when the camera moves in a certain direction.
You can see the yellow tracking dot and how it is allowing the player to move a certain distance outside of the Dead Zone. The larger the Damping the more the player can move outside of the Dead Zone before it starts to move.
The Lookahead Time allows the camera to pan a certain amount in the direction the player is moving, so they can see further ahead. The Lookahead Smoothing will smooth out this transition. The Lookahead Ignore Y when set true, will ignore the Lookahead camera movement on the Y and will only perform on the X.
You can see when the Lookahead Time is set to 1, we camera will pan forward to allow the player to see ahead. When the player jumps the camera moves wildly, so we can increase the smoothing to have the movement look more fluid.
There are a lot of settings and options with Cinemachine that can make the camera movement more dynamics, but this should give you enough to get started with your typical platformer.