First, we must find a good location to place the security cameras. We want to limit the amount of blind spots, but, similar to the real life, having too many cameras comes at a cost. Therefore, we have to find a right balance between the cost of implementing them and the amount we have in the scene. In our case, because this is a stealth game, putting too many cameras in the scene will cause the difficulty to increase dramatically.
Creating the Logic
Once you determine how many cameras you want in the scene, it’s time to create the logic to help the camera catch the intruders.
Here, I’ve added a GameObject variable that will receive the Game Over cutscene game object. This means that when the player is in the cone of vision, it will be game over for the player.
Furthermore, there’s a variable that will receive the camera’s Animator. This is needed to help the camera move.
Using a CoRoutine
A coroutine is used in this method to create a small delay between when the player is detected and when the game over cutscene is played. This is so the cutscene doesn’t play right away and create a delayed response.
Detecting the Player
Because we want the detection to start when the player comes into view of the cameras, we would want to use a OnTriggerEnter method. This way, it doesn’t start the Game Over cutscene if something or someone else comes into view of the camera.
Moreover, the color of the cone will change from green to red to signify the detection of the player. This is to help the player figure out why the Game Over cutscene is playing and plan another strategy to bypass the cameras.
Animating the Cameras
Now, it’s time to create the animation clip that allows the camera to rotate from side to side. First, select the camera in the Hierarchy panel and, in the Animation window, Create an animation clip.
After naming and saving the animation clip, it’s time to animate the camera.
Because the camera is only moving left and right, it won’t be too hard to animate it. Select the camera in the Hierarchy panel, if not already selected, and, in the Animation window, press Record.
Once you press Record, change one of the camera’s Rotation value in the Inspector panel. In my case, it’s the Y value that will allow the camera to move left and right.
Next, in the Animation window, select the next second you want the camera to move to the other side. In my case, because I want 6 seconds to pass before the camera reaches the other side, I selected 6 on the Time and rotate the camera the other way.
Depending on how you set up your cameras, you might have to do the following to the cone of vision
- Set “Is Trigger” to true in the Mesh Collider (to help determine if the player has collided with the cone)
- Add a Rigidbody (to allow for Triggers to work)
- Add the script you’ve just created and add the corresponding game objects.