Camera Follow in Unity (Cinemachine)

// Forget the scripts, lets do this the easy way!Moving camera in actionWe’ve all read the massive number of tutorials out there on “How to make the camera follow the player?” The most common options are A) Attach the Camera to the Player and B) Write …

// Forget the scripts, lets do this the easy way!

Moving camera in action

We’ve all read the massive number of tutorials out there on “How to make the camera follow the player?” The most common options are A) Attach the Camera to the Player and B) Write a script to handle the movement of the camera based on the player. While both of these methods are perfectly fine ways to accomplish this task I am going to look at a third option using Cinemachine.

In previous articles, I used the Cinemachine virtual camera set up to create camera transitions and cinematics. Today I am going to use it in a much simpler way.

To get started we have to add the Cinemachine Package to the engine. We do this in Unity 2021.2 the following way:

  1. Click Window
  2. Click Package Manager
  3. Change Packages to Unity Registry
  4. Locate Cinemachine — in Unity 2021.2 this is version 2.8.0-pre1 (RC) your version may vary.
  5. Click Install

With Cinemachine installed we are ready to add our virtual camera to the scene that will be handling all of our camera logic. Again this may be a little different in Unity 2021.2 that the version you are using since the Cinemachine menu has been moved to GameObjects Menu. Click GameObjects > Cinemachine > Add Virtual Camera. Unity 2021.2 convieniently adds a Cinemachine Brain to our main camera but you may need to add this component manually in previous versions of the program.

If we click on the Virtual Camera and look at the inspector we will see the following options:

The first two we care about are Follow and Look At. We set the Player transform in both of those areas and if we play the game the camera will naturally follow the player. However you might notice that there is some strange behaviors. The camera flips when we face the other way, that might be cool if you are designing your game that way but for us it’s not what we want. Second you might notice that the camera kind of floats to the left or right as we move, again not exactly what we want for this platformer. Luckily we have options!

If you expand Body and Aim submenus you will see a plethora of new options for us to play with. We can get rid of the flipping camera by changing the Body > Binding Mode to ‘Lock To Target on Assign’. We can get rid of the drift effect by adjusting the Damping values. Inside of Aim we can toy around with the exact positioning of the camera.

That’s all there is to it! A couple of clicks and we have a fully customizable follow cam without having to write a single line of code.


Camera Follow in Unity (Cinemachine) was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
James West | Sciencx (2024-03-28T21:22:56+00:00) » Camera Follow in Unity (Cinemachine). Retrieved from https://www.scien.cx/2021/06/09/camera-follow-in-unity-cinemachine/.
MLA
" » Camera Follow in Unity (Cinemachine)." James West | Sciencx - Wednesday June 9, 2021, https://www.scien.cx/2021/06/09/camera-follow-in-unity-cinemachine/
HARVARD
James West | Sciencx Wednesday June 9, 2021 » Camera Follow in Unity (Cinemachine)., viewed 2024-03-28T21:22:56+00:00,<https://www.scien.cx/2021/06/09/camera-follow-in-unity-cinemachine/>
VANCOUVER
James West | Sciencx - » Camera Follow in Unity (Cinemachine). [Internet]. [Accessed 2024-03-28T21:22:56+00:00]. Available from: https://www.scien.cx/2021/06/09/camera-follow-in-unity-cinemachine/
CHICAGO
" » Camera Follow in Unity (Cinemachine)." James West | Sciencx - Accessed 2024-03-28T21:22:56+00:00. https://www.scien.cx/2021/06/09/camera-follow-in-unity-cinemachine/
IEEE
" » Camera Follow in Unity (Cinemachine)." James West | Sciencx [Online]. Available: https://www.scien.cx/2021/06/09/camera-follow-in-unity-cinemachine/. [Accessed: 2024-03-28T21:22:56+00:00]
rf:citation
» Camera Follow in Unity (Cinemachine) | James West | Sciencx | https://www.scien.cx/2021/06/09/camera-follow-in-unity-cinemachine/ | 2024-03-28T21:22:56+00:00
https://github.com/addpipe/simple-recorderjs-demo