Adding a Player Follow Camera to the Platformer Prototype in Unity 2021

We need to change from a fixed camera perspective to a follow camera perspective so that we can accommodate levels wider than one screen.

To accomplish this, we’ll simply child the Main Camera game object to the Player game object.

This however isn’t working as intended.

This is because our movement code turns the Player game object, a Capsule primitive shape, 180 degrees when going to the left.

Due to the primitive nature of our Player’s game object, we didn’t really notice this until now.

We can make it easier to identify the “forward” face of our game object by adding a primitive cube and resizing it to represent the face of the player.

If we run the game while watching the scene view, we can see the camera is locked into a position that will change drastically when the player character game object rotates 180 degrees.

PlayerController Class / Script — MovePlayer() Method
We based our movement code on an example script provided in the Unity Documentation.

Since Unity has no idea what type of movement we desire, this sort of issue can crop up.

The fix is easy though, we simply delete the commented-out line above that is forcing the player character game object to always face forward.

Let’s also go ahead and move our Main camera back to its normal place in the hierarchy for now.

Unity acquired a product on the Asset Store known as Cinemachine a few years back.

I don’t know what they paid for it, but it was worth every penny!

For Unity’s reference documentation on Cinemachine, see the link above.

I’ve also written several articles on Cinemachine and its various features which you can view by scrolling through my Medium article list.

Those articles will show up starting around February 25th, 2022 on the list.

Cinemachine Virtual Cameras
Long story short, Cinemachine works by creating “virtual cameras” with their own settings, positions, and goals.

These virtual cameras take over the Main Camera when they are set to do so which is mostly determined by their “Priority” setting.

We can create a vanilla Virtual Camera by click GameObject > Cinemachine > Virtual Camera.

We can then rename the VCam in the hierarchy for easy identification.

Note that now the Main Camera game object has a red symbol overlapping the gear icon.
This indicates that the Main Camera now has the CinemachineBrain component attached to it.

This component is sort of the “mastermind” for controlling multiple virtual cameras and integrating it with the Main Camera.

Virtual Camera Settings
The first thing we’ll need to do is drag our Player game object into the “Follow” and “Look At” fields of the Virtual Camera’s Inspector settings.

We’ll then set Body to “Transposer” with a Binding Mode of “Lock To Target” and set the Follow Offset to “-20”.

Set Aim to “Hard Look At”.

Leave the rest as defaulted values.

If we run the game, we can see that our VCam is behaving as desired.

We can also fine tune the VCam later on but this will suffice for now.

In the next article, we’ll implement moving platforms for our game.

NOTE: If you are having issues with the double jump being infinite, please go back to this article and read the addendum posted at the end:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store