Setting an Interactive Security Camera System

Gabriel Perez
4 min readSep 25, 2022

Using Cinemachine, we can create different camera events when needed!

Objective

The idea is to have the player enter a trigger that will allow the switching of multiple virtual machine cameras set in position around the environment with a touch of a button.

Camera Setup

I set four virtual cameras around the room, looking in all directions.

Stationary Camera 1
POV Camera
Blend Camera
Stationary Camera 2

With my cams in place, I need a way for the player to trigger and enable the ability to cycle the security cams.

Trigger

I set a trigger collider in the middle of the room.

It will allow me to attach a script to the trigger collider so that it can detect the player.

Script

I want to access all of my security cams by caching them to an array.

[SerializeField] private CinemachineVirtualCameraBase[] _cams;

I also want to increment the index of the array every time I press the button to cycle through the cameras. So I set an integer variable to keep track of the value.

private int index = 0;

I also want to add a boolean so that when the player enters the trigger, I can set the bool to true to enable the cycling of the cameras.

private bool _canCycleCams;

Since I want this trigger collider to detect the player, I want to use the OnTriggerEnter() method that Unity provides.

private void OnTriggerEnter(Collider other)
{
if (other.CompareTag(“Player”))
{
_canCycleCams = true;
}
}

If what I (the trigger collider) detected is the player, then set the bool to true. When true, the player can use the input system to cycle through the cameras.

Next, I want to set the current camera to a higher priority. It will allow any camera with the current index to have priority over the others. If the current index value is 1, the camera’s priority at index level 1 will be a higher value.

private void SetCurrentCamera()
{
if (_cams[index])
_cams[index].Priority = 100;
}

If we are on this camera, set the priority of the camera to 100. The magic number 100 can be anything. As long as it’s higher than the default priority value.

Next, I want to set back the priority of all cameras to 10. It will allow for the current camera to always be active when switching cameras.

private void SetLowPriority()
{
for (int i = 0; i < _cams.Length; i++)
{
if (_cams[i])
_cams[i].Priority = 10;
}
}

I want to loop through each camera and set its priority to 10.

Next, I want to press the y button to cycle through each camera in the Update method.

if (Input.GetKeyDown(KeyCode.Y) && _canCycleCams)
{
index++;
if (index > _cams.Length — 1)
{
index = 1;
}
SetLowPriority();
SetCurrentCamera();
}

If I press the ‘Y’ button and I can cycle through the cameras, I want to increment the index. I also want to check if the index is greater than the total number of cameras. If so, I set the index to 1. It will switch my cameras back to the first. Index 0 is the default player camera.

I also want to set the lowest priority and the current camera methods to update the cameras.

Lastly, I want the default player camera to be active when the player leaves the trigger collider.

private void OnTriggerExit(Collider other)
{
_canCycleCams = false;
index = 0;
SetLowPriority();
SetCurrentCamera();
}

When the player exits the trigger, set the bool to false. If left to true, we can cycle through the cameras anywhere outside of the trigger.

I also set the index to 0, the default player camera. I call in the SetLowPriority() method to reset all the cameras. Then I call in the SetCurrrentCamera() method to update the camera back to the stored value in the index.

Conclusion

That is all for today! Thank you for your time.

Gabriel

--

--

Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.