Have you been working on a camera system for your 2D game for ages and wish there was something like Cinemachine for 2D? Not many people know about it, but there already is! This blog post gives you some tips for getting the best out of Cinemachine, and how this tool can benefit and speed up the development of your 2D game significantly. Keep reading to find out more about Cinemachine Virtual Cameras, Confiners and more, specifically for use in 2D games.
You can get Cinemachine from our Package Manager within Unity if you are using any version greater than 2018.1.0b7.
This can be done by going to Window > Package Manager > All and then selecting Cinemachine.
With Cinemachine, it’s relatively easy to start creating your camera system for a 2D environment. Let’s take a look at creating a Virtual Camera for 2D.
The main thing you will notice between a 2D Virtual Camera and a 3D Virtual Camera is the fact that we are using a Framing Transposer. This special transposer will follow a target on the camera’s X-Y plane, and stop the camera from rotating. For the framing transposer to work correctly we need to ensure that the Virtual Cameras ‘LookAt target’ is null. Another thing to note is that 2D games use an Orthographic View, when first creating your Virtual Camera you will need to change the projection to Orthographic on the Cinemachine Brain Camera of your Scene.
An important thing to remember is that with Cinemachine you shouldn’t try to make one camera do everything. Instead, you can have different ‘Virtual Cameras’ around your scene and blend between them using the Cinemachine Brain. This blending can occur if the player is low health, the player has entered a certain area or any other scenario you can imagine that requires a change of Camera framing or Post Processing. You can adjust the blend settings on the Cinemachine Brain depending on how you want the visuals to be.
Check out the video below to see how you can adjust your Virtual Camera during Play Mode.
Another cool thing we can do with Cinemachine is to use a boundary box to confine the Virtual Camera to a certain area. This feature is available in the extension section of the Virtual Camera. Below are the steps to create this effect.
See the results of this below:
Video Example:
Another cool feature from Cinemachine that we can use in 2D is the Group Camera. This camera allows us to create a target group for our camera to look at. A Group Camera is useful if you’re doing a cutscene where you want to show something of importance, you want to keep more than one object in each frame, or if you want to create a local multiplayer game in 2D.
In Cinemachine we can do this the following way:
A ‘Target Group’ is a Cinemachine component that allows you to view multiple targets on the same Virtual Camera. It adjusts the camera’s size to ensure that the targets are visible depending on the weight. For example, if all of the targets have a weight of 1, the target group will ensure that all targets are visible.
Here’s an example of a Group Camera that focuses on the player and a chest.
With Cinemachine V2.2, we’ve added in a new Impulse Extension. This extension allows users to create camera shake effects without creating any code. Impulse Extension can be added as an extension to your Virtual Camera. I’m going to walk you through how to add this to your scene:
If we wanted to we could adjust the Dissipation Mode to change the decay type. There are other settings we can adjust, but for now, that’s all we need.
You can see the result below:
So, to summarize, we now know how to:
I hope you enjoyed this blog post and now feel ready to use Cinemachine in your next 2D project! If you want to keep up to date with Cinemachine? Join in the discussion on our forum.