How to Build an Interactive Security Camera System in Unity
Objective: To show you how to build an interactive security camera system in Unity using different Cinemachine cameras.
Let’s say you have a scene and the main character needs to move past a bunch of security cameras (possibly down some hallways) and you have access to watching them. Now let’s take this basic scene, build it and play it out using primitive assets and Cinemachines cameras. For prototyping purposes this is a wonderfully quick way of figuring out whether the scene/gameplay will play out how you expect it to.
I will also create a trigger switch in the middle of the room so that when the character stands on it, it will automatically turn all the cameras on and subsequently turn them off when they step off the trigger, deferring back to the original 3rd person follow camera that we have setup initially.
Stepping on the trigger switch will also give the player the ability to press the ‘C’ key and cycle through the various cameras.
Example of the Scene
To get started I am showing a quick snapshot of the scene and how I have it all setup:
Add 3D Character
Let’s add our 3D character from the Unity Asset Store as this is the quickest and easiest solution for this.
Head to the Unity Asset Store for your ‘Starter Assets — Third Person Character Controller’. Get this downloaded and installed into your scene via Unity’s Package Manager:
Window → Package Manager → Packages: My Assets → Starter Assets — Third Person Character Controller → Import.
With this installed, head to the top tab and look for Tools → Starter Assets → Reset Third Person Controller Armature.
This will instantly create a working 3rd person character in the scene when you press Play!
Building the Environment
Let’s make this super simple and just create 3 cubes that are spaced apart to reflect hallways and adjust their scale to your liking.
Also create a cube in the centre for the player to step on. Make sure that the Box Collider component is added and that ‘IsTrigger’ is ticked. When this is done, make sure to turn the Mesh Renderer component off so we don’t see the cube.
Adding some Cinemachine Cameras
Referring to our initial screen snapshot, let’s now create our 3 Cinemachine cameras by simply adding them via the top tabs, Cinemachine → Virtual Camera…
For my project I am going to have one basic static camera, one point of view (POV) camera that has restricted movement on both the vertical and horizontal axis and finally for the last one, it will be an automatically panning camera.
This will be the static camera that won’t have a Follow or Aim target.
Cinemachine → Create Virtual Camera.
Move to the location you like, I have moved it to position 1 with this view:
I have only need to adjust the ‘Lens — Field of View’ setting to achieve this view.
This will be the Point of View (POV) camera with restricted movement.
Cinemachine → Create Virtual Camera.
Again I have adjusted the ‘Lens — Field of View’ setting for this camera and also set the ‘Aim’ setting to ‘POV’ to achieve this view:
Now let’s set the restricted movement of the camera so that when the player is cycling through the cameras and get to this one, they will notice as they move the mouse on the X and Y axis that it will stay within a set range:
This camera will be the automatically panning camera that will simply move left to right. In this instance we will use a Blended Camera List.
Cinemachine → Create Blend List Camera.
Let’s set this up at position 3 and we are going to need 2 children for the Blend List. Let’s also set this to loop so they will continue this behaviour.
Lastly, let’s choose our blend options and how long we want to hold for at each camera position:
When the player presses ‘C’ for the fourth time, they will go back to the original third person camera view.
Creating Code for Trigger Switch Functionality and Camera Cycling
We are going to need 2 scripts for this.
The first is the ‘TriggerSwitch’ script that will go on the trigger box and will activate the cameras whilst allowing for player input to switch between cameras as they stand on the trigger.
The second is the ‘CameraManager’ script which is going to go on the ‘CameraManager’ game object and will hold a reference to all the cameras in an array, allowing us to be able to grab the Cinemachine camera components and easily reset/adjusting the priority levels of the cameras using If/Else statements and a For Loop.