1 00:00:02,240 --> 00:00:04,030 So we have previously set 2 00:00:04,030 --> 00:00:06,030 the player up so we can see that the model 3 00:00:06,030 --> 00:00:09,160 has a gun built in, so let's go ahead 4 00:00:09,160 --> 00:00:11,160 and get the player to 5 00:00:11,160 --> 00:00:13,160 a point where the player can defend themselves. 6 00:00:13,160 --> 00:00:15,160 What we're going to do is give the enemy the ability to 7 00:00:15,160 --> 00:00:17,160 have health and then we're going to give the player 8 00:00:17,160 --> 00:00:19,160 the ability to take that health away. 9 00:00:19,160 --> 00:00:22,410 So we're going to go through the enemy health script next. 10 00:00:22,410 --> 00:00:24,410 So we'll start off by finding it in 11 00:00:24,410 --> 00:00:26,410 Scripts - Enemy folder. 12 00:00:26,410 --> 00:00:28,410 Then we can drag and drop that 13 00:00:28,410 --> 00:00:30,410 on to the Zombunny in the hierarchy. 14 00:00:30,410 --> 00:00:32,900 So that's going to apply the script for us. 15 00:00:34,810 --> 00:00:38,510 On the Zombunny, so we can see that there's 16 00:00:39,150 --> 00:00:40,740 a Death clip 17 00:00:41,380 --> 00:00:43,000 which we're going to apply. 18 00:00:43,540 --> 00:00:45,540 Use the circle select button 19 00:00:47,330 --> 00:00:50,370 and that's opened up the context sensitive menu so we can 20 00:00:50,370 --> 00:00:53,320 find Zombunny Death. Apply that. 21 00:00:54,480 --> 00:00:56,480 So now everything's setup for us, we can 22 00:00:56,480 --> 00:00:58,480 edit that script or view it 23 00:00:58,480 --> 00:01:00,480 and see what's going on in that. 24 00:01:00,930 --> 00:01:02,930 Double click the icon to open it. 25 00:01:04,710 --> 00:01:08,280 As always we've got our public variables at the top. 26 00:01:08,280 --> 00:01:10,960 And very similar to the player's health we've got 27 00:01:10,960 --> 00:01:12,690 startingHealth and a currentHealth. 28 00:01:12,690 --> 00:01:14,690 They work exactly the same as the player. 29 00:01:15,810 --> 00:01:18,490 The next one is the sinkSpeed. 30 00:01:18,490 --> 00:01:21,260 When these enemies die 31 00:01:21,260 --> 00:01:23,260 it looks a bit funky to just have them 32 00:01:23,260 --> 00:01:25,710 lay there and then suddenly disappear. 33 00:01:25,710 --> 00:01:27,710 So what we're going to do is make them sink through 34 00:01:27,710 --> 00:01:29,710 the floor, so as soon as they've 35 00:01:29,710 --> 00:01:31,710 finished flopping over and dying they sink 36 00:01:31,710 --> 00:01:33,710 through the floor, and that's how fast 37 00:01:33,710 --> 00:01:35,310 we want them to sink through the floor. 38 00:01:35,310 --> 00:01:37,310 Later on in the day we're going to start doing 39 00:01:37,310 --> 00:01:39,310 scoring for this game and 40 00:01:39,310 --> 00:01:41,310 so each enemy needs to have a scoreValue, 41 00:01:41,310 --> 00:01:43,310 how much they increase our score by 42 00:01:43,310 --> 00:01:45,310 and that is the scoreValue. 43 00:01:45,910 --> 00:01:47,910 And we've got the deathClip 44 00:01:47,910 --> 00:01:49,910 that they play when they die. 45 00:01:49,910 --> 00:01:51,910 We've got some private variables 46 00:01:51,910 --> 00:01:55,260 starting off with the animator component reference. 47 00:01:55,260 --> 00:01:57,260 Then we've got a reference to the 48 00:01:57,260 --> 00:01:59,950 audio source, we've also got a reference 49 00:01:59,950 --> 00:02:00,730 to the hit particles. 50 00:02:00,730 --> 00:02:03,520 If you remember we dragged that on as a prefab 51 00:02:03,520 --> 00:02:05,520 and applied it as a child object. 52 00:02:05,850 --> 00:02:08,810 Likewise we've got a capsule collider reference. 53 00:02:09,600 --> 00:02:12,860 Then we've got a pair of boolean variables. 54 00:02:12,860 --> 00:02:16,270 We've got IsDead, which works exactly the same as with the player. 55 00:02:16,270 --> 00:02:19,890 And IsSinking, so they don't immediately start sinking 56 00:02:19,890 --> 00:02:22,760 because we want to see the animation so we need to have 57 00:02:22,760 --> 00:02:24,760 separate bools to determine 58 00:02:24,760 --> 00:02:27,290 whether or not they're sinking and whether they're dead. 59 00:02:27,860 --> 00:02:30,580 Next we've got our awake function. 60 00:02:31,210 --> 00:02:33,390 Which is going to setup our references as usual. 61 00:02:33,950 --> 00:02:37,660 The first two, as we've seen before, GetComponent, 62 00:02:37,660 --> 00:02:40,470 the type of the component that we're going to find 63 00:02:40,470 --> 00:02:42,470 animator, audio source, 64 00:02:42,470 --> 00:02:44,470 but with hitParticles we need to 65 00:02:44,470 --> 00:02:48,460 find a component in the child object hitParticles. 66 00:02:48,840 --> 00:02:51,150 So what GetComponentInChildren will do 67 00:02:51,150 --> 00:02:53,150 will go through all of the children 68 00:02:53,150 --> 00:02:55,150 game object and then find 69 00:02:55,150 --> 00:02:57,720 the first particle system and return that. 70 00:02:57,720 --> 00:03:00,420 Again, we've got GetComponent to find the capsule collider. 71 00:03:01,210 --> 00:03:03,210 Then at the end of the awake function 72 00:03:03,210 --> 00:03:05,210 we're setting the current health to the starting health. 73 00:03:05,960 --> 00:03:07,960 In update 74 00:03:08,340 --> 00:03:10,840 all we're doing is we're checking whether or not 75 00:03:11,180 --> 00:03:13,180 the enemy is supposed to be sinking or not. 76 00:03:13,180 --> 00:03:17,070 If it is sinking then we're going to translate 77 00:03:17,070 --> 00:03:19,070 the transform, and that means just move it. 78 00:03:20,400 --> 00:03:22,400 So we're going to move it in a negative-up 79 00:03:22,400 --> 00:03:24,400 direction, down. 80 00:03:24,400 --> 00:03:27,280 and we're going to do that by the sinkSpeed 81 00:03:27,280 --> 00:03:31,000 per second, so that's that Time.DeltaTime thing. 82 00:03:31,620 --> 00:03:34,080 If we do that then we're moving per second 83 00:03:34,080 --> 00:03:35,620 instead of per frame. 84 00:03:35,620 --> 00:03:37,340 So just a quick note about translate. 85 00:03:37,340 --> 00:03:39,340 Previously we used move position to move 86 00:03:39,340 --> 00:03:41,340 this thing but we're going to be no longer using 87 00:03:41,340 --> 00:03:43,340 physics when the enemies die so 88 00:03:43,340 --> 00:03:45,340 we can go ahead and use translate without 89 00:03:45,340 --> 00:03:47,340 worrying about losing sync with physics. 90 00:03:48,790 --> 00:03:52,000 Next we've got, again very similar 91 00:03:52,000 --> 00:03:54,000 function to the player's health we've got 92 00:03:54,000 --> 00:03:56,000 a public function, so again that means it 93 00:03:56,000 --> 00:03:57,930 can be called from another function 94 00:03:57,930 --> 00:03:59,450 and that's where we'll be calling it from. 95 00:03:59,450 --> 00:04:00,090 Another script. 96 00:04:00,090 --> 00:04:01,190 Another script, sorry. 97 00:04:01,770 --> 00:04:03,770 But this time we've got the integer 98 00:04:03,770 --> 00:04:05,770 of how much damage is going to be taken 99 00:04:05,770 --> 00:04:08,360 but also the hitPoint, 100 00:04:08,360 --> 00:04:10,360 so where has it been it? 101 00:04:10,360 --> 00:04:12,360 And we'll be using that hitPoint to 102 00:04:12,360 --> 00:04:14,880 move the particle system around the enemy 103 00:04:14,880 --> 00:04:17,380 so that fluff is flying out wherever it gets hit. 104 00:04:19,000 --> 00:04:21,000 Right, so the first thing we want to do in this function 105 00:04:21,000 --> 00:04:24,710 is check if the enemy is dead. 106 00:04:24,710 --> 00:04:26,710 If it is dead then we don't need to do 107 00:04:26,710 --> 00:04:28,710 anything so we're going to return out of this function. 108 00:04:29,670 --> 00:04:32,170 Assuming we are not dead, or the enemy is not dead 109 00:04:32,170 --> 00:04:34,170 we can continue on with this function. 110 00:04:34,170 --> 00:04:36,170 Then since we've taken damage we want to play 111 00:04:36,170 --> 00:04:37,890 the Hurt sound effect. 112 00:04:38,490 --> 00:04:40,490 We'll then lose the amount of health 113 00:04:40,490 --> 00:04:42,050 from our current health. 114 00:04:42,900 --> 00:04:44,900 Next what we're going to do is 115 00:04:44,900 --> 00:04:48,650 find the hitParticles, so that's the particle system 116 00:04:48,650 --> 00:04:51,230 Find the transform that that is on 117 00:04:51,230 --> 00:04:54,200 and move that position to the hitPoint. 118 00:04:54,790 --> 00:04:57,810 So we've got a child game object 119 00:04:57,810 --> 00:05:00,730 finding that position and moving it to 120 00:05:00,730 --> 00:05:02,550 where ever we've been hit. 121 00:05:02,550 --> 00:05:04,790 So note that we haven't actually defined the hitPoint 122 00:05:04,790 --> 00:05:07,660 but it's going to be passed in to this TakeDamage function 123 00:05:07,660 --> 00:05:10,320 using that second argument, this vector3. 124 00:05:10,320 --> 00:05:13,380 So we're going to send it wherever we call TakeDamage 125 00:05:13,380 --> 00:05:15,030 and you'll see that a little later on. 126 00:05:15,840 --> 00:05:17,840 Okay, so after we've moved 127 00:05:17,840 --> 00:05:19,840 the position of the particle system 128 00:05:19,840 --> 00:05:23,100 we can then play the particle system, so the fluff starts flying out. 129 00:05:26,500 --> 00:05:28,500 Last in this function, we're going to check 130 00:05:28,500 --> 00:05:30,500 if our current health is less than 131 00:05:30,500 --> 00:05:32,500 or equal to 0. 132 00:05:32,500 --> 00:05:35,750 If we've run out of health then we'll use the Death function. 133 00:05:36,810 --> 00:05:38,080 So the Death function. 134 00:05:39,420 --> 00:05:42,360 First of all we set isDead equals to true. 135 00:05:43,330 --> 00:05:47,170 Then we set the capsule collider to a trigger. 136 00:05:47,170 --> 00:05:49,170 So what that means is because you don't 137 00:05:49,170 --> 00:05:51,520 actually physically hit triggers 138 00:05:51,890 --> 00:05:55,160 if the player is running along mowing down enemies 139 00:05:55,810 --> 00:05:57,810 then when they die they won't become 140 00:05:57,810 --> 00:05:59,810 an obstacle any more, it can keep on moving 141 00:05:59,810 --> 00:06:01,230 and keep on mowing through them. 142 00:06:02,750 --> 00:06:04,750 We set the animator trigger Dead 143 00:06:04,750 --> 00:06:06,750 so the enemy knows it's dead 144 00:06:06,750 --> 00:06:08,750 it performs it's Dead animation. 145 00:06:09,570 --> 00:06:11,570 And lastly we're going to set the audio 146 00:06:11,570 --> 00:06:14,150 source to play the Death clip. 147 00:06:14,150 --> 00:06:16,150 So we change the clip that it's got to play to Death 148 00:06:16,150 --> 00:06:17,770 and then make it play. 149 00:06:17,770 --> 00:06:19,770 Okay, so we've got a public function here 150 00:06:19,770 --> 00:06:21,970 called StartSinking and we'll discuss why it's 151 00:06:21,970 --> 00:06:23,970 public in a minute but for now we'll just 152 00:06:23,970 --> 00:06:25,380 go through what it does. 153 00:06:26,450 --> 00:06:28,450 In this we're going to find references 154 00:06:28,450 --> 00:06:29,920 to the nav mesh agent 155 00:06:30,710 --> 00:06:32,710 and disable it. 156 00:06:32,710 --> 00:06:34,710 And then we're going to find a reference to the 157 00:06:34,710 --> 00:06:38,080 rigidbody component and set it to isKinematic 158 00:06:38,080 --> 00:06:41,100 The reason we're going to set it to kinematic is 159 00:06:41,100 --> 00:06:43,100 that when you move a collider 160 00:06:43,100 --> 00:06:45,100 in the scene Unity will try and 161 00:06:45,100 --> 00:06:47,100 recalculate all the static geometry 162 00:06:47,100 --> 00:06:49,880 because it thinks 'okay, the level's changed, 163 00:06:49,880 --> 00:06:51,880 I need to rethink about this. 164 00:06:52,530 --> 00:06:54,530 But if you've got a kinematic rigidbody 165 00:06:54,530 --> 00:06:57,960 and you're translating this object 166 00:06:57,960 --> 00:07:00,860 then it will ignore it, so that's why we're doing that. 167 00:07:01,290 --> 00:07:05,230 Real quick here, we see GetComponent 168 00:07:05,230 --> 00:07:07,230 .enabled = false; 169 00:07:07,230 --> 00:07:09,230 so if we were trying to turn 170 00:07:09,230 --> 00:07:11,230 off a game object 171 00:07:11,230 --> 00:07:14,640 we say .setActive 172 00:07:14,640 --> 00:07:16,640 and in parenthesis say false. 173 00:07:16,640 --> 00:07:18,640 Nav mesh agent's a component so we say 174 00:07:18,640 --> 00:07:20,640 .enabled, so just keep that in mind. 175 00:07:20,640 --> 00:07:23,690 If you see .setActive = false 176 00:07:23,690 --> 00:07:25,690 that's a game object and we're turning the whole 177 00:07:25,690 --> 00:07:27,090 game object off. 178 00:07:27,090 --> 00:07:29,590 Here we're doing .enabled = false 179 00:07:29,590 --> 00:07:31,590 That means I'm not turning off the whole game object 180 00:07:31,590 --> 00:07:35,340 just this one component of that game object. 181 00:07:36,210 --> 00:07:39,670 Since we're starting to sink isSinking is true. 182 00:07:39,670 --> 00:07:43,300 And lastly we're going to destroy the game object 183 00:07:43,300 --> 00:07:46,020 after 2 seconds, so basically 184 00:07:46,020 --> 00:07:48,020 it's started sinking, it's going through the floor, 185 00:07:48,020 --> 00:07:50,020 after 2 seconds we're not going to see it any more 186 00:07:50,020 --> 00:07:52,370 we can get rid of it, so we'll destroy the game object. 187 00:07:52,370 --> 00:07:53,540 And that's the end of that function 188 00:07:53,540 --> 00:07:55,270 and the end of this script as well. 189 00:07:55,270 --> 00:07:57,270 When we're done with that we can hop back over 190 00:07:57,270 --> 00:07:59,270 in to Unity here. 191 00:08:00,850 --> 00:08:03,640 The enemy now has health and so one of the 192 00:08:03,640 --> 00:08:05,640 things that we want to do is we 193 00:08:05,640 --> 00:08:08,090 want to make the enemy's 194 00:08:08,090 --> 00:08:10,090 ability to attack dependent on whether or not 195 00:08:10,090 --> 00:08:11,550 the enemy is alive. 196 00:08:11,550 --> 00:08:13,550 Sorry to interrupt you Mike, I've just remembered that we've 197 00:08:13,550 --> 00:08:15,550 missed something out. 198 00:08:15,550 --> 00:08:17,550 So we had that StartSinking function 199 00:08:17,550 --> 00:08:19,550 and it was public 200 00:08:19,550 --> 00:08:21,550 but we never called it. 201 00:08:21,550 --> 00:08:22,950 The reason we never called it is because 202 00:08:22,950 --> 00:08:24,710 it's on an animation event. 203 00:08:25,410 --> 00:08:28,600 So all these enemies, they have an animation event 204 00:08:28,600 --> 00:08:30,600 where they flop and then die. 205 00:08:31,380 --> 00:08:33,960 And what we can do in Unity is say 206 00:08:33,960 --> 00:08:35,960 somewhere along the line of that animation 207 00:08:35,960 --> 00:08:37,960 we're going to say 'at this point 208 00:08:37,960 --> 00:08:40,610 try and look for this function 209 00:08:40,610 --> 00:08:43,270 on the game object, somewhere on the game object 210 00:08:43,270 --> 00:08:46,010 there will be a function called StartSinking. 211 00:08:46,320 --> 00:08:48,320 So it'll look for that and then if 212 00:08:48,320 --> 00:08:50,320 it finds it it'll play that function. 213 00:08:51,100 --> 00:08:53,100 This was setup already. 214 00:08:53,100 --> 00:08:55,100 Yes, this isn't something that you have to do, 215 00:08:55,100 --> 00:08:57,100 this is something that we've setup for you. 216 00:09:00,130 --> 00:09:03,670 So this is something that is already there, 217 00:09:03,670 --> 00:09:05,670 and basically what this says is at this 218 00:09:05,670 --> 00:09:07,670 mark, which is something that is already in the animation 219 00:09:07,670 --> 00:09:09,050 it's not anything that you guys have to do, 220 00:09:09,050 --> 00:09:11,050 it's going to attempt to call a method called 221 00:09:11,050 --> 00:09:12,670 StartSinking. 222 00:09:12,670 --> 00:09:14,670 Now up until this point there has been 223 00:09:14,670 --> 00:09:15,790 no such method, alright. 224 00:09:15,790 --> 00:09:17,790 But now we've created one and we've added 225 00:09:17,790 --> 00:09:19,790 it so now it knows 226 00:09:19,790 --> 00:09:21,790 I have a method called StartSinking and 227 00:09:21,790 --> 00:09:23,790 that will happen when it's time comes. 228 00:09:25,700 --> 00:09:27,700 Normally if the animation were to play 229 00:09:27,700 --> 00:09:29,700 and that function were to not be there you would 230 00:09:29,700 --> 00:09:31,700 get an error saying 'hey, I'm trying to call this function 231 00:09:31,700 --> 00:09:32,820 and one does not exist. 232 00:09:32,820 --> 00:09:34,820 We did not get an error because we have yet to 233 00:09:34,820 --> 00:09:36,970 have any way of killing the enemy. 234 00:09:36,970 --> 00:09:38,970 If we had we would have seen an error but 235 00:09:38,970 --> 00:09:40,970 if we don't then we do not see an error. 236 00:09:40,970 --> 00:09:45,050 The function was public and so this animation will 237 00:09:45,050 --> 00:09:47,960 automatically call that function when the time comes. 238 00:09:47,960 --> 00:09:49,960 Animation events are really useful, 239 00:09:49,960 --> 00:09:52,500 you can use them for all kinds of things. 240 00:09:52,500 --> 00:09:54,500 A real common use would be footsteps 241 00:09:54,500 --> 00:09:56,500 if you had a single function to call a 242 00:09:56,500 --> 00:09:58,500 footstep at a particular point in an animation 243 00:09:58,500 --> 00:10:00,500 because obviously you can pick a particular frame 244 00:10:00,500 --> 00:10:02,000 where you want this to happen. 245 00:10:02,000 --> 00:10:04,320 We've got this fairly roughly placed 246 00:10:04,320 --> 00:10:06,320 and it just means that as soon as he just leaps up 247 00:10:06,320 --> 00:10:08,320 we'll start sending him down as well 248 00:10:08,320 --> 00:10:10,320 as his falling animation, 249 00:10:10,320 --> 00:10:12,320 which actually means that we're not getting the bounce on 250 00:10:12,320 --> 00:10:14,320 on the floor, but we could move it along if we wanted 251 00:10:14,320 --> 00:10:16,320 to but we're not going to bother doing that, 252 00:10:16,320 --> 00:10:18,180 you can play around with that later on. 253 00:10:20,020 --> 00:10:22,020 Now that we have the Enemy Health script 254 00:10:22,020 --> 00:10:24,250 it's important for us to pair the Enemy Attack script 255 00:10:24,250 --> 00:10:26,250 to that so that the enemy does not 256 00:10:26,250 --> 00:10:28,250 attack once they're already dead. 257 00:10:28,250 --> 00:10:30,710 What we'd like to do is edit 258 00:10:30,710 --> 00:10:33,540 the Enemy Attack script and uncomment 259 00:10:33,540 --> 00:10:35,550 those lines that we previously saw commented. 260 00:10:35,550 --> 00:10:37,550 Now we could locate the script in the project view 261 00:10:37,550 --> 00:10:38,940 and double click it and open it 262 00:10:38,940 --> 00:10:40,940 but I just want to run through another way that we 263 00:10:40,940 --> 00:10:42,940 could open that script. 264 00:10:42,940 --> 00:10:46,050 What I'm going to do is click on the Zombunny, 265 00:10:46,050 --> 00:10:48,050 and I am going to find the 266 00:10:48,050 --> 00:10:50,900 Enemy Attack script, the Enemy Attack Script. 267 00:10:50,900 --> 00:10:55,080 And of the script components the first property is always 268 00:10:55,080 --> 00:10:57,080 the script itself. 269 00:10:57,080 --> 00:10:59,080 Every script component has a first property called 270 00:10:59,080 --> 00:11:02,140 Script and the value of that is itself. 271 00:11:02,140 --> 00:11:04,140 So if I click on it what it does is highlight 272 00:11:04,140 --> 00:11:06,140 for me in the project view that script. 273 00:11:06,140 --> 00:11:07,500 So I'm like 'hey, which script is this?', 274 00:11:07,500 --> 00:11:08,860 you click on it, there it is. 275 00:11:08,860 --> 00:11:10,860 But I can then double click on it here 276 00:11:10,860 --> 00:11:12,860 inside this property and it again will 277 00:11:12,860 --> 00:11:14,590 open up inside Mono Develop. 278 00:11:14,590 --> 00:11:16,590 So that's another way to open the script up. 279 00:11:16,590 --> 00:11:18,590 So this is now a part that you're all going to want to 280 00:11:18,590 --> 00:11:20,590 follow along with because we're now going to remove 281 00:11:20,590 --> 00:11:22,590 this commenting while we explain what 282 00:11:22,590 --> 00:11:24,010 the comments were for. 283 00:11:24,010 --> 00:11:26,010 I'm going to come up to the top here 284 00:11:26,010 --> 00:11:28,010 and the first bit of commenting I see 285 00:11:28,010 --> 00:11:30,010 is a commented out variable 286 00:11:30,010 --> 00:11:32,740 that references EnemyHealth. 287 00:11:32,740 --> 00:11:34,740 Obviously we had to comment that out before 288 00:11:34,740 --> 00:11:37,060 because EnemyHealth didn't exist on the enemy yet 289 00:11:37,060 --> 00:11:38,850 so we had to keep that commented out. 290 00:11:38,850 --> 00:11:40,850 Now we're going to uncomment it so we can now have a 291 00:11:40,850 --> 00:11:43,360 reference to the enemy health script. 292 00:11:43,360 --> 00:11:45,010 Next we're going to go to our awake function, 293 00:11:45,010 --> 00:11:46,730 and again, since we have this variable, 294 00:11:46,730 --> 00:11:48,730 we now have to get that reference and we dothat 295 00:11:48,730 --> 00:11:51,630 by saying enemyHealth = GetComponent, 296 00:11:51,630 --> 00:11:54,090 and the component name is the name of the script, 297 00:11:54,090 --> 00:11:56,090 which is EnemyHealth. 298 00:11:56,790 --> 00:11:58,790 And then we're going to come down here towards the bottom 299 00:11:58,790 --> 00:12:01,110 and we're going to notice this. 300 00:12:01,110 --> 00:12:05,290 And this is not the commenting you've seen before. 301 00:12:05,290 --> 00:12:07,290 There we go, it's about as close as it'll let me get. 302 00:12:07,290 --> 00:12:10,430 So this is called a Block Comment 303 00:12:10,430 --> 00:12:12,430 and what block comments enable me to do is 304 00:12:12,430 --> 00:12:14,430 comment many, many, many lines 305 00:12:14,430 --> 00:12:17,240 or pieces inside a line 306 00:12:17,240 --> 00:12:19,240 or whatever, right? So what we have here 307 00:12:19,240 --> 00:12:21,240 is what's called a Block Comment. 308 00:12:21,240 --> 00:12:23,240 And we can see that here's 309 00:12:23,240 --> 00:12:25,170 the start of the block comment, 310 00:12:25,170 --> 00:12:26,940 and here is the end of the block comment. 311 00:12:26,940 --> 00:12:32,410 So it's / all the way through to /. 312 00:12:32,410 --> 00:12:34,410 And anything between there is commented out. 313 00:12:34,410 --> 00:12:36,410 And you'll see that these parentheses 314 00:12:36,410 --> 00:12:38,620 or parenthesis is not commented out 315 00:12:38,620 --> 00:12:40,620 because only the things between the block 316 00:12:40,620 --> 00:12:41,840 comments get commented out. 317 00:12:41,840 --> 00:12:44,360 So what we want to do is remove the block comment. 318 00:12:44,360 --> 00:12:46,360 So when we start to remove this 319 00:12:46,360 --> 00:12:47,970 we'll see everything gets commented out and then 320 00:12:47,970 --> 00:12:49,210 nothing's commented out. 321 00:12:49,210 --> 00:12:51,210 So what this line says now 322 00:12:51,210 --> 00:12:52,970 if you recall previously it says 323 00:12:52,970 --> 00:12:54,970 'hey, if it's time to attack 324 00:12:54,970 --> 00:12:56,970 and the player is in range 325 00:12:56,970 --> 00:12:58,540 we attack the player'. 326 00:12:58,540 --> 00:13:00,540 Well there's one more step, one more decision 327 00:13:00,540 --> 00:13:02,200 you have to make, we have to say 328 00:13:02,200 --> 00:13:03,780 'if it's time to attack 329 00:13:03,780 --> 00:13:06,400 and the player is in range 330 00:13:06,400 --> 00:13:08,220 and we're not dead' 331 00:13:08,220 --> 00:13:10,060 and at that point we attack. 332 00:13:10,060 --> 00:13:12,060 So at this point now with all of that stuff 333 00:13:12,060 --> 00:13:14,060 uncommented now the enemy will attack the player 334 00:13:14,060 --> 00:13:16,060 correctly like they had been and they'll 335 00:13:16,060 --> 00:13:17,750 also stop when they're dead, 336 00:13:17,750 --> 00:13:21,710 which is obviously an important part of this whole dynamic. 337 00:13:21,710 --> 00:13:23,710 Be sure to get that stuff done. When you're finished 338 00:13:23,710 --> 00:13:25,320 be sure to save, 339 00:13:25,320 --> 00:13:27,320 save the script and return, 340 00:13:27,320 --> 00:13:29,850 which will bring us back in to Unity. 341 00:13:31,030 --> 00:13:35,560 The enemy is cool, we can leave it alone for now. 342 00:13:35,560 --> 00:13:37,560 Now let's go ahead and get this 343 00:13:37,560 --> 00:13:39,080 player ready to rock and roll. 344 00:13:39,080 --> 00:13:40,990 There's a few things that we're going to need to do. 345 00:13:40,990 --> 00:13:42,990 The player already has this mesh and the mesh 346 00:13:42,990 --> 00:13:44,990 has a gun, so the player is already setup 347 00:13:44,990 --> 00:13:46,990 with his weapon of choice so what we want to 348 00:13:46,990 --> 00:13:49,860 do is we want to make it behave appropriately. 349 00:13:49,860 --> 00:13:51,110 So there's some stuff we want to add to it. 350 00:13:51,110 --> 00:13:53,680 The first is we're going to add a particle component 351 00:13:53,680 --> 00:13:55,680 which is going to enable the gun to 352 00:13:55,680 --> 00:13:58,120 spit fire, which makes it kinda fun. 353 00:13:58,120 --> 00:14:00,120 We're also going to add the ability for 354 00:14:00,120 --> 00:14:03,130 it to have sound so we can hear 355 00:14:03,130 --> 00:14:04,150 when the player is firing. 356 00:14:04,150 --> 00:14:06,150 We're going to add a light so that the player 357 00:14:06,150 --> 00:14:08,150 illuminates the scene when firing 358 00:14:08,150 --> 00:14:10,150 and then we're going to add a line renderer 359 00:14:10,150 --> 00:14:12,150 and the line renderer is actually going to be that 360 00:14:12,150 --> 00:14:14,150 line that we're firing out 361 00:14:14,150 --> 00:14:16,150 to make it look like we're firing bullets 362 00:14:16,150 --> 00:14:18,150 or lasers or whatever, 363 00:14:18,150 --> 00:14:19,820 and all of these things are going to make this 364 00:14:19,820 --> 00:14:23,310 a much more fun look and feel to the game. 365 00:14:23,310 --> 00:14:25,310 So what we're going to do is go to the prefabs folder 366 00:14:25,310 --> 00:14:28,240 and we're going to locate gunParticles. 367 00:14:28,240 --> 00:14:31,300 Unlike the hitParticles of the enemy 368 00:14:31,300 --> 00:14:33,300 we are not going to click and drag this prefab 369 00:14:33,300 --> 00:14:35,300 anywhere near the player. 370 00:14:35,300 --> 00:14:37,300 Instead we are going to copy 371 00:14:37,300 --> 00:14:39,300 a component off of it. 372 00:14:39,300 --> 00:14:41,600 So we're not going to use this prefab 373 00:14:41,600 --> 00:14:43,600 we're just going to use a piece of this prefab. 374 00:14:43,600 --> 00:14:45,600 So if we look over here in the inspector 375 00:14:45,600 --> 00:14:47,600 we see the particle system that is 376 00:14:47,600 --> 00:14:50,620 a part of that gunParticles 377 00:14:50,620 --> 00:14:53,580 and I can click the cog, or gear 378 00:14:53,580 --> 00:14:57,110 right here and I can select Copy Component. 379 00:14:57,110 --> 00:14:59,110 I'm not actually interested in the game object or the 380 00:14:59,110 --> 00:15:01,570 prefab I'm interested in the component that's on it. 381 00:15:01,570 --> 00:15:03,570 Once I've copied that component 382 00:15:03,570 --> 00:15:05,570 I'm going to go to the player in my hierarchy 383 00:15:05,570 --> 00:15:07,450 and I'm going to expand it. 384 00:15:07,450 --> 00:15:10,520 And I'm going to look for GunBarrelEnd. 385 00:15:10,520 --> 00:15:12,520 Since the player has a collider 386 00:15:12,520 --> 00:15:14,870 and the player has all these pieces and parts 387 00:15:14,870 --> 00:15:16,870 I'm actually really interested in the 388 00:15:16,870 --> 00:15:18,870 tip of the gun so that I can 389 00:15:18,870 --> 00:15:20,870 align bullets and everything with 390 00:15:20,870 --> 00:15:22,870 the tip of the gun and get 391 00:15:22,870 --> 00:15:25,470 shooting to happen the way we want it to. 392 00:15:26,010 --> 00:15:29,290 With GunBarrelEnd not Gun, 393 00:15:29,290 --> 00:15:31,290 not Player, GunBarrelEnd selected 394 00:15:31,290 --> 00:15:33,290 in the hierarchy I need to 395 00:15:33,290 --> 00:15:35,590 locate a cog, there's always a cog 396 00:15:35,590 --> 00:15:37,590 in the upper right hand corner of the transform component. 397 00:15:38,270 --> 00:15:40,270 And I'm going to click that and I'm going to select 398 00:15:40,270 --> 00:15:42,270 Component As New. 399 00:15:42,270 --> 00:15:43,780 And there's my particles. 400 00:15:43,780 --> 00:15:45,780 So at this point the particles are 401 00:15:45,780 --> 00:15:47,780 on the end of the gun. 402 00:15:47,780 --> 00:15:49,410 Now they're not set to play. 403 00:15:49,410 --> 00:15:51,410 We going to use scripts to say when 404 00:15:51,410 --> 00:15:53,410 those particles should start firing off. 405 00:15:53,410 --> 00:15:56,400 So now we have the particle system on the gun. 406 00:15:56,400 --> 00:16:00,920 Now let's minimise the particle system by 407 00:16:00,920 --> 00:16:02,920 clicking the little arrow next to it 408 00:16:02,920 --> 00:16:04,920 because the particle system itself takes up so much space 409 00:16:04,920 --> 00:16:06,920 so we're just going to collapse that down 410 00:16:06,920 --> 00:16:08,620 so that we have some room to work. 411 00:16:08,620 --> 00:16:10,620 So we have our particle system and the next thing 412 00:16:10,620 --> 00:16:11,880 we want is a Line Renderer. 413 00:16:11,880 --> 00:16:13,880 The line renderer is going to be the visual 414 00:16:13,880 --> 00:16:15,880 component of shooting and we're going to add that 415 00:16:15,880 --> 00:16:18,670 by going to Add Component and I'm just going to type Line 416 00:16:18,670 --> 00:16:20,460 and find Line Renderer. 417 00:16:20,460 --> 00:16:21,730 I'm specifically typing in line because I can't 418 00:16:21,720 --> 00:16:23,240 remember where it is otherwise. 419 00:16:23,240 --> 00:16:25,240 Now a line renderer appears 420 00:16:25,240 --> 00:16:27,240 as a component in our game object 421 00:16:27,240 --> 00:16:29,240 and so what the line renderer is, like I say, 422 00:16:29,240 --> 00:16:31,240 it renders a line, and so there's some settings 423 00:16:31,240 --> 00:16:33,240 that we've got to do because as we can see right now 424 00:16:33,240 --> 00:16:35,240 that doesn't really look like a bullet and that doesn't 425 00:16:35,240 --> 00:16:36,410 really look like a gunshot, it just looks like 426 00:16:36,410 --> 00:16:38,160 a giant magenta block. 427 00:16:38,160 --> 00:16:40,790 So we need to set this up appropriately. 428 00:16:40,790 --> 00:16:42,790 The first thing we're going to do is 429 00:16:42,790 --> 00:16:45,950 expand the Materials drop down, and you can see here 430 00:16:45,950 --> 00:16:48,520 I can just expand that by clicking the Materials drop down. 431 00:16:48,520 --> 00:16:51,140 And I'm going to look for this Element 0. 432 00:16:51,140 --> 00:16:53,140 And right now the reason this is 433 00:16:53,140 --> 00:16:55,810 that hideous fuchsia colour is that it doesn't have 434 00:16:55,810 --> 00:16:57,870 a material, it doesn't know what it's supposed to look like. 435 00:16:57,870 --> 00:16:59,870 So what we want to do is give it 436 00:16:59,870 --> 00:17:01,870 a colour so we're going to use the 437 00:17:01,870 --> 00:17:03,610 circle selector, we're going to click on that, 438 00:17:03,610 --> 00:17:06,700 and we already have a material created. 439 00:17:06,700 --> 00:17:09,360 It's just a basic line renderer material 440 00:17:09,360 --> 00:17:12,650 which we have cleverly named LineRendererMaterial. 441 00:17:12,650 --> 00:17:14,650 So if you use the circle selector and look for 442 00:17:14,650 --> 00:17:17,150 that list you should see a line renderer material. 443 00:17:17,150 --> 00:17:19,150 Double click on that and it will apply it 444 00:17:19,150 --> 00:17:20,950 to the gun. 445 00:17:20,950 --> 00:17:25,400 What we want to do is and manage this line renderer better. 446 00:17:25,400 --> 00:17:27,400 As we can see it's still, while it's still a 447 00:17:27,400 --> 00:17:29,780 better colour, it's the colour of our laser 448 00:17:29,780 --> 00:17:33,230 it's still way too big, so we definitely want to manage this. 449 00:17:33,230 --> 00:17:35,230 I'll go ahead and collapse the material 450 00:17:35,230 --> 00:17:36,660 back down as we're done there. 451 00:17:36,660 --> 00:17:38,660 I'm going to expand Parameters and there's two things 452 00:17:38,660 --> 00:17:40,660 I'm interested in, the first is Start Width 453 00:17:40,660 --> 00:17:42,260 and the second is End Width. 454 00:17:42,260 --> 00:17:43,500 And what we're going to do is set these up 455 00:17:43,500 --> 00:17:44,910 to be the same value. 456 00:17:44,910 --> 00:17:46,910 Because they're the same value the laser will 457 00:17:46,910 --> 00:17:48,410 consider them to be the same size. 458 00:17:48,410 --> 00:17:50,410 If they were different values it would either flare out in a cone 459 00:17:50,410 --> 00:17:51,570 or narrow in or whatever. 460 00:17:51,570 --> 00:17:53,570 And so for each of these we're going to specify 461 00:17:53,570 --> 00:17:55,660 .05. 462 00:17:55,660 --> 00:17:59,280 That's not 0.5, that's .05. 463 00:17:59,280 --> 00:18:01,280 We're going to do that for both the start 464 00:18:01,280 --> 00:18:03,280 and the end width. 465 00:18:04,150 --> 00:18:06,150 Once that is done we are going 466 00:18:06,150 --> 00:18:09,050 to go ahead and disable this component. 467 00:18:09,050 --> 00:18:11,050 And the way that we're going to disable this component 468 00:18:11,050 --> 00:18:13,280 is with this checkbox right here. 469 00:18:13,280 --> 00:18:15,280 Right next to the name Line Renderer I'm 470 00:18:15,280 --> 00:18:16,050 going to turn it off 471 00:18:16,050 --> 00:18:18,050 And the reason I'm going to do that is the game doesn't 472 00:18:18,050 --> 00:18:20,050 start with you shooting, the game starts with you 473 00:18:20,050 --> 00:18:22,050 not shooting so I'm going to turn it off and only 474 00:18:22,050 --> 00:18:24,050 turn it on once the time comes that 475 00:18:24,050 --> 00:18:25,400 you have actually fired. 476 00:18:25,400 --> 00:18:27,400 Now when we fire the gun what we want to do is 477 00:18:27,400 --> 00:18:29,400 we want to have the gun flash and light things up 478 00:18:29,400 --> 00:18:31,400 and in order to do that we need a 479 00:18:31,400 --> 00:18:33,400 light so I'm going to go ahead and 480 00:18:33,400 --> 00:18:35,400 collapse Line Renderer and now I'm 481 00:18:35,400 --> 00:18:37,400 going to add a light component. 482 00:18:37,400 --> 00:18:40,580 So I'm going to click the Add Component button 483 00:18:41,270 --> 00:18:42,960 and I'm going to go to 484 00:18:42,960 --> 00:18:46,060 Rendering and I'm going to select Light. 485 00:18:46,750 --> 00:18:48,750 What that's going to do is add a light 486 00:18:48,750 --> 00:18:51,330 to my gun, which we can see there. 487 00:18:51,330 --> 00:18:53,330 What we want to do now is give this 488 00:18:53,330 --> 00:18:55,330 the settings that will be appropriate 489 00:18:55,330 --> 00:18:56,690 to this particular gun. 490 00:18:56,690 --> 00:18:59,680 So the first thing to do is choose a colour 491 00:18:59,680 --> 00:19:01,250 and we have this little eye dropper, 492 00:19:01,250 --> 00:19:03,250 we can use the eye dropper just like you'd expect, 493 00:19:03,250 --> 00:19:04,610 pick a colour out of something, 494 00:19:04,610 --> 00:19:06,610 or we can just click next to it 495 00:19:06,610 --> 00:19:09,340 and actually use the gradient/color picker 496 00:19:09,340 --> 00:19:10,590 to pick a color. 497 00:19:10,590 --> 00:19:12,590 I'm going to pick a yellowish color. 498 00:19:12,590 --> 00:19:15,120 If you want to pick a different color go for it. 499 00:19:15,120 --> 00:19:17,120 But I kind of want my light to be the same color 500 00:19:17,120 --> 00:19:19,120 as my line renderer so I'm going with yellow there. 501 00:19:20,020 --> 00:19:22,720 So now when we fire the weapon 502 00:19:22,720 --> 00:19:24,720 there will in fact be a yellow light. 503 00:19:24,720 --> 00:19:26,720 Now again like the line renderer we're not 504 00:19:26,720 --> 00:19:28,720 starting firing so we're going to 505 00:19:28,720 --> 00:19:30,720 disable the light. 506 00:19:31,210 --> 00:19:33,210 So we'll just uncheck it there 507 00:19:33,210 --> 00:19:35,210 and the light will be turned off until 508 00:19:35,210 --> 00:19:37,210 we turn it on when we fire. 509 00:19:37,210 --> 00:19:39,210 And so the last little bit we're going to 510 00:19:39,210 --> 00:19:41,420 add is we need an audio source 511 00:19:41,420 --> 00:19:44,250 so the gun can play the firing sound. 512 00:19:44,250 --> 00:19:47,080 So what we're going to do is click Add Component. 513 00:19:47,080 --> 00:19:50,750 We're going to choose Audio and Audio Source. 514 00:19:51,340 --> 00:19:54,970 Now once the audio source is on the game object, 515 00:19:54,970 --> 00:19:56,970 again just like we've done in the past we're going 516 00:19:56,970 --> 00:19:59,590 to use the circle select picker 517 00:19:59,590 --> 00:20:03,170 and we are going to locate Player GunShot. 518 00:20:03,170 --> 00:20:05,170 Double click to add that and we are going to 519 00:20:05,170 --> 00:20:07,170 uncheck Play On Wake. 520 00:20:07,170 --> 00:20:09,170 We also want to be sure 521 00:20:09,170 --> 00:20:11,170 that we do not have loop because 522 00:20:11,170 --> 00:20:13,170 that will just drive everybody nuts. 523 00:20:13,170 --> 00:20:15,570 So no play on awake, no looping, 524 00:20:15,570 --> 00:20:16,940 That would be completely unnecessary. 525 00:20:16,940 --> 00:20:19,640 At this point the gun is setup. 526 00:20:19,640 --> 00:20:21,640 So now we can add the scripts 527 00:20:21,640 --> 00:20:23,640 that's going to allow the player to attack the enemy. 528 00:20:23,640 --> 00:20:26,600 So to recap, the enemy has health, 529 00:20:26,600 --> 00:20:28,600 we've added the health script to the enemy which will 530 00:20:28,600 --> 00:20:31,620 control when the enemy can attack and how the enemy dies. 531 00:20:31,620 --> 00:20:35,880 The gun has particle effects, lights, line renderers and sound. 532 00:20:35,880 --> 00:20:37,880 Now the last piece of this puzzle 533 00:20:37,880 --> 00:20:39,880 is to give the player a script that's 534 00:20:39,880 --> 00:20:41,880 going to allow the player to actually 535 00:20:41,880 --> 00:20:44,720 fire the gun and harm the enemy. 536 00:20:44,720 --> 00:20:47,910 So in the Scripts - Player folder 537 00:20:49,660 --> 00:20:53,560 we're going to look for the PlayerShooting script. 538 00:20:53,560 --> 00:20:55,560 And just like the previous scripts we're going to 539 00:20:55,560 --> 00:20:57,950 click and drag this 540 00:20:59,310 --> 00:21:01,310 and we are going to place it on the GunBarrelEnd, 541 00:21:01,310 --> 00:21:03,950 not on the Player, 542 00:21:03,950 --> 00:21:05,310 on the GunBarrelEnd. 543 00:21:05,310 --> 00:21:07,910 So if we select the GunBarrelEnd we should see 544 00:21:07,910 --> 00:21:09,910 the PlayerShooting script there. 545 00:21:09,910 --> 00:21:11,910 So again ensure that it is not 546 00:21:11,910 --> 00:21:15,620 on the Player, it is on the GunBarrelEnd. 547 00:21:15,620 --> 00:21:17,480 And now let's go ahead and open up the PlayerShooting script. 548 00:21:17,480 --> 00:21:20,530 Save your scene first. And let's look at what this script does. 549 00:21:20,530 --> 00:21:22,530 The first thing we have is 550 00:21:22,530 --> 00:21:24,950 these public variable declarations here, 551 00:21:24,950 --> 00:21:27,040 the first is public in damagerPerShot 552 00:21:27,040 --> 00:21:29,040 and we can see that every bullet is going to do 553 00:21:29,040 --> 00:21:30,870 20 points of damage. 554 00:21:30,870 --> 00:21:34,030 We also have a public float timeBetweenBullets 555 00:21:34,030 --> 00:21:36,030 which again is going to control how quickly 556 00:21:36,030 --> 00:21:39,010 our gun can fire, obviously we reduce that value 557 00:21:39,010 --> 00:21:40,680 if we want to make our gun fire more quickly. 558 00:21:40,680 --> 00:21:42,680 Then we have public float range 559 00:21:42,680 --> 00:21:44,680 and that's how far bullets can go. 560 00:21:44,680 --> 00:21:47,280 In this case they're going to be able to go 100 units 561 00:21:47,280 --> 00:21:49,790 which is actually a really far distance. 562 00:21:49,790 --> 00:21:51,530 Then we've got some private variables here 563 00:21:51,530 --> 00:21:53,350 the first is a float timer, 564 00:21:53,350 --> 00:21:55,350 and the float timer, just like the enemy attack, 565 00:21:55,350 --> 00:21:56,880 it's going to keep everything in sync, 566 00:21:56,880 --> 00:21:59,560 it'll make sure we can only attack when the time is right. 567 00:21:59,560 --> 00:22:02,970 Then we have a Ray shootRay. 568 00:22:02,970 --> 00:22:04,620 If you recall we have a gun, 569 00:22:04,620 --> 00:22:06,620 we're firing so we're going to use this ray to actually 570 00:22:06,620 --> 00:22:09,920 raycast out and figure out what it is we've hit 571 00:22:09,920 --> 00:22:13,010 with these bullets, and that's how we're going to hit things. 572 00:22:13,010 --> 00:22:15,550 We then have a RaycastHit variable called shootHit, 573 00:22:15,550 --> 00:22:17,550 which is going to return back to us 574 00:22:17,550 --> 00:22:19,390 whatever it is that we've hit. 575 00:22:19,390 --> 00:22:22,160 We're then going to have an int shootableMask. 576 00:22:22,160 --> 00:22:24,790 So we remember the floorMask which dictated that the 577 00:22:24,790 --> 00:22:27,690 raycast from the camera could only click on the floor. 578 00:22:27,690 --> 00:22:29,690 So what we're going to have is a shootable mask 579 00:22:29,690 --> 00:22:32,550 to make sure that we can only hit shootable things, 580 00:22:32,550 --> 00:22:35,090 we only want to shoot things that we can actually shoot. 581 00:22:35,090 --> 00:22:37,090 Then we have a particle system referenced 582 00:22:37,090 --> 00:22:39,980 to gunParticles, you'll recall that's the particle component 583 00:22:39,980 --> 00:22:41,740 that we added in our previous step, 584 00:22:41,740 --> 00:22:43,450 so that just gives us a reference to it. 585 00:22:43,450 --> 00:22:45,450 Here's our reference to our line renderer called 586 00:22:45,450 --> 00:22:49,320 gunLine, right, so again just so we can reference this in the script. 587 00:22:49,320 --> 00:22:52,370 Our audio source reference called gunAudio. 588 00:22:52,370 --> 00:22:54,960 Our light reference called gunLight. 589 00:22:54,960 --> 00:22:58,430 And then our float effectsDisplayTime, which is 590 00:22:58,430 --> 00:23:00,220 how long these effects are going to be 591 00:23:00,220 --> 00:23:02,560 viewable before they disappear. 592 00:23:02,560 --> 00:23:04,560 In our awake function we're going to setup all of our 593 00:23:04,560 --> 00:23:06,560 references so we want to 594 00:23:06,560 --> 00:23:09,490 set out shootableMask to the appropriate values 595 00:23:09,490 --> 00:23:12,530 by saying LayerMask.GetMask ("Shootable") ; 596 00:23:12,530 --> 00:23:14,530 What this is going to return back to us is 597 00:23:14,530 --> 00:23:16,530 the number of our shootable layer. 598 00:23:16,530 --> 00:23:19,700 You'll recall the level or the obstacles and everything 599 00:23:19,700 --> 00:23:22,650 is on the shootable layer and the Zombunny we 600 00:23:22,650 --> 00:23:24,170 created is also on the shootable layer. 601 00:23:24,170 --> 00:23:26,170 So by setting up this mask we can shoot pretty 602 00:23:26,170 --> 00:23:28,170 much anything that should be shootable. 603 00:23:28,170 --> 00:23:30,780 Then we have the gun particles which we're going to 604 00:23:30,780 --> 00:23:34,410 get access to by saying GetComponent 605 00:23:34,410 --> 00:23:36,910 Then we're going to do the same for line, audio and light, 606 00:23:36,910 --> 00:23:39,010 just gunLine = GetComponent 607 00:23:39,010 --> 00:23:41,010 gunAudio = GetComponent 608 00:23:41,010 --> 00:23:43,010 gunLight = GetComponent 609 00:23:43,010 --> 00:23:45,010 which is just giving us a reference 610 00:23:45,010 --> 00:23:46,730 so we can directly access those. 611 00:23:46,730 --> 00:23:48,730 Now in the update function is where we 612 00:23:48,730 --> 00:23:51,270 control whether or not it is time to shoot. 613 00:23:51,270 --> 00:23:53,270 So very much like with the enemy attacking 614 00:23:53,270 --> 00:23:55,270 here we have a function that's going to manage that. 615 00:23:55,270 --> 00:23:57,270 So the first thing we're going to do is 616 00:23:57,270 --> 00:23:59,270 accumulate your time and we do that by 617 00:23:59,270 --> 00:24:01,270 saying time += Time.deltaTime. 618 00:24:01,270 --> 00:24:03,270 So we'll basically increase in size 619 00:24:03,270 --> 00:24:05,270 as time progresses. 620 00:24:05,270 --> 00:24:06,610 Then we're going to say 621 00:24:06,610 --> 00:24:12,740 if(Input.GetButton ("Fire1") and you might be thinking 'what is Fire1?' 622 00:24:12,740 --> 00:24:14,200 Remember earlier we talked about the 623 00:24:14,200 --> 00:24:17,580 Input.GetAxisHorizontal, Input.GetAxisVerticle, 624 00:24:17,580 --> 00:24:21,520 these input axis that are built in to Unity for us 625 00:24:21,520 --> 00:24:23,400 Fire1 is one of those. 626 00:24:23,400 --> 00:24:25,400 Fire1 automatically maps to the 627 00:24:25,400 --> 00:24:27,720 left control on your keyboard or 628 00:24:27,720 --> 00:24:30,080 your mouse0 which is your left mouse button. 629 00:24:30,080 --> 00:24:31,430 So that happens for you automatically. 630 00:24:31,430 --> 00:24:33,430 You can override it here but if you don't do anything 631 00:24:33,430 --> 00:24:35,430 that's built in to Unity, so 632 00:24:35,430 --> 00:24:37,430 so you can always say Fire1 and that's how you talk 633 00:24:37,430 --> 00:24:38,920 about the left mouse button. 634 00:24:39,830 --> 00:24:41,830 So what we're saying is 635 00:24:41,830 --> 00:24:43,830 if the player has clicked the left mouse button 636 00:24:43,830 --> 00:24:47,490 or pressed the left control key 637 00:24:47,490 --> 00:24:49,150 and 638 00:24:49,150 --> 00:24:52,290 timer is greater than the delay between our shots. 639 00:24:52,290 --> 00:24:54,290 So if it's time to shoot and the player wants to shoot 640 00:24:54,290 --> 00:24:55,490 by clicking the button, 641 00:24:55,490 --> 00:24:57,490 then we're going to call our function Shoot. 642 00:24:57,490 --> 00:24:59,490 Shoot is a function we've written further down and 643 00:24:59,490 --> 00:25:00,900 we'll talk about that in a second. 644 00:25:00,900 --> 00:25:03,690 So if it's time and you're pressing the button we're going to shoot. 645 00:25:03,690 --> 00:25:05,690 Then we're going to say if the timer 646 00:25:05,690 --> 00:25:07,690 is greater than or equal to the 647 00:25:07,690 --> 00:25:09,690 timeBetterBullets times the 648 00:25:09,690 --> 00:25:13,630 effectDisplayTime what we'll do is disable the effects. 649 00:25:13,630 --> 00:25:15,630 So what that means is if we've fired 650 00:25:15,630 --> 00:25:17,630 and then enough time has progressed we're going 651 00:25:17,630 --> 00:25:20,440 to turn the light and the line renderer and everything back off. 652 00:25:20,440 --> 00:25:23,180 So we don't leave it on consistently. 653 00:25:23,180 --> 00:25:26,030 So the DisableEffects function is public 654 00:25:26,030 --> 00:25:29,130 which means again it can be A) referenced 655 00:25:29,130 --> 00:25:33,530 by another script, B) referenced by another script on another game object 656 00:25:33,530 --> 00:25:36,610 C) accessed by animation events and so on and so forth. 657 00:25:36,610 --> 00:25:38,870 Public basically gives us a lot of access to it. 658 00:25:38,870 --> 00:25:40,870 And the function basically says 659 00:25:40,870 --> 00:25:43,550 the light and the line renderer, 660 00:25:43,550 --> 00:25:44,990 just disable both of those. 661 00:25:44,990 --> 00:25:47,080 Again not SetActive but Enabled = false 662 00:25:47,080 --> 00:25:49,250 because they're components. 663 00:25:49,250 --> 00:25:51,250 Now the Shoot function itself. 664 00:25:51,250 --> 00:25:53,250 This is where we do the physics 665 00:25:53,250 --> 00:25:55,250 of actually firing the bullet and it's 666 00:25:55,250 --> 00:25:57,820 actually a fairly complex function here 667 00:25:57,820 --> 00:25:59,820 so we'll step through it nice and easy. 668 00:25:59,820 --> 00:26:01,820 The first thing we're going to do is reset the timer 669 00:26:01,820 --> 00:26:03,820 back to 0 because we're firing now and we're going to 670 00:26:03,820 --> 00:26:06,560 reset the amount of time we have to wait between firing. 671 00:26:06,560 --> 00:26:08,930 The very next thing we're going to do is play the audio 672 00:26:08,930 --> 00:26:10,750 and then we're turning the light on. 673 00:26:10,750 --> 00:26:12,440 Now we don't need to do anything with the light 674 00:26:12,440 --> 00:26:15,070 or the audio, we basically need to say 675 00:26:15,070 --> 00:26:17,070 Then what we're going to say is if the 676 00:26:17,070 --> 00:26:19,810 particles are still playing 677 00:26:19,810 --> 00:26:21,810 stop them and then start them again. 678 00:26:21,810 --> 00:26:23,810 What you don't want to happen is you don't 679 00:26:23,810 --> 00:26:25,810 want to go to play the particles 680 00:26:25,810 --> 00:26:27,810 have them already be playing 681 00:26:27,810 --> 00:26:29,810 thus they don't replay and we get 682 00:26:29,810 --> 00:26:32,600 a disconnect between the visuals of firing 683 00:26:32,600 --> 00:26:35,790 and the actual raycasting and physics that's happening. 684 00:26:35,790 --> 00:26:38,780 So we say if the particles are playing stop and start again. 685 00:26:38,780 --> 00:26:40,780 Also if that were to happen the stop 686 00:26:40,780 --> 00:26:42,780 and start would be so far your eye wouldn't even pick it up. 687 00:26:42,780 --> 00:26:46,810 It would just feel like you're just firing really fast. 688 00:26:46,810 --> 00:26:50,810 Then we're going to say 'let's turn on our line renderer', 689 00:26:50,810 --> 00:26:53,340 which is the actual visual element of the bullet. 690 00:26:53,340 --> 00:26:54,910 So we turn that on, so we say 691 00:26:54,910 --> 00:26:56,600 gunLine.enabled = true. 692 00:26:56,600 --> 00:26:59,590 Then here comes the tricky part about using lines, 693 00:26:59,590 --> 00:27:01,270 lines have 2 points right? 694 00:27:01,270 --> 00:27:02,870 One end and the other end. 695 00:27:02,870 --> 00:27:04,460 Well the first end we know, it's the end of the gun, 696 00:27:04,460 --> 00:27:06,080 it's the barrel of the gun, 697 00:27:06,080 --> 00:27:09,820 so we access that by saying gunLine.SetPosition 0. 698 00:27:09,820 --> 00:27:11,820 Computers start counting at 0 699 00:27:11,820 --> 00:27:13,820 so when we say SetPosition 0 we mean 700 00:27:13,820 --> 00:27:15,820 the first position of the line, 701 00:27:15,820 --> 00:27:17,820 which is right at the barrel of our gun. 702 00:27:17,820 --> 00:27:20,330 So we specify that as transform.position 703 00:27:20,330 --> 00:27:22,330 since the script is on the barrel of the gun. 704 00:27:22,330 --> 00:27:24,330 So that's the first point. 705 00:27:24,330 --> 00:27:26,330 But what's the second point? 706 00:27:26,330 --> 00:27:28,970 We don't know yet, that's the part we need to calculate. 707 00:27:28,970 --> 00:27:30,750 And that's the part that requires a bit of physics 708 00:27:30,750 --> 00:27:32,280 and a bit of raycasting. 709 00:27:32,280 --> 00:27:35,360 So if you recall we create a variable called shootRay, 710 00:27:35,360 --> 00:27:37,360 which is the raycast from our gun 711 00:27:37,360 --> 00:27:39,360 and so the first thing we want to do is 712 00:27:39,360 --> 00:27:41,360 setup this ray so that we can utilise it. 713 00:27:41,360 --> 00:27:43,360 The ray is going to start 714 00:27:43,360 --> 00:27:45,360 at the tip of the gun, so 715 00:27:45,360 --> 00:27:48,590 shootRay.origin = transform.position; 716 00:27:48,590 --> 00:27:50,590 That's where the ray is starting off. 717 00:27:50,590 --> 00:27:52,590 A ray starts at one point and 718 00:27:52,590 --> 00:27:54,590 goes in some direction, so we need to specify 719 00:27:54,590 --> 00:27:56,590 a direction for our ray, and we say 720 00:27:56,590 --> 00:28:00,760 shootRay.direction = transform.forward; 721 00:28:00,760 --> 00:28:03,520 and this is something James has talked about previously, 722 00:28:03,520 --> 00:28:06,410 we generally treat forward in the Z axis 723 00:28:06,410 --> 00:28:08,410 as forward, positive on Z axis as forward. 724 00:28:08,410 --> 00:28:12,490 So as the gun is pointing directly away from the player 725 00:28:12,490 --> 00:28:13,910 that is forward. 726 00:28:13,910 --> 00:28:15,910 So if I say transform.forward what I'm saying 727 00:28:15,910 --> 00:28:17,910 is 'that way', 728 00:28:17,910 --> 00:28:19,910 so that the bullet will travel 729 00:28:19,910 --> 00:28:24,550 directly along the line of this gun barrel and this gun. 730 00:28:24,550 --> 00:28:27,140 So we have our ray setup, at the tip of the gun 731 00:28:27,140 --> 00:28:29,140 that way, right, forward, so now we need to 732 00:28:29,140 --> 00:28:31,650 actually use physics to fire it. 733 00:28:31,650 --> 00:28:33,650 And so let me tell you what's going to happen 734 00:28:33,650 --> 00:28:35,220 and then we'll walk through the code. 735 00:28:35,220 --> 00:28:37,220 The idea is that we are going to fire a ray 736 00:28:37,220 --> 00:28:39,220 forward 100 units, because that's 737 00:28:39,220 --> 00:28:40,690 how far we say we can fire it. 738 00:28:40,690 --> 00:28:44,370 If it hits something, what ever it hits is going to be returned 739 00:28:44,370 --> 00:28:46,370 back to us and we're going to say 'the other end of 740 00:28:46,370 --> 00:28:48,370 the line is there', whatever it is we hit 741 00:28:48,370 --> 00:28:50,370 that's the other end and we draw the line. 742 00:28:50,370 --> 00:28:52,370 If we don't hit anything, 743 00:28:52,370 --> 00:28:54,370 we still want to fire, but if we don't hit anything 744 00:28:54,370 --> 00:28:56,010 what we're going to do is say 745 00:28:56,010 --> 00:28:57,480 'where's the other end of this line? 746 00:28:57,480 --> 00:28:59,480 It's just way out there' 747 00:28:59,480 --> 00:29:01,480 and it's actually such a long line 748 00:29:01,480 --> 00:29:03,480 there's literally no way to see 749 00:29:03,480 --> 00:29:04,810 the end of it so you just think 750 00:29:04,810 --> 00:29:06,810 'cool, he's just firing', you don't really notice. 751 00:29:06,810 --> 00:29:09,550 So we need to say shoot a ray, if it hits something 752 00:29:09,550 --> 00:29:11,550 that's what we hit, and if it doesn't 753 00:29:11,550 --> 00:29:13,550 then just draw a really long line. 754 00:29:13,550 --> 00:29:15,030 We do that by saying 755 00:29:15,030 --> 00:29:18,960 if(Physics.Raycast, so this again is that raycast function, 756 00:29:18,960 --> 00:29:20,810 and we're parsing in to it the ray that we specify, 757 00:29:20,810 --> 00:29:22,480 we're saying 'that way'. 758 00:29:22,480 --> 00:29:24,480 And then we're going to say 'give us information 759 00:29:24,480 --> 00:29:26,690 about what it is that we've hit' 760 00:29:26,690 --> 00:29:28,690 and that's why we have this Out keyword 761 00:29:28,690 --> 00:29:29,970 and the shootHit variable. 762 00:29:29,970 --> 00:29:31,970 If we hit something the variable shootHit will be 763 00:29:31,970 --> 00:29:33,970 like 'this is what you hit' 764 00:29:33,970 --> 00:29:36,780 cool, we need that information so it's important to have. 765 00:29:36,780 --> 00:29:38,780 Then we specify the range, and the range 766 00:29:38,780 --> 00:29:40,780 is 100f, it's something that we specified 767 00:29:40,780 --> 00:29:42,780 up at the top, it's a variable. 768 00:29:42,780 --> 00:29:45,090 Finally we parse in our shootable mask. 769 00:29:45,090 --> 00:29:47,090 Meaning this ray can only hit 770 00:29:47,090 --> 00:29:49,090 the things we want this ray to be able to hit, 771 00:29:49,090 --> 00:29:50,380 shootable things. 772 00:29:50,380 --> 00:29:53,030 We're not interested in anything else. 773 00:29:53,030 --> 00:29:56,160 If that ray hits something 774 00:29:56,160 --> 00:29:58,160 we're going to go in to this code. 775 00:29:58,160 --> 00:30:01,220 if it hits something what we're going to do 776 00:30:01,220 --> 00:30:03,220 is we are going to get from 777 00:30:03,220 --> 00:30:05,500 it the enemy health script. 778 00:30:05,500 --> 00:30:07,030 I'm going to talk about that here in a second. 779 00:30:07,030 --> 00:30:08,180 So what we're going to say is 780 00:30:08,180 --> 00:30:10,180 shootHit was going to have whatever 781 00:30:10,180 --> 00:30:12,180 it is we hit, we know we hit something. 782 00:30:12,180 --> 00:30:13,740 So we're going to say 783 00:30:13,740 --> 00:30:17,580 shootHit.collider.GetComponent 784 00:30:17,580 --> 00:30:19,580 So whatever it is I shoot I'm going to say 785 00:30:19,580 --> 00:30:21,580 give me your enemy health script and I'm going to store it. 786 00:30:21,580 --> 00:30:23,580 We need the enemy health script so we can 787 00:30:23,580 --> 00:30:26,980 reduce the life of the enemy. 788 00:30:26,980 --> 00:30:28,980 But what if we shot a wall? 789 00:30:28,980 --> 00:30:30,590 Or the ground? 790 00:30:30,590 --> 00:30:31,890 Or some Legos? 791 00:30:31,890 --> 00:30:33,890 Those do not have an enemy health script, 792 00:30:33,890 --> 00:30:36,260 those are not able to 793 00:30:36,260 --> 00:30:38,260 have health taken away from them. 794 00:30:38,260 --> 00:30:40,640 So if we attempt to access that enemy health script 795 00:30:40,640 --> 00:30:42,640 right away it's not going to work 796 00:30:42,640 --> 00:30:44,640 and we're going to get an error and the whole thing's 797 00:30:44,640 --> 00:30:45,820 going to stop working. 798 00:30:45,820 --> 00:30:47,820 So what we need to do next is 799 00:30:47,820 --> 00:30:50,100 we need to say if the enemy health script 800 00:30:50,100 --> 00:30:51,540 does not equal null, 801 00:30:51,540 --> 00:30:53,540 this is specifically in here to 802 00:30:53,540 --> 00:30:55,540 show you that this 803 00:30:55,540 --> 00:30:58,370 function will work even if that component does not 804 00:30:58,370 --> 00:31:01,290 It's just going to give you null, and null means nothing. 805 00:31:01,290 --> 00:31:03,940 It means one does not exist. 806 00:31:03,940 --> 00:31:05,940 So now we need to check it. We need today 807 00:31:05,940 --> 00:31:07,940 'hey, by the way, if that 808 00:31:07,940 --> 00:31:10,480 actually exists we're going to do 809 00:31:10,480 --> 00:31:12,830 enemyHealth.TakeDamage. 810 00:31:12,830 --> 00:31:14,830 We're going to parse in our damagePerShot 811 00:31:14,830 --> 00:31:16,830 and exactly where we hit it, 812 00:31:16,830 --> 00:31:18,830 that's a really nice thing about raycasting too, 813 00:31:18,830 --> 00:31:20,830 instead of just having a generic effect 814 00:31:20,830 --> 00:31:22,830 if we shoot on the shoulder that's where 815 00:31:22,830 --> 00:31:24,830 we hit it, the shoulder, wherever, 816 00:31:24,830 --> 00:31:26,830 we get to put the effects exactly 817 00:31:26,830 --> 00:31:28,830 where we hit it, which is pretty neat. 818 00:31:28,830 --> 00:31:30,830 If you remember in the enemy health script 819 00:31:30,830 --> 00:31:32,830 TakeDamage, if I just jump back to that, 820 00:31:32,830 --> 00:31:34,830 very briefly. 821 00:31:35,340 --> 00:31:40,070 We can see that there is a vector3 hitPoint. 822 00:31:40,070 --> 00:31:42,070 So you can see that TakeDamage here 823 00:31:42,070 --> 00:31:44,070 needs to have a vector3, needs to have a position 824 00:31:44,070 --> 00:31:46,070 in order to move the hitParticles to 825 00:31:46,070 --> 00:31:48,070 where they need to be 826 00:31:48,070 --> 00:31:50,070 so in PlayerShooting that's exactly what we 827 00:31:50,070 --> 00:31:51,640 provide using that raycast. 828 00:31:51,640 --> 00:31:56,400 Whether or not whatever we've shot has an enemy health script, 829 00:31:56,400 --> 00:31:59,120 regardless of whatever it is we still hit something 830 00:31:59,120 --> 00:32:04,170 and thus we say gunLine.SetPosition (1), meaning the second point 831 00:32:04,170 --> 00:32:06,170 and we parse in that point that we hit. 832 00:32:06,170 --> 00:32:08,170 So whether we're hitting an enemy or the wall or 833 00:32:08,170 --> 00:32:09,710 some Legos or whatever, 834 00:32:09,710 --> 00:32:11,410 we now have our line 835 00:32:11,410 --> 00:32:14,330 the beginning of the gun to the object that we hit. 836 00:32:14,330 --> 00:32:15,280 1 line. 837 00:32:15,280 --> 00:32:17,280 When we were prototyping this game 838 00:32:17,280 --> 00:32:19,280 we just had the raycast shoot by 839 00:32:19,280 --> 00:32:21,280 100 units but then we realised that actually it makes more 840 00:32:21,280 --> 00:32:23,280 sense if the gunLine stops at 841 00:32:23,280 --> 00:32:25,280 that point, we could expand upon 842 00:32:25,280 --> 00:32:27,280 the idea to have effects like ricochets 843 00:32:27,280 --> 00:32:29,280 and things like that but we didn't want to get too bogged down. 844 00:32:29,280 --> 00:32:31,280 So actually what this is saying is 845 00:32:31,280 --> 00:32:33,280 if we hit something 846 00:32:33,280 --> 00:32:35,580 then we end that line renderers second 847 00:32:35,580 --> 00:32:37,580 point or it's end point at 848 00:32:37,580 --> 00:32:39,580 the point at which the ray hit it, 849 00:32:39,580 --> 00:32:41,580 so when we shoot a bit of the environment 850 00:32:41,580 --> 00:32:43,580 it's going to stop at that point, 851 00:32:43,580 --> 00:32:45,580 likewise with the enemies. 852 00:32:45,580 --> 00:32:47,580 So all of that happens 853 00:32:47,580 --> 00:32:49,580 if we hit something. 854 00:32:49,580 --> 00:32:52,260 What if we don't hit something? That's our else. 855 00:32:52,260 --> 00:32:55,170 We're going to say gunLine.SetPosition 856 00:32:55,170 --> 00:32:57,170 1 again because it's the second point, 857 00:32:57,170 --> 00:32:59,170 the second part of the line, and what we're 858 00:32:59,170 --> 00:33:04,660 going to say is shootRay.origin, so where the ray starts, 859 00:33:04,660 --> 00:33:09,260 + shootRay.direction * range; 860 00:33:09,260 --> 00:33:11,260 So what that's going to mean is it'll take 861 00:33:11,260 --> 00:33:15,170 the point and then it's going to multiply (0, 0, 1) 862 00:33:15,170 --> 00:33:17,170 because that's forward in the Z direction, 863 00:33:17,170 --> 00:33:19,170 by 100, which is the range, and it's going to say 864 00:33:19,170 --> 00:33:21,170 this point and then take it way down there 865 00:33:21,170 --> 00:33:23,170 and then that point, and then it's going to draw a line. 866 00:33:23,170 --> 00:33:25,170 So it's really simple, it'll say 867 00:33:25,170 --> 00:33:28,340 one end, the other end, we're going to draw a line between it. 868 00:33:28,340 --> 00:33:30,340 So a lot of physics stuff in there but basically 869 00:33:30,340 --> 00:33:32,340 we're just attempting to point at something 870 00:33:32,340 --> 00:33:34,340 and if we're successful at 871 00:33:34,340 --> 00:33:36,340 pointing at something we're going to draw a line to it 872 00:33:36,340 --> 00:33:38,970 and take damage from it, otherwise we're just going to draw the line. 873 00:33:38,970 --> 00:33:40,970 So the else there effectively 874 00:33:40,970 --> 00:33:42,970 is giving us the ability to shoot in 875 00:33:42,970 --> 00:33:44,700 a direction if we don't hit something on the 876 00:33:44,700 --> 00:33:45,710 on the shootable layer. 877 00:33:45,710 --> 00:33:48,360 So if you remember that the level is basically a box, 878 00:33:48,360 --> 00:33:50,360 if you're aiming around in the other direction 879 00:33:50,360 --> 00:33:52,360 and you're effectively raycasting off 880 00:33:52,360 --> 00:33:54,360 of the screen you still want to see the 881 00:33:54,360 --> 00:33:56,360 gun fire so we're just giving it a range 882 00:33:56,360 --> 00:33:58,360 of 100, which is outside of the camera 883 00:33:58,360 --> 00:34:01,800 frustum, the view that you can see so we're 884 00:34:01,800 --> 00:34:04,400 just spraying bullets around even though we don't hit anything. 885 00:34:06,970 --> 00:34:08,970 What we'll do now that the gun barrel 886 00:34:08,970 --> 00:34:10,970 script is on there is save our scene 887 00:34:10,970 --> 00:34:12,220 if you haven't saved in a while. 888 00:34:12,220 --> 00:34:14,220 Now with the player selected 889 00:34:14,220 --> 00:34:16,220 in the hierarchy we're going to 890 00:34:16,220 --> 00:34:18,220 look at the Apply button in the upper 891 00:34:18,220 --> 00:34:19,180 right hand corner 892 00:34:19,180 --> 00:34:21,180 You'll recall we clicked and dragged the player 893 00:34:21,180 --> 00:34:23,180 and turned it in to a prefab but the 894 00:34:23,180 --> 00:34:25,180 prefab that we have on file is now 895 00:34:25,180 --> 00:34:27,910 slightly different than the prefab we have in our scene. 896 00:34:27,910 --> 00:34:29,910 The one on file doesn't have all the 897 00:34:29,910 --> 00:34:30,830 changes we just made 898 00:34:30,830 --> 00:34:32,830 If we would like to update the one we have 899 00:34:32,830 --> 00:34:34,830 on file we have to click the Apply button 900 00:34:34,830 --> 00:34:36,830 in the upper right hand corner. 901 00:34:36,830 --> 00:34:39,160 That will apply all of the changes we've made 902 00:34:39,160 --> 00:34:41,430 to the copy we have on file, 903 00:34:41,430 --> 00:34:43,430 thus saving it, if you will. 904 00:34:43,430 --> 00:34:45,430 The way that you notice the difference between 905 00:34:45,430 --> 00:34:48,010 a prefab and a prefab that 906 00:34:48,010 --> 00:34:50,010 needs updating is that somethings will appear 907 00:34:50,010 --> 00:34:52,010 in bold, so if you note the difference between 908 00:34:52,010 --> 00:34:54,010 Player Movement's properties and 909 00:34:54,010 --> 00:34:56,010 Player Health's which is added after 910 00:34:56,010 --> 00:34:58,010 that we made it in to a prefab 911 00:34:58,010 --> 00:35:01,400 you'll see that these are in bold and when I update 912 00:35:01,400 --> 00:35:04,370 or apply the changes and look back down 913 00:35:04,370 --> 00:35:06,370 some of them change so that the things 914 00:35:06,370 --> 00:35:08,850 that are still bold are things that are in the scene. 915 00:35:09,330 --> 00:35:11,330 So now we know that's up to date we can carry on 916 00:35:11,330 --> 00:35:13,330 using it in other scenes and if 917 00:35:13,330 --> 00:35:15,920 there are instances of this prefab elsewhere 918 00:35:15,920 --> 00:35:17,920 then you would see those changes are 919 00:35:17,920 --> 00:35:19,700 automatically applied. 920 00:35:19,700 --> 00:35:21,830 Let's go ahead and everyone 921 00:35:21,830 --> 00:35:23,830 turn your volume down because we're about to test this. 922 00:35:23,830 --> 00:35:25,830 Cool, let's see how loud it is. 923 00:35:28,650 --> 00:35:29,640 Oh yeah! 924 00:35:30,970 --> 00:35:31,710 That's what I'm talking about, 925 00:35:31,710 --> 00:35:33,080 that's an American gun right there. 926 00:35:36,120 --> 00:35:38,120 Just to reiterate, do not worry about 927 00:35:38,120 --> 00:35:40,120 this error that you will see appearing at the 928 00:35:40,120 --> 00:35:42,120 bottom of the screen, it's deliberate, 929 00:35:42,120 --> 00:35:45,150 this actual mistake is a genuine mistake 930 00:35:45,150 --> 00:35:47,940 that we want to show you how to fix. 931 00:35:48,600 --> 00:35:50,600 What you will notice is that at the 932 00:35:50,600 --> 00:35:53,410 bottom of the screen you have an error. 933 00:35:53,410 --> 00:35:55,800 So just to give you some insight on 934 00:35:55,800 --> 00:35:57,800 catching errors in Unity. 935 00:35:57,800 --> 00:35:59,800 Sometimes there'll be a compile error, 936 00:35:59,800 --> 00:36:01,800 in that you've written something wrong in a script 937 00:36:01,800 --> 00:36:03,800 and the bottom of the screen 938 00:36:03,800 --> 00:36:06,810 will show you where and what in that script has happened. 939 00:36:07,520 --> 00:36:10,950 And this is all contained within the console. 940 00:36:10,950 --> 00:36:12,950 So if you go to Window - Console 941 00:36:12,950 --> 00:36:14,950 you will see that there are a lot 942 00:36:14,950 --> 00:36:17,760 of errors saying all about the same thing, 943 00:36:17,760 --> 00:36:20,200 'set destination can only be called 944 00:36:20,200 --> 00:36:22,200 on an active agent that has been 945 00:36:22,200 --> 00:36:24,200 placed on a nav mesh'. 946 00:36:24,200 --> 00:36:26,200 So remember that the word 'agent' 947 00:36:26,200 --> 00:36:28,200 is referring to the enemy, 948 00:36:28,200 --> 00:36:31,320 the enemy has that nav mesh agent component 949 00:36:31,320 --> 00:36:33,780 and set destination is the function of code 950 00:36:33,780 --> 00:36:36,750 that we used to tell it to seek out the player. 951 00:36:36,750 --> 00:36:40,290 So we said nav.setDestination player.position. 952 00:36:41,260 --> 00:36:43,260 What is actually happening there is that there is 953 00:36:43,260 --> 00:36:46,350 a problem in our Enemy Movement script. 954 00:36:47,390 --> 00:36:49,390 What we can do is double click on that 955 00:36:49,390 --> 00:36:51,390 error in the console. 956 00:36:51,390 --> 00:36:53,390 Watch that one more time, I double click that and it 957 00:36:53,390 --> 00:36:56,550 takes me to what's causing that error. 958 00:36:57,230 --> 00:36:59,230 And if you recall earlier we mentioned 959 00:36:59,230 --> 00:37:01,230 that there are some bits of code that are 960 00:37:01,230 --> 00:37:04,440 commented out, that are disabled currently in this. 961 00:37:04,440 --> 00:37:07,400 When we first made the enemy chase after you 962 00:37:07,400 --> 00:37:09,400 we didn't have a concept of 963 00:37:09,400 --> 00:37:11,400 the enemy's health or the player's health. 964 00:37:11,400 --> 00:37:13,400 So having those things in our script 965 00:37:13,400 --> 00:37:15,400 wouldn't have made any sense. 966 00:37:16,010 --> 00:37:19,000 So now we do have those things. 967 00:37:19,000 --> 00:37:22,560 The player can be killed or the enemy can be killed 968 00:37:22,560 --> 00:37:25,340 and if it is then 969 00:37:25,340 --> 00:37:27,080 setting it's destination on the nav mesh 970 00:37:27,080 --> 00:37:29,080 doesn't make any sense any more 971 00:37:29,080 --> 00:37:31,080 so it's creating an error. 972 00:37:31,080 --> 00:37:33,080 So now what we can do is we can 973 00:37:33,080 --> 00:37:35,080 bring back those references to 974 00:37:35,080 --> 00:37:37,740 the enemy's health and the player's health 975 00:37:37,740 --> 00:37:41,940 and every time we do an update 976 00:37:41,940 --> 00:37:43,940 we want to check if both the player and the enemy 977 00:37:43,940 --> 00:37:46,220 are alive before we say 978 00:37:47,030 --> 00:37:49,030 setDesination, because if they're not 979 00:37:49,030 --> 00:37:50,220 then we're in trouble. 980 00:37:50,220 --> 00:37:51,850 I'm going to use a shortcut here 981 00:37:51,850 --> 00:37:54,950 I'll teach you which is command / 982 00:37:54,950 --> 00:37:57,990 which is a way of commenting out and entire line. 983 00:37:57,990 --> 00:37:59,990 That's control / for you PC folk. 984 00:37:59,990 --> 00:38:02,770 Yes, for PC, and we can do all of this at once. 985 00:38:03,540 --> 00:38:06,180 We've re-enabled this if statement and 986 00:38:06,180 --> 00:38:08,180 else structure around this to just check 987 00:38:08,180 --> 00:38:10,180 those different scripts, so it's checking the 988 00:38:10,180 --> 00:38:12,180 enemy's health, it's checking the player's health 989 00:38:12,180 --> 00:38:14,180 and if they're both more than 0 then it should 990 00:38:14,180 --> 00:38:17,270 keep trying to seek out the player. 991 00:38:17,270 --> 00:38:20,220 But if they're not then we switch off the nav mesh agent. 992 00:38:20,220 --> 00:38:23,370 So the error that we were having in Unity 993 00:38:23,370 --> 00:38:26,310 is saying that setDestination is effectively invalid 994 00:38:26,310 --> 00:38:28,310 and we're solving that by checking 995 00:38:28,310 --> 00:38:31,090 if the player is actually dead to do it. 996 00:38:31,090 --> 00:38:33,090 And likewise if the enemy has enough health 997 00:38:33,090 --> 00:38:34,750 to be moving around. 998 00:38:34,750 --> 00:38:36,750 If not we switch off that component 999 00:38:36,750 --> 00:38:40,340 by using .enabled = false. 1000 00:38:41,190 --> 00:38:44,440 So uncomment those lines in Enemy Movement, 1001 00:38:44,440 --> 00:38:47,790 save the script and then return to Unity. 1002 00:38:47,790 --> 00:38:49,790 Basically all of Enemy Movement should 1003 00:38:49,790 --> 00:38:51,790 now be enabled, you shouldn't have any comments 1004 00:38:51,790 --> 00:38:53,790 in it at all and that should get you to the 1005 00:38:53,790 --> 00:38:55,450 same point as us. 1006 00:38:55,450 --> 00:38:58,080 Okay, so the last part of this 1007 00:38:58,080 --> 00:39:01,750 is to look at the Player Health script. 1008 00:39:02,490 --> 00:39:04,490 What we're going to do is 1009 00:39:04,490 --> 00:39:06,490 reopen the Player Health script, 1010 00:39:06,490 --> 00:39:08,370 so if you double click on it 1011 00:39:08,370 --> 00:39:10,370 so go in to Scripts - Player 1012 00:39:10,370 --> 00:39:12,370 double click on the icon to open it. 1013 00:39:13,560 --> 00:39:15,560 And you'll see that 1014 00:39:16,410 --> 00:39:18,410 on the player's death 1015 00:39:19,510 --> 00:39:21,090 we've got some commented stuff here, 1016 00:39:21,090 --> 00:39:23,090 so we want to be able to put that back in 1017 00:39:23,090 --> 00:39:24,950 so that when the player dies 1018 00:39:24,950 --> 00:39:26,440 he can no longer shoot his gun. 1019 00:39:26,440 --> 00:39:29,170 But we haven't got our references to PlayerShooting yet. 1020 00:39:29,170 --> 00:39:31,170 Because it's turning red it's saying 1021 00:39:31,170 --> 00:39:33,170 'I don't know what PlayerShooting is and I 1022 00:39:33,170 --> 00:39:34,370 don't know what DisableEffects is'. 1023 00:39:34,370 --> 00:39:36,370 It can't find the reference to that so it's 1024 00:39:36,370 --> 00:39:38,830 turning red to alert us that there's a problem. 1025 00:39:38,830 --> 00:39:40,830 So on line 19 1026 00:39:41,300 --> 00:39:44,600 PlayerShooting, reference to the PlayerShooting script 1027 00:39:44,600 --> 00:39:46,320 which we've named the same. 1028 00:39:46,320 --> 00:39:48,030 So this can cause some confusion. 1029 00:39:48,030 --> 00:39:50,030 We've got PlayerMovement, PlayerMovement 1030 00:39:50,030 --> 00:39:52,030 remember this is the type, which is the name 1031 00:39:52,030 --> 00:39:54,600 of the script, and this is the name of the variable. 1032 00:39:54,600 --> 00:39:56,570 Names of variables can of course be anything 1033 00:39:56,570 --> 00:39:58,570 you want them to be but if I name PlayerMovement 1034 00:39:58,570 --> 00:40:01,700 Blah I will have to write Blah anywhere else. 1035 00:40:01,700 --> 00:40:03,150 But we're not going to do that. 1036 00:40:03,150 --> 00:40:05,960 We've named it something sensible, which means it's going to 1037 00:40:05,960 --> 00:40:08,550 sync up with the other instances and work. 1038 00:40:08,550 --> 00:40:10,550 So the last thing we need to uncomment is 1039 00:40:10,550 --> 00:40:12,550 the GetComponentInChildren 1040 00:40:12,550 --> 00:40:14,550 for PlayerShooting because remember 1041 00:40:14,550 --> 00:40:16,550 that the PlayerShooting script is on the 1042 00:40:16,550 --> 00:40:18,790 GunBarrelEnd, it's not on the Player 1043 00:40:18,790 --> 00:40:21,290 so we need to find that component in the children. 1044 00:40:21,290 --> 00:40:23,290 And now we've got our references and we've already 1045 00:40:23,290 --> 00:40:28,100 uncommented the codes about shooting when dead. 1046 00:40:28,100 --> 00:40:30,100 They're no longer red. 1047 00:40:30,100 --> 00:40:32,100 So it should all work. 1048 00:40:32,100 --> 00:40:34,100 Make sure that you save your script. 1049 00:40:34,100 --> 00:40:37,240 It's very important, and switch back to Unity. 1050 00:40:37,890 --> 00:40:40,900 You can then go ahead and play 1051 00:40:41,470 --> 00:40:42,520 and test your game. 1052 00:40:44,780 --> 00:40:46,780 Now we can kill them, there's no more error 1053 00:40:46,780 --> 00:40:49,060 about the nav mesh destination. 1054 00:40:49,830 --> 00:40:51,830 It's a very easy game, 1 enemy. 1055 00:40:53,370 --> 00:40:55,660 And we can also be killed by them. 1056 00:41:00,530 --> 00:41:02,530 So that is the end of phase 7.