Unity Development — Security Cameras
We have worked on some AI enemies spotting you out in a stealth game. There are other things that can blow your cover, however. Let’s make some security cameras that could give the players some challenge as well.
As far as the visual aesthetic, you can do what you like. For our purposes today, we will have two cones representing the area where the player can be caught, which are children of two camera game objects on some pillars. These cones have on a material in their Mesh Renderers to have them be transparent. They also have Mesh Colliders, which are Triggers, and Rigidbodies, so the triggers can be detected.
Before we get to the code for the triggers, we are going to move things around as an added challenge and more realistic camera style. On the parent camera objects, we are going to add some animation. Open the Animation window and hit ‘Create’ when the camera is highlighted. This will allow you to create a new animation and automatically add an Animator component.
In the Animation window, we are simply going to hit the red record button and set a number on the Y Rotation in the camera’s Transform. At certain time length away, set the Y Rotation to the opposite of your previous set number. Try -45 to 45 for example.
We can take a look at this in the Animator controller itself. You’ll see the standard ‘Entry’, ‘Any State’, and our only animation for the camera. If you click on that animation, you’ll see some attributes in the Inspector, including speed.
Duplicate that animation and set the speed of the duplicate to -1. This will have it run backwards. Right click and make a transition to and from these animations. These will now run back and forth, creating the same affect for the camera.
Finally time to look at the code. Make sure your cones are triggers and have a Rigidbody. Just as we have done before, we are going to use the OnTriggerEnter() function to see if it is the player that was seen. You can also add any bit of polish you like. Stop the animation from happening so that they stay still, change the color, add some audio alarm, or even trigger a cutscene.
As you can see in my code example, we check for the tag of the ‘Player’, disable the Animator, change the color, and wait a half second before starting the Game Over Cutscene. You, however, can do whatever you like at this point. Maybe have enemies attack and continue the gameplay. It is all up to you.