Cinemachine for 2D: Tips and Tricks
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.
Creating a Camera for 2D
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.
- Create a 2D Virtual Camera by going to the menu bar and selecting Cinemachine > Create 2D Camera. This will create a Virtual Camera set up for a 2D environment. If it’s your first Virtual Camera in the scene, it will also add a Cinemachine Brain Component to your Main Camera.
- Drag your player from the hierarchy to the Follow target.
- Make sure that nothing is in the LookAt Target, if you have something there, select it and press backspace or delete to remove the reference.
- Adjust the Orthographic Size and Body settings to suit your needs.
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.
Tip: You can adjust the camera settings while in Play Mode and they will save as long as you have “Save During Play” checked on the Virtual Camera
Confining a Camera
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.
- Set up a boundary box for our level. This will be used to confine the level. This is done by:
- Create an empty ‘GameObject’.
- Add a ‘CompositeCollider2D’ to the GameObject.
- Set the ‘CompositeCollider2D’s Geometry Type to Polygons.
- Set the ‘Rigidbody’ on the GameObject to Static.
- Add a ‘BoxCollider2D’ to the GameObject.
- Adjust the ‘BoxCollider2D’ to fit your level.
- Set the ‘BoxCollider2D’ to be “Used by Composite”.
- Add the Confiner Extension to your Virtual Camera.
- Drag in the boundary box we created earlier, to the Bounding Shape 2D Box.
- Decide if you want the Camera to Confine to the Screen Edges, this can be adjusted with the “Confine Screen Edges” checkbox.
- Finally, we can decide if we want the Confiner to have damping. This will allow the camera to smoothly overlap with the edge of the confining collider. If you don’t want this effect – set the Damping Time to 0.
See the results of this below:
Tip: When using the “Confine Screen Edges” checkbox the bounding area needs to be bigger than the orthographic size of the Virtual Camera. If the bounding box is smaller than the screen, the confiner will not behave correctly in that area.
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:
- Create a new 2D Virtual Camera
- Create a new GameObject in the scene
- Add Component > Cinemachine > ‘Cinemachine Target Group’
- Add the GameObjects that you want to focus on to the Target Group
- Drag the target group to the Virtual Cameras ‘Follow Target’ property in the Inspector.
- Adjust minimum and maximum Orthographic Size – which in camera terms defines the ‘zoom’ of the camera – to your preferred setting
- Determine the type of Group Framing you want. ‘Horizontal’ only considers the horizontal frame dimension when framing the camera. ‘Vertical’ only considers the vertical frame dimension when framing. ‘Horizontal and Vertical’ type takes both dimensions into account.
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.
Tip: When adding or removing group members while the Group Virtual Camera is live, lerp the member’s weight to and from zero. This will allow the Camera to reframe smoothly.
Camera Shake using Impulse
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:
- Create a 2D Virtual Camera using the Cinemachine menu option and set it up to follow our target.
- Click Add Extension > Cinemachine Impulse Listener.
- On the Impulse Listener, ensure the checkbox named ‘Use 2D Distance’ is checked.
- The Channel Mask allows you to filter the impulses you want to listen to. In this example, we’re going to use the default channel.
- Select the GameObject to send the Impulse from, in this example we’re going to use a Ball that bounces. Every time it hits the floor, it will broadcast an impulse signal on the default channel and our Impulse Listener will pick it up.
- Click Add Component and search ‘Cinemachine Collision Impulse Source’.
- Under the ‘Signal Shape’ heading, we’re going to select a Signal for the Raw Signal variable. This is a ‘NoiseSettings’ profile. We can either use the default ones or create our own.
- Create a new ‘NoiseSettings’ profile by clicking on the gear icon, and select ‘New Noise Setting’. Save this to your project.
- We can choose to have the noise affect Position and Rotation or just one of them. Because it’s for a 2D Game, we’ll affect the X and Y positions and the Z rotation. A good noise profile is unpredictable. With noise, the only thing we want to do is replicate some of nature’s randomness. We can do this by having multiple layers with different detail.
- If we wanted to, we could add a gain to the Frequency and Amplitude that the Noise Profile applies.
- Under the Spatial Range heading, we’re going to adjust the Dissipation Distance. This determines at what range we no longer feel the impulse. Set it to 25 for now.
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:
- Get Cinemachine with Package Manager
- Create a 2D Virtual Camera
- Confine a Virtual Camera to a 2D Space
- Compose a Virtual Camera to follow multiple targets
- Set up an Impulse Module to add Camera Shake
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.
4 ComentariosSubscribirse a los comentarios
Ya no se aceptan más comentarios.