Creating Security Cameras In Unity

Ali Emre Onur
4 min readApr 19, 2021

Another day is gone and new things are learned! If you are following me, my previous article was about distracting security guards in a stealth game. Now, when we pass the guards, there will be two cameras with motion which we will need to hide from. This article will be regarding creating these cameras in Unity.

As you can see above, there are 2 cameras in the scene, which will detech us if we trigger them by passing through their colliders. To create an automatic movement effect for the camera, I have created an animation to the camera by changing its rotate values. Simply click on record, set the first transform rotate value and then jump to the x second later and set it to the latest rotated position.

If you click preview, you will notice that it will loop directly from the beginning position, so make sure to turn loop off. If you think it is too fast (mine was), you can manipulate it by changing the sample size from 60 fps to a lower value.

Here comes the tricky part. Certainly, I could have make it to return to it return to the beginning location easily. Well, it would not teach us anything new. So here’s another way to do it.

The idea is that camera will scan 90 degrees of angle area as a second is passed. So far, we already knew it. Here comes the tricky part.

Open the Animator window, and select the rotation animation that you have created. On the Inspector, you will realize a speed variable.

Camera Rotate Inspector view

This speed variable acts just like in a script, so if we turn it’s value to 0, our camera will stand still (will not rotate), whereas if we set it to -1, it will move in the opposite direction.

From the Animator, right click on the rotate animation and paste it as a new animation just next to it. Create transitions in both ways without adding any condition.

And we are all set. Don’t forget to change the speed variable of the Reverse rotation animation to -1.

Reverse Rotation Animation Inspector view

Once we hit Play, we our cameras are rotating perfectly!

To detect the player, all you have to do is to add a Security Camera script with an if statement to end the game if the player triggers the camera colliders.

To give a better feeling of getting caught, you might want to add a delay after being detected. First, we need to reach to the animator component so that the camera will stand still (rather than keep moving) as we turn off the animator component. Then, reaching the mesh renderer, we can change the color of the cone area (the area within the colliders) to red. Lastly, I have used a coroutine to create a delay.

Just a quick note, the color change part is written in this way as the shader was not standard. So instead of simply typing Color.red, we are create a color by using RGB numeric values. If it gives an error on the run, simply divide the numeric RGB values with 255 and write them accordingly.

And here’s a shot from the game :)

--

--