Creating an Immersive Security Camera System with Unity, Cinemachine and User Input
Creating an interactive security camera system can add a layer of excitement and challenge to your project. With the help of two powerful scripts, “CameraTrigger” and “CameraController,” you can easily build an engaging security camera system that responds to player actions. In this article, we will guide you through the process of constructing an interactive security camera system using these scripts in Unity.
The “CameraTrigger” and “CameraController” scripts are essential components of the security camera system. The “CameraTrigger” script detects when the player enters or exits a specific area, while the “CameraController” script manages camera priorities and camera switching.
Setting Up the Security Camera Area: Start by placing a trigger collider in the area you want to monitor with the security camera. Attach the “CameraTrigger” script to the trigger collider. I’ll use this long cuboid as trigger.
Here is the Camera Trigger script:
public class CameraTrigger : MonoBehaviour
{
[SerializeField]
private CameraController _camController;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_camController.onStep = true;
_camController.SetLowCamPriorities();
_camController.currentCamera = 1;
_camController.SetCurrentCamera();
}
}
private void OnTriggerExit(Collider other)
{
_camController.onStep = false;
_camController.SetLowCamPriorities();
_camController.currentCamera = 0;
_camController.SetCurrentCamera();
}
}
The “CameraTrigger” script listens for the player’s entry and exit events within the trigger area. Upon detecting the player, it instructs the “CameraController” to switch to the designated security camera. When the player exits the area, the script returns the camera to its default state.
Here is the Camera Controller script:
using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[SerializeField]
private GameObject[] _vCams;
[SerializeField]
private GameObject _trigger;
public bool onStep;
public int currentCamera;
public void SetLowCamPriorities()
{
foreach (var c in _vCams)
{
if (c.GetComponent<CinemachineVirtualCamera>())
{
c.GetComponent<CinemachineVirtualCamera>().Priority = 10;
}
if (c.GetComponent<CinemachineBlendListCamera>())
{
c.GetComponent<CinemachineBlendListCamera>().Priority = 10;
}
}
}
public void SetCurrentCamera()
{
if (_vCams[currentCamera].GetComponent<CinemachineVirtualCamera>())
{
_vCams[currentCamera].GetComponent<CinemachineVirtualCamera>().Priority = 15;
}
if (_vCams[currentCamera].GetComponent<CinemachineBlendListCamera>())
{
_vCams[currentCamera].GetComponent<CinemachineBlendListCamera>().Priority = 15;
}
}
private void Update()
{
if (onStep)
{
if (Input.GetKeyDown(KeyCode.C))
{
currentCamera++;
if (currentCamera >= _vCams.Length)
{
currentCamera = 1;
}
SetLowCamPriorities();
SetCurrentCamera();
}
}
else
{
currentCamera = 0;
}
}
}
The “CameraController” script is responsible for handling camera priorities. It sets all cameras except the current active camera to a low priority level to ensure that only the chosen security camera has the highest priority and remains in view.
With the security camera active, players can interact with the system by pressing the “C” key to switch between different security cameras. This feature adds an interactive element to the gameplay and allows players to survey their surroundings strategically. This is how it looks in action:
To further enhance your interactive security camera system, you can customize the number and positions of the security cameras, adjust camera angles, and even introduce more complex camera behaviors using Cinemachine’s powerful features.
Creating an interactive security camera system in Unity is a rewarding endeavor that can elevate your game’s immersive experience. With the “CameraTrigger” and “CameraController” scripts, you can seamlessly manage camera priorities and dynamically switch between security cameras as the player interacts with the environment. By customizing and expanding on this system, you can craft engaging and challenging gameplay scenarios that keep players on their toes. Happy developing!