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


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. Subtitles by the Amara.org community

31 thoughts on “Stealth game tutorial – 107 – CCTV Cameras – Unity Official Tutorials

  1. this was done just 2 weeks after I send the script of this type in the Asset Store, until now I realize the reason that no I approved it.

  2. Just completed the tutorial in Unity4.2 Free. Had to make a small change to the CCTVPlayerDetection script to fix a prob when killed in camera detection zone. I'm using JS, but I'm sure you can work out the C#

    Put this in the variables
    private var playerHealth : PlayerHealth;

    Put this in Awake()
    playerHealth = player.GetComponent(PlayerHealth);

    Make this change to OnTriggerStay function.
    change if(other.gameObject == player) to if(other.gameObject == player && playerHealth.health >0f)

  3. I have a bug with last free version of Unity in CCTVPLayerDetection.cs at the line 7 :
    private LastPlayerSighting lastPlayerSighting; error compiler : the type or namespace LastPlayerSighting can't be found.
    Can you help me please ?

  4. Great series so far. I'm using Unity 4.3 and I cannot add the animation as shown in this video. When I attempt to add it a different way I get warnings about it not being legacy and nothing plans. Anyone got any advice?

  5. For anyone doing this tutorial on 4.3 and does not know what the fuck to do:

    In order to make the animation loop, click CCTvSweep animation and in the animation inspector, check "Loop Time".

    Once you are in the animation window, click add curve->transform->rotation

    In the dope sheet tab, click, the graph at 2:00 and set Rotation.y to 60, after that click the grapth at 4:00 and set the Rotation.y to 0:00, also making sure that you have an initial state set to 0:00.

  6. In Unity 4.3 they made some changes to the animations. To change your animation to a legacy one, select the animation from your project tab and in the inspector right click the inspector tab to choose debug mode. Now you should see a Animation Type property. Change that to 1.

  7. The animation works, but I get this message in the console as well:

    "'prop_cctvCam_joint' AnimationEvent has no function name specified!"

  8. I've read the comments and see that there are answers to people trying to do the animation in the new version, but I'm curious: what would be the ideal way to do the animation using the new system? Clearly it's not intended to work the old way anymore so I'd like to learn it the new way…

  9. Using Unity 4.5   The Animation screens are different from the Tutorial. There does not seem to be a setting for Wrap Mode any longer.  I can't seem to find where to set this in any of the Unity documentation.  Please update!!!!

  10. Unity 4.5 no Ping Pong option, do this: In the animation window Add a Key at 4 seconds. In that key set the Y rotation to 0 –> In the Project window click on the animation you created (cctvSweep) and place a check  on Loop Time –> Play it and you will see it working as shown

  11. For anyone struggling with the animations in Unity 4.5 here is an easy way to do it (avoiding the legacy problem):
     
    Before you create the CCTVSweep animation file, open up the animation window under the window tab (not the animator window). With the camJoint child object selected, click add Curve in the animation window. Navigate to the animations folder and save your file there as CCTVSweep. (I don't know why that would make a difference but it stopped the legacy warning coming up for me.) 

    You can click add Curve again and select Transform > Rotation. This will give you an animation clip. You can expand its properties using the drop down arrow to the left of the clip and its rotation.y you want to be working with. You can pretty much follow the tutorial from there. (I made a 16s clip, with keyframes (time,rot.y): (0,60), (8,0) and (16,60) to create the panning.)

    Also for people complaining about not having the ping pong feature you can simulate the smooth motion by going into the curves tab, and right clicking on the start and end key frames and selecting "free smooth". You can grab the key frame and fashion the curve into a cosine wave. (y)

  12. How to solve Wrap Mode problem:
    (Thanks for: http://forum.unity3d.com/threads/4-3-0-missing-wrap-mode-dropdown-in-animation-window.212352/)
    1. In Model Folder select prop_cctvCam
    2. Go to Rig tab. Change Animation Type to Legacy
    3. Click Apply
    4. In Hierarchy select prop_cctvCam_joint
    5. Add Animation Component
    6. In Animation window click Add Curve > Transform > Rotation. In appeared window save Animation Clip as cctvSweep.anim
    7. Make rotation animation like in video
    8. In Animation folder select cctvSweep.anim 
    9. In Inspector change Wrap Mode to Ping Pong

  13. stupid tutorial was a waste of time. I found the wrap -> ping pong and it doesn't even work. I eventually just did 0 to 60 to 0 with three key frames.

  14. En Unity 4.6 y gracias al tutorial y a los buenos comentarios de los compañeros acá abajo, describo mi solución.
    1. Seleccione Prop_cctvCam_joint.
    2. Add Component – Miscellaneous – Animation.
    3. Abre la ventana de Animation Ctrl+6 en PC.
    4. Add Curve – guardar la animacion y agregar transform- rotation.
    5. Hacer la animación como en el video.
    6. seleccione la animación y en el inspector cambie Wrap mode: Default por Ping Pong.
    7. Seleccione Prop_cctvCam_joint y Arrastre la animación al animation component en la propiedad Animation.
    8. guarde y presione play.
    Nota: Si presenta errores ubíquese en la carpeta Models – Prop_cctvCam – Rig en el inspector – Animation Type: Legacy – Apply y repita el proceso.

  15. Using Unity 5, and for the camera animation I had to do the following, you will get an animator and a animation file, click Add Property and add the transition rotation properties and at 2 seconds i changed the y to 60 and at 4 sec i changed it back to zero, and of course at 0 sec it is also at zero, i did this because i could not find ping pong.  I also slowed down the speed of the animation on the animator to .5 to create a slower camera.

  16. In Unity 4.6, to find Wrap Mode = Ping Pong

    1. Highlight cctvSweep
    2. Click on the Inspector tab
    3. Wrap Mode -> Ping Pong

  17. Question to the video editor: Will this script detect the character if let's say… he's hiding behind a wall but only his toe is visible to the camera? I made a similar, yet simpler, script and that was the only problem, the character was only detected when the actual center of the character was visible to camera. To my understanding by reading your script, it seems like it might have the same behavior as mine? I am wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *