Creating Security Cameras in Unity

Objective: Animate and give the security cameras game logic.

To get started animating the cameras, we select one and create a new animation. We can then record in the animation (camera position/rotations) in the animation window.

Next go in the animation and turn loop time off. We want the camera to move back and forward between the start and end positions. For this open the animator and make a copy of the animation. We will then rename it with a reverse name association and set the speed to -1 which will make the animation run in reverse. Next make transitions between both animations.

In the image above you will notice the animation is set to run over 1 second making the animation very fast. To slow it down you can re-record the animation or reduce the samples. By changing from 60 to 10 samples our animation now runs over 6 seconds.

Because both cameras will run on the same pattern and rotation, we can share the animation clip. To do this, add an Animator component onto the second camera and drag in the controller created for the first camera.

Next we want to set the cameras to trigger if the Player enters the cameras view. The cameras have a Mesh Collider attached and set to Convex and IsTrigger. This allows the view or cameras cone we are using for the light/cameras view to interact with other colliders.

To create the trigger interaction we create a new script for the cameras and set up an OnTriggerEnter method. We will use this trigger system to trigger the Player caught/GameOver cutscene. We will attach the script and a Rigidbody to the cones. To trigger the cutscene we need to create a public GameObject _cutscene inside the script and assign it in the Inspector. Inside the OnTriggerEnter method we check if the Player tag is the trigger and if so activate the cutscene.

You will notice in the above image when the security cameras are triggered and we cut to the cutscene, we now have 2 Players in the scene. We need to turn off the one we have been moving with and only have the one animated in the cutscene active.

To solve this, we open the GameOverCutscene and go to the Timeline.

We need to add an Activation Track to hide the playable Player. Drag the Player into the track.

You can see in the image above that the Player is active for the duration of the cutscene. Our cutscene has a fade to black at the end so we will set the Player to only be active for the final frame so that it is active when we restart later but is hidden for the visable duration of the cutscene.

Next for more dramatic effect we can freeze the camera that gets triggered and change the color of the cone light to a red tint for a half seconf before triggering the cutscene. To do this, we need to get the MeshRenderer on the cone, set a color and get the Animator off the parent so we can turn off the animation on the triggered camera. We do this using GetComponent and GetComponentInParent. We will also use a coroutine to give the delay before triggering the cutscene.

In the next post, we will look at the Sleeping Guard interaction.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Running a free VPS on Google Cloud

Delta Lake for Dummies?

Encode x Polygon: Polygon Technical Workshop (Asia + Oceania) [Video + Resources]

Web-enabling your Flutter app and hosting on Firebase

10 free stock photos you would actually use (Sunday 22nd 02PM edition)

Everything You Need To Know About Kafka (Part 2)

How to turn your older Raspberry Pi into a BitTorrent Home Media Sever

14 Days Study Plan to Crack Algo I

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
Simon Leen

Simon Leen


More from Medium

Adding a thruster speed boost on keypress

Eyesight in Unity

IDamageable Implementation!

Quick tip — Enabling post-processing in Unity Universal Render Pipeline