Introduction

In the previous chapters we implemented a fishing game where we controlled our fishing boat to catch fish and avoid collisions with solid ground objects. During the game implementation we introduced some code to make Main Camera follow the coordinates of our boat so we would be able to see where the boat is headed. However, this is not the best and the optimal way of doing things. Instead, we would like to introduce you to Cinemachine. It is a Unity library that allows us to easily configure cameras and scope of lens without adding any code logic.

Displays Unity runtime game execution were fishing boat rides along the map while camera follows, so boat is always on the middle of the screen.
Fishing boat followed by camera

Package Manager

The first thing we need to do is add Cinemachine library to our project. We add different libraries to Unity project by using package manager. We can navigate to it from the menu: Window > Package Manager.

Displays Unity toolbar menu. Under Window indicating Package Manager menu item.
Access Package Manager from toolbar menu

Once inside Package Manager select Unity Registry and search for Cinemachine. In library details click Install.

Displays Package Manager window. Under Unity Registry a search term is entered 'cinemach'. In search results it is listed a Cinemachine library.
Search Unity Registry for Cinemachine library

Once installed the library should be added to other dependencies which you can check in Project explorer under Packages.

Displays Project explorer. Under Packages folder there is Cinemachine library among other dependencies.
Project explorer and Packages folder

Camera

Now we can add Cinemachine camera to our game. To do that do right click in scene Hierarchy and go to Cinemachine > Cinemachine Camera. In your game you will now have MainCamera which will be enriched with Cinemachine Brain component (see in Properties). This means the MainCamera from Unity will be controller for all virtual cameras in the scene. If you select newly added Cinemachine camera, you’ll discover its properties in Inspect tab.

Displays Scene menu under right mouse click, with Cinemachine menu option. Under Cinemachine option the chosen item is Cinemachine Camera that is being added.
Adding Cinemachine Camera

The important properties are:

  • Lens - this will allow you to define the proper value for camera scope that you need for your object,
  • Tracking Target - your tracking object from the game, in our case it will be the boat that player controls,
  • Position Control - this determines how do you wish your camera to track the moving target.
Displays Inspect tab with Cinemachine Camera properties, highlighting Lens, Tracking Target and Position Control.
Properties of Cinemachine Camera

Once these properties are set, you’ll enable camera tracking for your game.

Conclusion

Cinemachine allows us to effortlessly add camera support to our game. By assigning the Tracking Target, the camera will follow the controlled object regardless of what part of screen is visited. For the more advanced scenarios we recommend researching the official documentation of Cinemachine package.

References