Unity3D: Creating Security Cameras

Creating logic for security cameras is very simple! Let’s have a look.

Objective

We want the camera to rotate back and forth at a 45-degree angle. We also want the camera to detect the player.

Security Camera Game Object

We want to select the game object with the camera mesh.

In this case, for me, it’s Camera_1.

Animation Window

We want to animate this game object in the “Animation” window.

You can find it in Window > Animation.

The animation window will open.

With the camera game object selected, we want to click on “Create.”

Name the animation “Security_Cam_Rotate.”

Create a folder for your animations if you haven’t already. Under your animations folder, create another folder called “Cameras.” It’s a good habit to keep your project files organized.

Save the file in “Cameras.”

Animate the Security Camera

We want to animate the rotation of the Y-axis to go from 45-degrees to negative 45 and back to positive 45. It will give us an accurate behavior of a security camera.

We must first click on the record button.

We want to adjust the y rotation to 45.

It will record the first position of the animation.

Go to the next 10 frames and change the Y-axis of the rotation to -45.

Let’s do the same one more time and make it positive.

When you hit the play button in the animation window, you will see that the camera rotates back and forth too fast. We need to change the samples from 60 to 10.

Now we have a good rotation speed!

Security Camera Trigger Scripting

With the animation of the security camera’s all set, we can now script logic so that when the player collides with the camera’s cone of vision, it’s game over!

Let’s create a new script and call it “SecurityCamera.”

Select the game object where the cone of vision of the camera is set. It’s a child object of the Camera_1 game object I animated.

Drag and drop the script into the game objects hierarchy window.

Open the script!

On Trigger Enter

We want the security camera to detect the player. So, we need to use a OnTriggerEnter() function.

private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
//Logic for when the player collides with this game object, which is the cone of vision. Remember, we added the script to the cone game object of the security camera.
}
}

We need a condition to look for the player’s tag. And if the player is detected, we want to execute logic for a game over cutscene or anything else you wish for your game.

We also want to make sure that the player has a rigidbody component attached. We also want the cone game object to have its collider be a trigger collider for the OnTriggerEnter() function to work.

Since I have made a cutscene for when the player is detected, I want there to be logic for the game over cutscene game object to be set active. I also want there to be a delay before the cutscene starts. And to spice it up a bit, I have the cone’s material change color to red when the player is detected.

private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
_rend.material = _red;
_anim.enabled = false;
StartCoroutine(DelayCutsceneRoutine());
}
}

private IEnumerator DelayCutsceneRoutine()
{
yield return new WaitForSeconds(0.5f);
_gameOver.SetActive(true);
}

Conclusion

We now have a working security camera that can detect the player!

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

Gabriel

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store