How to implement a different kind of guard — a security camera
In this article we continue working on our simple stealth game in Unity. A “stealth game” is a game where we seek to achieve our objectives by avoiding contact with the enemies rather than fight them.
This particular game will be a variation on “The Great Fleece” project from GameDevHQ. This project is a collection of assets put together with the express purpose of being the aid for learning cinematography and building a basic stealth game.
In the last article, we gave our security guards a rudimentary sense of “sight”. This time, we’re going to work on this idea a bit more by building out some security cameras.
Getting a better cone of vision
The first thing we’ll do to iterate upon the security guard is upgrade our “vision” collider. With the guard, we could only create a collider out of simple 3d shapes such as boxes and cylinders. With the security camera, we’re going to make use of the Mesh Collider because we have a mesh asset available to us.
This allows us to have a much more natural shape for the “vision” of our cameras.
As stated earlier, using a Mesh Collider requires a mesh asset to form the shape of the collider. Also, in order to use the collider as a trigger, it must also be configured to be Convex.
A visual cue for the player
Another improvement we can make to our game is to make things a bit clearer for our players by making the camera’s vision cone visible.
Why do this though? Won’t it make the game too easy?
Well, maybe, but given the 3rd person perspective the game is in, the security cameras are arguably too difficult to notice as is. So, with this change in visibility we will change the nature of the camera’s threat.
Achieving this effect is not difficult. All we need is to have an appropriately tinted material on the mesh being used as the vision cone.
How does the camera move?
In a previous article, we had learned about using Unity’s navigation features to move AI security guards. We want our security cameras to move too, but not in the same way. We want them to remain in place while rotating back and forth on a limited angle. For this we will use the Unity animation system.
Start by opening the Animation window in the Unity editor, then select the security camera object in the scene. We should see in the Animation window a Create button. Clicking that button allows us to create an animation and an Animator Controller simultaneously, while also attaching an Animator component to our security camera object.
Once this is done, everything should be ready to make an animation and the Animation window (when selecting the security camera) should show as being present but empty. Click the record button in the Animation window to get started making the animation.
With the animation recording, go to your security camera object and set the rotation of the camera to one extreme of what the security camera’s range will be. For example, if we want the security camera to rotate between 60 and 120 degrees, we would make sure the animation records the security camera as being at either 60 or 120 degrees at frame zero.
With that accomplished, we next need to decide how quickly the security camera should be rotating. Let’s say it should do a sweep in five seconds. So we then create a new animation frame at the five second mark by double clicking at that point along the animation timeframe. Adjust the position of the animation frame as needed, then in the security camera change the rotation value to the other end of the security camera’s rotation range.
Now we can stop recording and examine our work. Press the play button in the Animation window to see the animation play out. We should see it gradually rotate from one end of the range to the other, then instantly snap back to where it started from. We can address this issue by finding the animation we created in our Project window, and then in the Inspector unchecking the Loop Time option.
So now we have the camera turning once and then stopping. We want it to rotate back and forth continuously, so we next need to look at our animator controller in the Animator window.
We can see here that the security camera will immediately begin our animation by default. Good.
Next we need to have our camera rotate back to its starting position. Fortunately, there is a cool trick we can do in the animator to accomplish this.
In the Animator window, copy the rotation animation (in the picture above, it is the orange box labelled “Camera_Rotate”) then paste it into that same animator. Click on the new animation and look at it in the Inspector window.
Within the Inspector window, find the Speed setting and then change it from 1 to -1. This reverses the animation!
Now, back in the Animator window, we can make transitions from the original animation to the reversed animation and back by right-clicking on the animations and finding the “Make Transition” option in the drop down menu.
With this change, the security camera now moves back and forth on its own.
And that covers it. Much like with the guard, how the security camera should react now when the player is spotted is up to us to figure out, but I hope this article was helpful in exploring how a security camera could be formulated in Unity. Thanks for reading.