Creating Security Cameras in Unity
Objective: Animate and give the security cameras game logic.
To get started animating the cameras, we select one and create a new animation. We can then record in the animation (camera position/rotations) in the animation window.
Next go in the animation and turn loop time off. We want the camera to move back and forward between the start and end positions. For this open the animator and make a copy of the animation. We will then rename it with a reverse name association and set the speed to -1 which will make the animation run in reverse. Next make transitions between both animations.
In the image above you will notice the animation is set to run over 1 second making the animation very fast. To slow it down you can re-record the animation or reduce the samples. By changing from 60 to 10 samples our animation now runs over 6 seconds.
Because both cameras will run on the same pattern and rotation, we can share the animation clip. To do this, add an Animator component onto the second camera and drag in the controller created for the first camera.
Next we want to set the cameras to trigger if the Player enters the cameras view. The cameras have a Mesh Collider attached and set to Convex and IsTrigger. This allows the view or cameras cone we are using for the light/cameras view to interact with other colliders.
To create the trigger interaction we create a new script for the cameras and set up an OnTriggerEnter method. We will use this trigger system to trigger the Player caught/GameOver cutscene. We will attach the script and a Rigidbody to the cones. To trigger the cutscene we need to create a public GameObject _cutscene inside the script and assign it in the Inspector. Inside the OnTriggerEnter method we check if the Player tag is the trigger and if so activate the cutscene.
You will notice in the above image when the security cameras are triggered and we cut to the cutscene, we now have 2 Players in the scene. We need to turn off the one we have been moving with and only have the one animated in the cutscene active.
To solve this, we open the GameOverCutscene and go to the Timeline.
We need to add an Activation Track to hide the playable Player. Drag the Player into the track.
You can see in the image above that the Player is active for the duration of the cutscene. Our cutscene has a fade to black at the end so we will set the Player to only be active for the final frame so that it is active when we restart later but is hidden for the visable duration of the cutscene.
Next for more dramatic effect we can freeze the camera that gets triggered and change the color of the cone light to a red tint for a half seconf before triggering the cutscene. To do this, we need to get the MeshRenderer on the cone, set a color and get the Animator off the parent so we can turn off the animation on the triggered camera. We do this using GetComponent and GetComponentInParent. We will also use a coroutine to give the delay before triggering the cutscene.
In the next post, we will look at the Sleeping Guard interaction.