Combining LookAt And Body In Virtual Cameras (Part 2: Changing Cameras using Triggers)

Alex Frankovic
6 min readJun 16, 2023

Objective: How do we utilize what we know about LookAt and Body within a virtual camera to combine the two to create an effective and well-working virtual camera on runtime?

In my last article, I looked into how we can combine both the LookAt and the Body to create a well functioned virtual camera. I was able to transition smoothly from a 3rd person camera to an orbital camera when holding down the right mouse button.

In this next piece, we are going to look at a different scenario where I now have four different virtual cameras and when my capsule passes through these triggers, my virtual cameras will change as the player advances.

SCENARIO 2: USING TRIGGERS TO CHANGE VIRTUAL CAMERAS

So I want to have four different triggers scattered across my plane when when my capsule passes through those triggers, my active virtual camera will change. I did this by creating a 3D cube object and stretched it out so it basically serves as a line. The idea here is that once my capsule touches each of these blue lines it will trigger a new virtual camera to be active.

The following triggers will activate the four cameras as followed:

  • Start — 3rd Person camera
  • Trigger 1 — A higher elevation camera
  • Trigger 2 — A track camera
  • Trigger 3 — A camera that looks at the player
  • Trigger 4 — Back to 3rd person camera

STEP 1: SETTING UP OUR CAMERAS

So before I get into transitioning between my virtual cameras, I would have to create them first.

Camera 1: 3rd Person

Assign our capsule to follow and set our aim to same as follow and then adjust our height settings so we have a good follow through camera behind our capsule.

Camera 2: High Elevation Camera

This one is simple! I created a virtual camera at a high elevation above where my capsule will be moving and used align to view to get it where I want it to be.

Since this is a high elevation camera, i’m probably going to just sit on the aim and ignore the follow so I’m assigning my capsule to my LookAt.

Camera 3: Track/Dolly Camera

For this one, we want to create an empty gameObject and add the Cinemachine path script (built in Unity) where we can create this imaginary track that our camera will follow along on.

With the path set up, we can assign this within our body inside of the virtual camera. Make sure we assign our capsule to the LookAt and Body as well.

Camera 4: Looking At The Player/Capsule

This next camera I’m going to do a close up to the face of my capsule. I want to assign my capsule to my lookat for this one and make sure I am adjusting my soft zones only because this camera is up close and personal so I want my camera to keep up with my object!

With all four cameras set up, I can now jump into transitioning between one another upon trigger.

STEP 2: TRANSITIONING BETWEEN CAMERAS

The next step here to transition from one camera to another upon stepping over each blue line! The first thing we are going to want to do is create a new c# script to handle all the managing and transitioning between our cameras and assign this script to an empty gameObject. Once set up, I’m going to create a gameObject array type variable where I can store these cameras within that array.

Now instead of setting cameras active and deactivated, I’m going to tackle this just a bit differently. I’m going to focus on changing the priority of our cameras when passing through our triggers. Now this new C# script will serve as a manager and we will use script communication to pass these methods through.

The two methods I created for this script is setting all our priority cameras back to default (10 each camera) and another method where we are setting one camera to a higher priority (15).

Notice how I passed in an int value in my parameter. This is going to represent the element of the gameObject (camera we are changing) when we pass it to our other script. This will all make sense soon. Now let’s create another C# script where we are going to assign it to each of our triggers (blue lines) to implement the change of camera.

Note: Set a tag for our capsule so we can locate it through OnTriggerEnter for this next step!

Now we are creating another C# script which is being assigned to each of our triggers. We first want to communicate this script with our camera manager script. We do this by setting a handle variable we can use to access the other script and using it to get the components.

With my script communication now set up, I can pass in my methods from my first created script through OnTriggerEnter. I create an int variable where I assign the int value I can pass through my parameter and assign it in my inspector in Unity. With that being said, here are my two full C# scripting followed by my smooth transition between all four of my cameras based on the priorities switching.

cameramanager scripting
cameratransition scripting (assigned to my triggers)

--

--