2D Cameras Using Virtual Cameras

Dennisse Pagán Dávila
4 min readJan 19, 2023

--

Cinemachine makes it extremely easy to add a full 2D view to your game by using the 2D Virtual Camera, which comes preset and ready to go with minimal tweaks needed.

Objective: Learn how to add a 2D camera that will follow the player. This camera will make use of a Confiner extension(optional) to lock into a specific zone, and an added Look Ahead feature to show more of the environment.

Table of Contents

· Setting Up the 2D Virtual Camera
Adding a Confiner
· The Result

You can check out my other Cinemachine articles here.

Setting Up the 2D Virtual Camera

  1. Let’s start by adding the 2D Camera via the cinemachine menu. It’ll have a generic name so I just renamed it to 2D Camera.

2. We need to add a Follow target. For this example, a capsule represent my 2D character, I will drag and drop this to the Inspector.

  • We won’t be adding a Look At target and therefore our Aim will remain as “Do nothing”. However, the Body setting will remain as the default Framing Transposer.
  • The reason why 2D Cameras use Framing Transposer as a default is because, in a 2D setting, we don’t need to concern ourselves with rotation. A Framing Transposer algorithm merely alters the camera’s spatial location. The camera is not reoriented and no rotation occurs.

3. In the 2D Camera settings, I will add a bit of Look Ahead time. This will make the camera move just a bit ahead of the character to show more of the environment. This tends to be extremely useful for side-scrolling games.

4. Back at our Main Camera, we need to Orthographic view which favors the 2D perspective and is a recommended Projection for a Framing Transposer algorithm.

With these elements in place, if you press play and move your character around, you will notice how you already have a working 2D Camera. However, if you get to an edge, the camera will show this less-than-refined corner, this is where the Confiner comes in.

Adding a Confiner

A Confiner is a cinemachine extension that you can add to an object to act as a type of zone. This will then confine your 2D camera to only move within a given space. So, if there are areas in your level that you rather keep out of sight, you can make use of this.

  1. For this example, I am placing and scaling a cube around the area where I want to keep my camera. Don’t worry, this won’t actually be visible in the Game view once the component is properly added.

Scaling the Cube:

The approximate size. This can always change but for now, I will leave it as is.

2. Go back to the 2D Camera and add the Cinemachine Confiner extension.

3. Set the Confinement type. Regardless of whether you use a 2D or 3D Confine mode, it can work for this example. It will all depend on the type of object you choose as a confinement zone. Since I have a cube, I will choose 3D.

4. Drag and drop your confinement object to the Bounding Volume slot. When you add this, the object will disappear from the Game View.

The Result

Now when you press play and get to that edge again, the camera should stay within the confined zone and keep it out of sight.

hope you have found this information valuable! Follow me for more Unity Development articles! :) I am a writer and Unity Developer on a journey to join the video game industry.

Check out my LinkedIn and Twitter!

--

--

Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. Portfolio: dennissepagan.com