Creating Game Eyes Part 2: Security Camera Vision

I previously covered setting up an artificial “eyes” behavior for pathing AI characters, but we can also create this behavior for stationary objects that only have their vision moving along a track — say, a security camera for example. In this guide I’ll cover setting up that behavior.

Our stealth adventure game, The Great Fleece, has a challenge that requires our player to sneak past a pair of security cameras that are scanning the ground below. If our player gets caught in the beam it’s game over.

Because our cameras are fixed to the wall their transform position will remain stationary, however we can still modify the rotation aspect of the transform to create a sweeping behavior.

Select the object you wish to animate and navigate to the Animation window to create a new Animation Clip. Name and save your clip. Then on your game object add an Animator component and drag in the new Animator Controller you’ve just created.

Now in the Animation window hit the record button and select the starting rotation for your object. In my case we will set a starting position of 45 on the Y rotation. At the 5 second mark we want it to reach a rotation of -45 on the Y, so we move the white marker to that mark and change the rotation value to create a new reference point.

This feels like a pretty good speed for our camera. Now, when the camera reaches the end point instead of instantly cutting back to the beginning of the animation we want it to play in reverse back to the starting point. This we can handle inside our animator controller.

There is a parameter on our new Camera_Rotate animation that controls the speed of the animation, but this feature also controls the direction. If our speed value is 1 the animation will play forward at normal speed, but if we set it to -1 the animation will play in reverse at normal speed. To ping pong back and forth between playing forward and in reverse, we can duplicate our animation, set the copy to speed of -1, and transition the two animations to each other in a loop.

Finally, ensure that exit time is turned on for the end of each animation. Our first camera is now looping correctly back and forth between the two animation states:

To add this behavior to our second camera, we can simply add an animator component to it and drag in the same controller into the Controller field.

Now that we have our cameras animating, we need to add the eyes behavior to trigger a game over cutscene if our player gets caught in the light beam. As you can see in the images above, when our beams are highlighted you can see the both the color of the beam and the green outlines of the mesh collider for the beam. These components are the Mesh Renderer and the Mesh Collider. The Mesh Renderer controls the live view of the object, in our case a cone shaped beam, and the Mesh Collider allows you to set behaviors for that shape. Using a Mesh collider allows you to create more detailed and accurate interactions with an object beyond the primitive shapes such as a box or sphere collider. This Mesh Collider is matched to the exact shape of our beam.

Within the Mesh Collider component, we have a setting for “Convex” which when enabled allows our mesh to detect collisions with other colliders. We set this to enabled and also set “Is Trigger” to enabled so that we can create an OnTriggerEnter function for our player collision.

To do this, we create a new script for our security cameras and apply them to our camera game objects on which the Mesh Colliders sit. Inside the script we create the following function to detect the collision:

Here we get a handle to our game over cutscene gameobject and set it active if a collision is detected.

That’s it! Our security cameras now have eyes to detect our player and our game over cutscene is working as intended. In my next guide we’ll connect the rest of the cutscenes to triggers in our game and go through setting up a GameManager for our game. See ya there!

--

--

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