-
Now we will add one of the systems that
-
triggers the alarm in our game,
-
the CCTV cameras that are trying to spot the player.
-
The first step is to drag the model in
-
to the scene and to position it on the battle bus.
-
In the Models folder locate prop_cctvCam.
-
It should look like this. Drag it in
-
to the hierarchy and then position it at
-
(-8, 3, 16.1).
-
This will place it on the battle bus
-
but it needs rotating by 180 in the Y axis.
-
Select it in the hierarchy and with
-
your mouse cursor over the scene view
-
press F to focus on it.
-
Make sure to check the Use Light Probes
-
box on the mesh renderer on each of the
-
game objects in the camera hierarchy
-
so that the camera has dynamic light shining on it.
-
As you can see it's now properly lit.
-
Light probes have been setup for you
-
and are part of the Lights prefab that we
-
created earlier.
-
The next thing our CCTV camera needs
-
is a frustum. We want to start by adding
-
the collision mesh to the camera.
-
When the player is detected within this frustum
-
the alarm will sound.
-
We want to start by adding the collision mesh
-
to the camera. We need to make it a
-
child of the camera body.
-
In the Models folder locate prop_cctvCam_collision.
-
Drag and drop this on to prop_cctvCam_body
-
in the hierarchy.
-
We need to reposition it slightly.
-
The frustum should appear to be coming
-
from the lens. Set the position of this object
-
to (0, 0.15, 0.35).
-
The purpose of this game object is to show
-
where the camera is pointing and to detect
-
the player when he is in front of the camera.
-
To physically detect the player we're going
-
to use a trigger collider. Add a mesh
-
collider to the collision object that we
-
just added. Click the Add Component button
-
and choose Physics - Mesh Collider.
-
Then check the box for Is Trigger.
-
The frustum looks a little poor at the moment
-
so instead we're going to use a spot light
-
to show where the camera is pointing
-
and remove the unnecessary visual
-
components from the game object.
-
Remove the mesh filter by clicking on the
-
cog to the right of it, and choose Remove Component.
-
Do the same for the mesh renderer.
-
Before we create any visual effects
-
we want the camera to be pointing in the right direction.
-
We want to prevent the player from passing
-
through this space when the camera is facing
-
forward. So to completely cover the area
-
we're going to tilt the camera body
-
60 degrees in the X axis.
-
Select prop_cctvCam_body
-
and in the X axis for rotation
-
put in 60 degrees.
-
To visually represent the frustum we're going
-
to add a spotlight to it.
-
Reselect prop_cctvCam_collision
-
then go to Add Component - Rendering - Light.
-
Change the type to Spot.
-
Set the range to 9.5,
-
the angle to 90,
-
the colour to red.
-
We'll set the shadow type to Hard Shadow
-
with a bias of 0.02.
-
We don't want this light baked in if
-
we rebake the light map later.
-
So we need to set the light mapping setting
-
to Real Time Only.
-
This looks okay, but to improve the effect
-
we're going to add a cookie to the light.
-
A cookie is effectively a texture that
-
your light projects. The one we're going to
-
use is in the Textures folder and is called
-
FX_cameraview_ALP.
-
Drag and drop this on to the cookie
-
property of the light
-
and you should see a starlight pattern
-
projected on the floor. Now we need to make
-
a script to report the location of the player
-
when he's spotted by the CCTV camera.
-
We'll call it CCTVPlayerDetection
-
Click the Add Component button
-
and choose New Script and name it
-
CCTVPlayerDetection. Click Create and Add
-
and then double click the icon
-
to launch it for editing.
-
Remove the default functions and setup
-
your script for use. For this script we'll
-
need a couple of private class variables
-
to store references.
-
Firstly to the player so that we know it's
-
the player that we've detected.
-
Secondly we need a reference to the last
-
player sighting script on the game controller.
-
So that we can update the position variable
-
when we need to.
-
We will now use the Awake function to
-
allocate these references.
-
We can allocate the player reference using
-
FindGameObjectWithTag and parsing
-
the Player variable from our tag script.
-
We can also find the game controller
-
in the same way. We then need to use
-
GetComponent afterwards to get a reference
-
to the last player sighting script
-
on the game controller.
-
Now we're going to use OnTriggerStay
-
to detect the player.
-
Given that a collider will be detected
-
within the trigger zone, we want to check if
-
the game object belonging to that collider
-
is the player.
-
We need to be careful at this point.
-
It could be that our player is on the other
-
side of a wall but still
-
intersecting the collider.
-
To resolve this we're going to
-
raycast from the lens of the camera
-
to the player to check the line of sight.
-
We'll start by finding the relative position
-
of the player from the camera
-
so that we have a direction to raycast in.
-
We will also need a raycastHit variable
-
to determine whether or not the raycast
-
intersects the player collider.
-
Using these we can raycast from the
-
camera towards the player and get back
-
information on what the raycast hits.
-
Next we need to check whether the game object
-
of the collider of whatever we hit
-
was the player.
-
If it was then we want to update the game
-
controller with the last sighting of the player.
-
This concludes the CCTV player detection script
-
so we need to save it and return to the editor.
-
At this point we want to drag our CCTV
-
camera in to the Prefabs folder
-
to save it as a prefab.
-
We can then duplicate this around our level
-
to make more cameras. Select the parent
-
prop_cctvCam and drop it on to the
-
Prefabs folder in the project panel.
-
Now let's duplicate this camera twice
-
for our other cameras in the scene.
-
We now have three in the same place.
-
Let's position one of them at (-21, 2.2, 2).
-
Then reselect it in the hierarchy and
-
press F to focus on it.
-
You should see that this is now on one
-
of the buildings in the lower left
-
of the game.
-
Now select one of the other CCTV cams
-
and place it at (-23, 1.8, 24).
-
Reselect it in the hierarchy and press F
-
with your mouse over the scene view to focus on it.
-
You should see that it's now on the wall
-
of the security department building
-
in the centre of the map.
-
This camera needs to spot the player coming
-
through the break in the fence.
-
So we will tilt it up in the X axis
-
in order to face the fence.
-
Drill down in to this object and select
-
prop_cctvCam_body
-
and set the rotation of X from 60
-
to 30. Select prop_cctvCam_collision.
-
We also need to lengthen the trigger
-
collider so that it reaches the fence.
-
So change the scale to (1, 1, 1.8).
-
We want the camera on the battle bus to be
-
animated, so we will return to working
-
on this camera now.
-
Reselect it and focus on it.
-
We want the camera on the battle bus to
-
be animated. But before we do this
-
we can notice that the intensity of the
-
spotlight is not enough.
-
This is the power of prefabs in Unity.
-
Because we now want to update all three
-
cameras with a brighter intensity
-
we can drill down in to any one of them.
-
Select prop_cctvCam_collision
-
and change the intensity.
-
Set it to a value of 4 and press return.
-
Then hit the apply button.
-
This saves the change out to the prefab
-
and now when we check our other cctv cams
-
we can see that they have a brighter intensity too.
-
Now let's animate our camera.
-
This camera should sweep back and forth.
-
We do this by creating a new animation asset.
-
Select the Animation folder in the project panel.
-
Click the Create button and choose Animation.
-
Call the animation cctv_sweep.
-
Now expand the prop_cctvCamera
-
and select prop_cctvCam_Joint.
-
Drag and drop this animation asset
-
on to the object in the inspector.
-
This adds an animation component
-
and assigns this asset as the animation to play.
-
Now we need to open up Unity's animation
-
window. Go to Window - Animation.
-
We will dock this, along with our game
-
view at the bottom.
-
This allows us to see the animation we are doing
-
in the scene view and work on the animation
-
at the same time. We want the camera
-
to rotate around the Y axis.
-
So we will click on Rotation.Y and select Add Curves.
-
To the right of Rotation.Y
-
click and choose Add Curves from the menu that appears.
-
This will add rotations as curves on the
-
graph and gives a keyframe at the start
-
of the animation. We want the animation
-
to last about 2 seconds so we will add
-
another keyframe there.
-
Scrub the playhead to 2 seconds
-
and click the Add Keyframe button at the
-
top of the animation window.
-
Now set the value to 60.
-
All that's left to do is to make sure that
-
our animation loops. We do this by
-
changing the wrap mode at the bottom,
-
which is currently set to default.
-
Change this to Ping Pong.
-
This means that our animation will go from
-
0 to 60 and then head back to 0 in an infinite loop.
-
This completes the creation of our animation
-
and we can test it by pressing the play button
-
at the top of the interface.
-
You should note that when our camera is
-
rotated there is a gap around which
-
the player can sneak.
-
This concludes the creation of our CCTV camera
-
and the end of this assignment.
-
In the next assignment we will look at
-
creating laser gates, which are designed to
-
detect the player also.
-
Remember to save your scene
-
and save the project before moving on.