How to Build an Interactive Security Camera System

Dennisse Pagán Dávila
8 min readJan 27, 2023

--

By combining the power of Cinemachine and C# scripts, we can create a fully interactive Security Camera system, let’s have a look!

Objective: Learn how to create security camera systems that, once triggered, allow you to alternate the views between the cameras at the press of a button.

Table of Contents

· Camera Set Up
Camera 1 — Point of View(POV) Camera:
Camera 2— Static Camera:
Camera 3— Blend List (pan camera):
· Scripting the Camera Manager
Accessing the Cameras through Code
Using Camera Priorities to Switch View
· Scripting the Camera Trigger Enter
· Scripting the Camera Cycle
Cycling Example
· Scripting the Trigger Exit — Return to Default View
Tweaks to Manager Script
Tweaks to the Camera Trigger Script
· The Result

You can check out all my Cinemachine articles here.

Camera Set Up

Since I’ve gone over individual camera setups in previous articles, I won’t be making a deep dive into this section.

Here we will be setting up three different cameras for our player to alternate between, this script works the same no matter how many cameras you have or which type you are using.

Camera 1 — Point of View(POV) Camera:

The view from Camera 1 — POV Cam
  • This first camera is a Virtual Camera with a POV Aim system.
  • The vertical and Horizontal movement has been limited to a -20 to 20 range.
  • The wrap has been unchecked because we don’t want the player to look behind.

Camera 2— Static Camera:

Camera 2 — Static Cam View
  • The static Camera is overlooking the entrance and does not move because the entry point is the most valuable thing it can guard.
  • The only characteristic of this camera is that it is a virtual camera that does nothing with the Body or Aim.

Camera 3— Blend List (pan camera):

  • This virtual camera is set to pan back and forth between two views, this is why we use a Blend List camera so that we can create this animation automatically.
  • The Blend List Camera has 2 Child Cameras in different positions. It will automatically pan between the view of these two.
  • It will stay at each view for 2 seconds before it pans to the next with 1 second to transition.

You can learn more about how they work here:

With the cameras set up, we can start scripting this to be interactive.

Scripting the Camera Manager

This script will handle anything to do with our camera properties and how we can use them.

Note: I am using a Starter Assets Third Person Controller to move my character, you can learn more about it here, but you don’t need to use it for this example.

Accessing the Cameras through Code

  1. You will need to add the Cinemachine namespace so that we can access the virtual camera features we need.

2. Now we need a way to access the cameras we created before. Add a global array variable with a SerializeField so that we can assign the cameras in the Inspector.

3. Create an empty Game Object, this will be our camera manager, and our Camera Manager script will be attached to it. (Pardon the capitalization there, it should read CamManager).

4. Assign the cameras to the Inspector. Make sure that camera 0 is your default camera view.

With the cameras in place, we can properly access them through code. so, let’s start solving the first problem: switching from camera to camera.

Using Camera Priorities to Switch View

We previously learned that switching cameras in the cinemachine can be achieved by controlling the priority level. Cinemachine will always activate whichever camera has the highest priority, and we can control the priority level through code.

  1. Since we need to cycle between multiple cameras, we will create a public void that holds a for each loop within it. I will be explaining the code per line.

The purpose of this void function is to set a low-level priority per camera.

For each camera it finds within the camera array, it will set a priority of 10. The if-statement is there to search if there is any Cinemachine Virtual Camera present.

It’s important to note that the type of component you use here will depend on the type of camera used in your scene.

If you used any other camera in that list, simply change the name accordingly, just as I did with my Blend List Camera on the second if-statement.

Full Void Function to Set Low Priority:

2. We need to determine what is a high priority, and we want that to be the current camera in use at all times, which means we need a way to track which is the current camera. Let’s go back to our global variables and add an int.

3. Create a void function to set the current camera. This will be extremely similar to our for-each loop, but here we will set a high priority instead.

With our priority-setting functions in place, our next move is to tell our project when each of these functions should be used. For that, we will create a Camera Trigger Script.

Scripting the Camera Trigger Enter

In my scene, I have a Cube set as a trigger event. Our Camera Trigger Script will be attached to this cube.

When the player walks into it, they will enter the trigger, activate the camera system, and enable us to change the view at the press of a button.

  1. Since we’ll be using functions from our previous script, we need to start by creating a reference.

Then drag and drop the CamManager object into the SerializeField slot. Through this, we can access the script attached to that object.

2. Let’s add our collision logic by using an OnTriggerEnter since that is the type of collision our Cube is set to detect.

  • To make sure this isn’t triggered by other objects, we will be checking only for objects that have the Player tag.
  • We call our functions from the previous script(Manager Script) into this one.
  • We also call the currentCamera variable and set it to 1, which is the #1 element in our camera array.

This alone makes it so that the camera switches view to our camera 1 (POV cam) when the player walks into the trigger. However, we can’t cycle through cams yet or exit this mode.

Scripting the Camera Cycle

This bit of code will be added to our previous script, the Camera Manager script.

Cycling through cameras a the press of a button is simple enough.

  1. Let’s start by registering the input. Once the input is registered, we add it to the currentCamera so that we can keep cycling between them.

2. But we don’t want it to cycle indefinitely, instead, if we get to a number higher than the number of cameras(greater or equal to the array length), it will simply reset the cycle back to our first camera.

3. If nothing is pressed, we need to make sure that our low-priority and current camera functions run regardless so that they can be modified accordingly when the security cameras are activated.

Cycling Example

The cycle logic should be fully functional but we still need to go back to the default view when we exist the trigger.

Pressing C to cycle through camera views

Scripting the Trigger Exit — Return to Default View

This is very simple to manage with a bool variable.

Tweaks to Manager Script

  1. Add a public bool variable as we will be accessing this from the Trigger Script as well. It will be set to false by default because we only want to be able to cycle through views when we enter the trigger.

2. Add the condition before the Input.

Tweaks to the Camera Trigger Script

  1. Add the condition to your OnTriggerEnter Collision

2. Lastly, we will add an OnTriggerExit to stop and reset everything once we exit the trigger.

The Result

I hope you have found this information valuable! Follow me for more Unity Development articles! :) I am a passionate Unity Developer and Writer on a journey to join the video game industry. Check out my LinkedIn and Twitter!

--

--

Dennisse Pagán Dávila
Dennisse Pagán Dávila

Written by Dennisse Pagán Dávila

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

No responses yet