Making nice cameras in Unity with the Cinemachine plugin

Let’s see how we can use Unity’s Cinemachine package to easily create amazing game cameras!

This article is also available on Medium.

In a recent tutorial about behaviour trees, I showed an example of a little scene with a guard walking around, and a flying camera that keeps track of the character continuously:

To automate this camera follow, I relied on a great Unity plugin: Cinemachine!

But before we dive into the Cinemachine package, there is a question you might be asking yourself: how come we actually need to talk about game cameras? What’s so special, or complex, about those?

Creating a good game camera is difficult!

In a way, the first 2D games had it easy (like Mario, Pacman or Space Invaders for example): they usually used either a top-down or a side-view, and everyone was fine with it. That’s because, in this context, those views showed you all you needed to see in a clear and readable manner. Sure, you had to make sure that the camera followed the character, and you usually wanted to add some damping and delays to make for a smoother movement, but overall the logic was quite straight-forward.

When people started to dive into 3D and introduced that into video games, the industry was deeply transformed in many ways. And, among other things, it quickly forced the game designers to re-think the camera system. With this new dimension, the interaction to the medium was very different and you had to show a lot more to the player at once.

Our human eye is naturally tuned to understand depth and react to a 3D environment; however, with video games (just like with movies), you’re still printing the image on a 2D screen in the end. This means that you need to find tricks to give the players a comprehensive view of the virtual 3D world through a 2D render.

This issue did not appear with video games, of course – long before that, we’d already had to devise techniques to represent 3D shapes and spaces on a sheet of paper. Those are called “graphical projections” and they can be further grouped into various categories depending on how they approximate reality. Do you try and keep the proportions, or rather decompose the object as much as possible to clearly identify all of its parts? Do you use vanishing points? Do you use isometry?

All of these variations result in numerous graphical projection types:

Depending on the context, one projection might be more fitting than another; for example:

  • most strategy games use axonometric projections to better show the units on the map
  • whereas FPS or RPGs that want to immerse the player in the story use perspective, to try and mimic the real world the best they can
  • lots of 2D platformers use an orthographic side-view to make the level geometry clear to the player

But no matter the type of projection you choose, there is still an additional level of complexity you’ll have to deal with: movement! Video games are an interactive medium, and the player expects to be able to move around in this virtual world of yours 😉

You want the camera to show the players what they need to see every step of the way, and keep them hooked in the ambiance; you want the camera to move smoothly but quickly enough to adapt to the player suddenly jumping out of the building; you want the camera to feel grandiose and cinematic, but also to be a tool to support the player’s progression through the game’s world.

That’s a lot to ask, and, in truth, that’s why it’s near impossible to create a perfect camera in a video game that will work in every situation. But we can still rely on some proven techniques to create okay camera angles…

… and if you’re working with the Unity game engine, then you definitely need to hear about the Cinemachine package! 🙂

What’s Cinemachine? (and how to install it)

As stated in the Unity official docs:

[…] Cinemachine is a suite of tools for dynamic, smart, codeless cameras that let the best shots emerge based on scene composition and interaction, allowing you to tune, iterate, experiment and create camera behaviors in real-time.

Created by the Unity team itself, this free package allows you to quickly and easily create cameras for your games or films; it abstracts a lot of complexity and makes it pretty straight-forward to have the camera follow objects in the scene, translate as if attached to a dolly, rotate along with the player, etc.

Actually, it is so useful that lots of tutorials have pointed out it’s kind of weird that it is doesn’t come with Unity by default! But anyway, Cinemachine is fairly easy to install, thanks to the package manager…

To add this plugin to your project, just open the package manager window and look for “Cinemachine” in the Unity registry; then click “Install”:

Once it’s installed, you’ll have access to new objects in a brand new “Cinemachine” menu:

The big idea of Cinemachine is that it relies on a second “virtual” camera that controls the actual real Unity camera. The “Create Virtual Camera” option lets you create a basic virtual camera that you can then configure in various ways. The other options are pre-configured Cinemachine cameras.

But no matter the object you create in your scene, this object will have a CinemachineVirtualCamera component, and your camera will have a CinemachineBrain component added automatically:

This will allow the virtual camera to dynamically compute the position and rotation of your Unity camera, depending on its configuration.

Configuring a basic Cinemachine virtual camera

Note: if you want more detailed information on all of this, I really encourage you to check out Unity’s official series of tutorials on Cinemachine 🙂

If you want to configure your own Cinemachine camera, you’ll see that there are a lot of options you can set! Two important ones though are the “Follow” and “Look At” fields.

Tracking a target across the scene

By using the “Follow” field, you can have your camera track a specific transform in the scene and translate parallel to it, as if it was on rails next to the target object.

You’ll need to pick a specific body type to actually enable the camera to follow the target. For example, here I’ve used a Framing Transposer that targets the cube in the middle of the scene, and the virtual camera has a slight rotation along the X axis:

As you can see, the package’s script automatically computes the position of the real camera based on the parameters of the Cinemachine virtual camera… and the really cool thing is that it works directly if you start moving the cube!

By default, there will even be some damping on the tracking to get a smoother follow movement 🙂

Rotating around a target

The “LookAt” field, on the other hand, is useful if you want your camera to stay put at one position but rotate to keep the object in view.

Here is the same scene but using the “LookAt” field instead of the “Follow” one:

Of course, you can then dive into the “Aim” options to better decide how your camera keeps track of the target; here, you can choose the dead zone, the soft zone, an optional offset…

Diving deeper…

Cinemachine is a huge toolbox and to be honest, at first I was quite lost with all the parameters. But after trying out the different body types, and aim options, I eventually got a bit more apt with this amazing package.

From the “Orbital Transposer” that lets you rotate the camera around an object with the mouse to the “3rd Person Follow” that makes RPG cameras a breeze, or even the “Tracked Dolly” for point-and-click games, the package comes with about everything you might need 😉

Compositing shots

As its name implies, Cinemachine is of course very powerful for cinematic shots, too! It’s one thing to adapt dynamically to the position of a transform, but what if you have some pre-determined animation in your scene and you want to keep it in the frame of your camera?

In this section, we’re going to use the Timeline window to create sequences. It can be opened by going to Window > Sequencing > Timeline.

In this window, you’ll be able to create a Timeline asset for the currently selected object, in which you can then add one or more tracks. I won’t dive into the details of using timelines in Unity in this tutorial, but if you want more info, you can always take a look at the docs

Note: and by the way: if you’re interested in me writing a tutorial about Unity timeline basics, don’t hesitate to share it in the comments! 😉

Here is a basic timeline for my little guard character with two animation tracks (in blue), and one Cinemachine track (in red). The two animation tracks determine both the transform of the character and the animation it is currently playing (walking or idle). The Cinemachine track allows me to create a nice sequence of shots.

You see that, in the left column, where I have my list of tracks, I’ve assigned my main camera (more precisely its Cinemachine Brain component) to the Cinemachine track slot:

Then, I’ve created two Cinemachine shots and stacked them in the Timeline track:

When you create a Cinemachine shot, you need to give it a virtual camera; you can either pick an existing one or create a brand new virtual camera:

Finally, all that’s left to do is configure your Cinemachine virtual camera(s) as we’ve seen before, and you’ll get a nice sequence of shots! You can even drag one shot over the other to automatically create a fade between the two, and have the view lerp from one virtual camera to the other:

A really cool feature of the Timeline is that it works in preview mode, in the editor – so you don’t even have to run the game to see everything play before your eyes! And tadaa: my guard is now walking around and the camera first looks at it, and then follows it 🙂

Conclusion

The Cinemachine package is an amazing, free and easy-to-install plugin that can be added to any Unity project quickly thanks to the package manager. It is very powerful and abstracts away a lot of complexity… but it still requires a bit of getting used to 😉

Once you’ve started to dive into the tool, though, it will take care of all the annoying deep-level stuff and abstracts away lots of the complexity so that you can focus on creating the best game cameras and cinematic shots for your project!

I hope you enjoyed this quick Unity tutorial and, as usual, feel free to react in the comments and tell me if you have other Unity features you’re curious about…

Leave a Reply

Your email address will not be published.