- [Instructor] In this video, we're going to take a look at how we can use Cinemachine in a 2D project. We're going to look at how we can use Cinemachine to customize how an orthographic camera behaves and get the flexibility that Cinemachine can bring to the compositions in our 2D project. We're also going to look at how we can limit our camera's movement inside an environment using the Confiner component.
In this scene, we have a 2D environment with a character we can move around. Currently, we just have a standard camera attached to our player avatar. Because the camera is just parented to the game object, it moves whenever our character does.
This is okay but I'd like to customize how our camera reacts a bit better. Let's use Cinemachine to create a virtual camera and track our sprite. We'll create a new camera by choosing Cinemachine, Create 2D Camera.
Then we'll assign our player character to the follow property of the virtual camera we've just created. Now we can set up our shot just like we can in 3D. We can set the framing of the shot using the inspector or the guides in the game window to create the behavior for our camera.
I'd like my camera to only start moving when the character runs a small distance across the screen. So I'm going to set my dead zone width to about 0. 25.
This way, they can run back and forth a little bit without the camera moving. Let's also recompose the shot so that our character is on the left edge of the screen. I'd like the camera to move as the character jumps but with a little bit of delay.
Let's set the dead zone height to zero so that the camera definitely moves when the player jumps and then set the soft zone height to about 0. 8 so there's enough delay as the gravity pulls our character back down into place. This is already a lot better than where we started.
The main issue now is that our camera exposes the edge of the scene if we run too far. Ideally, our camera should stop moving once it's reached the edge of our environment. We can tell Cinemachine to do this by adding a Cinemachine confiner component to our virtual camera.
The confiner component uses a bounding collider to restrict how our camera moves. If we create a collider on our background, and then assign this to the bounding shape property on our camera, Cinemachine will now restrict itself from moving past the collider in the scene. For more information on Cinemachine, and for more Unity tutorials, please follow the links below.
Thanks for watching.