An Intro to Cinemachine for Programmers


I’d first heard about Cinemachine in passing when it was released for free for all users of Unity. “Oh, cool,” I thought. “I’ve never done cinematics before in Unity, I should give this a shot.” Unfortunately, after watching a couple of tutorials, I found myself confused about what Cinemachine really was. I ended up getting the impression that it was for film-makers exclusively - entirely out of the realm of my comprehension.

I bet I am not the only one with these misconceptions. I believe that the tutorials and other official material out there presents Cinemachine in… sort of a misleading way, unintentionally. At the very least, the target audience for their tutorials is not programmers.

I think it’s a shame that Cinemachine is not more immediately understandable, because it turns out to be an incredibly powerful drop-in tool once you do understand it. in this post, I’ll give a brief overview of how I’ve found it useful, as a programmer, to think about Cinemachine.

A short detour

Let’s forget about Cinemachine for now and imagine you’re putting together a system like that displayed in the above gif.

When the character activates a level select board, the camera switches from a third-person view to a view of the currently highlighted board. When they cancel out, the camera switches back to the third person view.

If I were to start putting together a system like this, here’s what I’d do:

  1. Create an empty gameobject (A) which faces the board.
  2. Create an empty gameobject (B) which follows the player around.
  3. Create a script, attached to the Unity camera, which has the following logic: a. If the board is activated, interpolate to the position and rotation of A. b. Otherwise, interpolate to the position and rotation of B.

Guess what - we’ve just recreated the primary use case of Cinemachine.

Wait, What?

Yeah, I’m not kidding. Cinemachine just positions cameras.

In Cinemachine terms, those empty gameobjects which we use to represent possible camera locations are given the CinemachineVirtualCamera script, and the script attached to the unity camera which interpolates between those virtual cameras is called the CinemachineBrain. The Brain makes the camera follow whichever VirtualCamera is active.

To effect the transition like we do above, all we need to do is deactivate the VirtualCamera which follows the player around, and activate the VirtualCamera which is attached to the board. The Brain handles the interpolation itself. It’s as simple as that.

At first, when I started using Cinemachine, I was confused about the role of the normal Unity Camera in all this. The general rule of thumb that I’ve been following is this: if your scripts affect rendering, then attach them to the real Camera. If they affect a camera’s position, always put them on a VirtualCamera.

For example, all ImageEffects go on the real Camera. However, your third-person follow script goes on a VirtualCamera, because the CinemachineBrain is controlling the position of the real Camera, and will tell it to follow that VirtualCamera when appropriate.

If it’s that simple, why use it at all?

  1. Ease of use. It’s drop-in, and you barely have to mess around with any code. It really does just work.
  2. Great-looking defaults. A simple linear interpolation is not usually what you want when dealing with cameras, and you can fiddle with the math for days trying to get a good result. Cinemachine’s defaults automatically give the camera that undefinable good feeling.
  3. Timeline support. This is covered in the next section.

There’s also a ton of options which I haven’t touched yet. For example, they have their own camera follow setup that you can use.

So where do cinematics come in?

A lot of amateur Unity developers (like me) have never messed around with Unity’s built-in Timeline tools. When we think cinematics, Timeline is actually the tool to use - not Cinemachine. It allows us to interpolate objects’ attributes, like position or alpha, over time.

Unfortunately, controlling a camera in vanilla Unity Timeline is a bit finnicky. You have to mess around with activation tracks for hard cuts cameras. Moving a camera as a transition is not too bad - that’s what Timeline is for, after all - but I find adjusting the transition after the fact to be a bit clunky.

Cinemachine provides an extremely intuitive and friendly interface into the timeline for adjusting all this using VirtualCameras. For this, I’ll defer to their tutorials, because this is something they cover extensively.

Conclusion

I’ve ignored some of the higher-level functionality of Cinemachine, such as the procedural camera system. That was on purpose; I feel that the simplicity of the underlying systems is something the creators overlook in their tutorial videos.

Once I understood how Cinemachine worked, my view of how cameras should be controlled radically changed. I think Cinemachine will be in many of my projects going forward. It’s a great addition to the Unity toolset.