Creating Security Cameras in Unity

Calum Slee
Level Up Coding
Published in
4 min readAug 27, 2021

--

After we get past the patrolling guards, we are faced with two security cameras. These objects are made up of a static holder arm, the camera itself we would like to rotate in a panning fashion, and a visible camera cone that will act as it’s detection method.

Let’s first look at animating the rotation. We simply need to plug in keyframes across our desired amount of time from the minimum Y value to the maximum. Then in our Animator window we can create an infinite loop between two instances of the animator state, with one’s speed set to -1 so it runs in reverse.

With the camera’s animated, let’s look at creating the detection. Our Camera Cones are fitted with a Mesh Collider which allows us to have a collider set to the shape of the Mesh Renderer, allowing for maximum precision.

To make use of the Collider we need to create a new Script for our security cameras. Each camera has it’s own instance of the script, but if one camera detects the player, I want the caught behavior to be enabled on both. To do this, we can create a List to store all instances of this script. Using Tags we can find all camera cones in the scene, store them in an array, then for each, access their script to store in our List.

Now On Trigger Enter, if the Player is detected, we can call the PlayerCaught method on all Security Camera scripts.

The PlayerCaught method is public, and serves as a way to simply set a bool to true.

Then in our Update method, if aforementioned bool is true, we can start a Coroutine to run through our camera behavior.

This Coroutine immediately sets the bool to false to prevent multiple instances being called. Then we run two methods, one to stop animating the camera rotation, and one to change the color. Next, we wait half a second before activating our cutscene game object.

These two methods need to capture a List of their required components.

Firstly, the Animator is contained on a different Game Object, so we need to run another FindGameObjectsWithTag function. Then for each of these objects, we can get the Animator and add it to the List.

With this done, we can simply disable each Animator in the List.

For our Mesh Renderer, we are already capturing an Array of each Camera Cone object, so we can simply append getting the Mesh Renderer and adding it to a List alongside where we get our Script.

To change the Color of the camera cone, we have to go a bit deeper than a standard color change. This is because our object is using a particle shader to create the appropriate look. What we want to change in this Shader is the TintColor, therefore we need to pass this string value in, before setting our color.

Now with static red cameras when the Player is caught, our cutscene doesn’t necessarily happen in the right place. To fix this, I’ve stored a Vector3 assignable in the Inspector to set a final rotation for our cameras to work with the cutscene. This method will get called alongside the Coroutine in our Update method.

We can simply grab the Parent Transform of the camera cone which our script is on. Then we can set the Euler Angles to our Vector3 variable.

--

--