< Return to Video

Stealth game tutorial - 107 - CCTV Cameras - Unity Official Tutorials

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

more » « less
Video Language:
Portuguese
Duration:
11:32

English subtitles

Revisions