2D Game Development – Cinemachine
Day 020 #100DaysOfGameDev
After adding the Cinemachine package in the Unity Editor, I was prompted to create a new 2D camera which was auto-named CM vcam 1 (for Cinemachine Virtual Camera 1).
Virtual cameras are used to cut between shots, and reverse shots in dialogue, which then tell the Main camera what to show to get seamless camera shots in your game.
Cinemachine cameras are useful for character following, or keeping your character centered on the screen at all times. To set this up it is as easy as dragging and dropping your main character object from the Hierarchy into the Follow property on the vcam Inspector. Now when you move the character around, the camera will automatically follow them.
Unfortunately, now that Ruby can move around, she can step outside of the drawn map and see the emptiness beyond. Stopping her is as easy as placing impassable barriers in her way, such as objects or water, but what about the camera which can still see beyond? The answer is to use a Cinemachine Confiner to define camera boundaries.
Think of a Cinemachine Confiner much like a Box Collider for sprites. By dragging the points of the shape around, you can define an area that the vcam recognizes as a boundary and won’t move beyond. If you run the game and try to move Ruby beyond this boundary she will keep going, but the camera will stop and she will eventually go off-screen, hence the need to put up physical barriers to block her movement.
To keep the character from reacting adversely with the new collider, which will push Ruby off the map until she is outside its boundary, we need to revisit the Layer Collision Matrix after establishing a new Confiner layer that only the camera will use. By unchecking everything on the Confiner layer, our game can run like it is supposed to.