Security Cameras in Unity

Simple cameras to detect the player.

The camera in action

Yesterday we gave our guards the ability to see the player using a box collider. Today we will setup some security cameras that will detect the player and trigger the same game over scenario.

The cameras are a little more complex than the guards. Guards see directly in front of them, while the cameras need to rotate and have a cone shaped line of sight.

We will still use a collider for this but a box collider won’t be the best option. Instead we will use a different type of collider that we can conform to the shape of our camera cone.

My camera has 3 parts. The camera cone, the camera itself and the holder. The holder won’t be touched once we set everything in place, but we will work with the camera and the cone.

The first part of our camera, the one that will do the work is the camera cone. The cone will be what detects our player when he enters into the light. Now we could use a sphere collider if we really wanted to but why do that when Unity gives us something as good as the Mesh Collider.

Mesh collider settings

The mesh collider will conform to the object we placed it on as long as we choose to make it convex. We want to minimize the number of mesh colliders we use in our games. If we have a lot of really complex meshes and we apply mesh colliders to them we may severely decrease the performance of our game.

The next thing we need to do is animate the cameras movement. We do this using a traditional animator and animation. We just need to click on the camera part we want to move and on the animation tab click create.

This creates the animator and we can now add what we want to animate by clicking ‘Add Property’. In this case I just want to animate the cameras y rotation.

Only the Y is animating

Note that it is much easier to hit the record button and key frame the animation for the Y rotation that it is to try recording the gizmos. When using the rotation gizmo you may get unintended results. Sticking with adjusting directly in the keyframe or the inspector will give you much better results.

Once we have a rotating camera, and a cone that can detect our player we need to add some script to handle the detection.

Camera AI Script

My CamerAI.cs file does two things if the player is detected. First, it changes the color of the cone to red using the Renderer on the cone object. Second, it activates the game over cut scene.

Once all of that is done we have working cameras that will detect the player and trigger game over. We could take this a step further and add real cameras to these security cams and create a real security camera with it.

Turning my passion for video games and 11 years of software development experience into a focus on video game development using Unity3D.

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