Day 70 — Bringing Security Cameras to Life
Hey and welcome!
We’ve had a couple of security cameras sitting in our game for a while now so it’s finally time to make use of them by giving them some functionality to move about their search area and also spot Darren to cause a game over similar to what we do for the guards.
Let’s start things off by animating some rotation for our cameras, if you don’t have the security cameras in your scene make sure to grab them from the Environment folder in Prefabs and drag them into the hierarchy where they should line up where they need to be.
Inside the object you’ll see a Camera_1 object and this is what we’re going to be rotating. Click on it and then click the create animation option in the animations window to save it and open up the dope sheet.
Record the first frame with the rotation at 45 on the Y-axis and then move to the frame on the 1 second mark and select -45 for the rotation this time and stop the recording. A controller should have been created when you saved your animation but if not go ahead and create one, attach it to your Camera_1 object and have your animator setup like this:
The Camera_Rotate_Reverse state was created by copy and pasting the other state so it’s the same animation here but reversed. In order to reverse the animation you just need to click into the state and set the speed to a negative value, I’ve set my speed to -0.2 as well as 0.2 for my Camera_Rotate as the cameras were moving far too quickly before.
With that done you should get a ping pong effect happening with your cameras moving back and forth.
Now let’s work on implementing that functionality for spotting Darren as he passed through the camera light which is pretty easy to do as we did something similar for the guards.
Inside of your camera objects you’ll find a CameraCone object with a Mesh Collider component attached to them that covers the area of the light. Make sure that the Is Trigger is checked and then add a rigidbody component to this with the gravity off.
Now create a SecurityCamera script you can attach to hear and then fill it with the following code:
public GameObject gameOverCutscene;
public Animator anim;private void OnTriggerEnter(Collider other)
if (other.tag == "Player")
anim.enabled = false;
yield return new WaitForSeconds(0.5f);
You should recognise most of this code as it’s partly identical to what we used for our guards and will cause the Game Over cutscene to play if the player collides with the camera light.
The new part we’ve added here is the animator component on our camera getting disabled as soon as the collision happens and then the cutscene plays 0.5 seconds later by using a coroutine.
Make sure to add in the game over cutscene into the script component along with the animator which you just need to add in the Camera_1 and Camera_2 object to do that.
Things are looking pretty good with our game now! We’re starting to near the end of our project here as well so in the next one we’re going to add a way for us to interact with the sleeping guard which we’ve had a cutscene ready for that for a while now.