WEBVTT 00:00:02.240 --> 00:00:04.030 So we have previously set 00:00:04.030 --> 00:00:06.030 the player up so we can see that the model 00:00:06.030 --> 00:00:09.160 has a gun built in, so let's go ahead 00:00:09.160 --> 00:00:11.160 and get the player to 00:00:11.160 --> 00:00:13.160 a point where the player can defend themselves. 00:00:13.160 --> 00:00:15.160 What we're going to do is give the enemy the ability to 00:00:15.160 --> 00:00:17.160 have health and then we're going to give the player 00:00:17.160 --> 00:00:19.160 the ability to take that health away. 00:00:19.160 --> 00:00:22.410 So we're going to go through the enemy health script next. 00:00:22.410 --> 00:00:24.410 So we'll start off by finding it in 00:00:24.410 --> 00:00:26.410 Scripts - Enemy folder. 00:00:26.410 --> 00:00:28.410 Then we can drag and drop that 00:00:28.410 --> 00:00:30.410 on to the Zombunny in the hierarchy. 00:00:30.410 --> 00:00:32.900 So that's going to apply the script for us. 00:00:34.810 --> 00:00:38.510 On the Zombunny, so we can see that there's 00:00:39.150 --> 00:00:40.740 a Death clip 00:00:41.380 --> 00:00:43.000 which we're going to apply. 00:00:43.540 --> 00:00:45.540 Use the circle select button 00:00:47.330 --> 00:00:50.370 and that's opened up the context sensitive menu so we can 00:00:50.370 --> 00:00:53.320 find Zombunny Death. Apply that. 00:00:54.480 --> 00:00:56.480 So now everything's setup for us, we can 00:00:56.480 --> 00:00:58.480 edit that script or view it 00:00:58.480 --> 00:01:00.480 and see what's going on in that. 00:01:00.930 --> 00:01:02.930 Double click the icon to open it. 00:01:04.710 --> 00:01:08.280 As always we've got our public variables at the top. 00:01:08.280 --> 00:01:10.960 And very similar to the player's health we've got 00:01:10.960 --> 00:01:12.690 startingHealth and a currentHealth. 00:01:12.690 --> 00:01:14.690 They work exactly the same as the player. 00:01:15.810 --> 00:01:18.490 The next one is the sinkSpeed. 00:01:18.490 --> 00:01:21.260 When these enemies die 00:01:21.260 --> 00:01:23.260 it looks a bit funky to just have them 00:01:23.260 --> 00:01:25.710 lay there and then suddenly disappear. 00:01:25.710 --> 00:01:27.710 So what we're going to do is make them sink through 00:01:27.710 --> 00:01:29.710 the floor, so as soon as they've 00:01:29.710 --> 00:01:31.710 finished flopping over and dying they sink 00:01:31.710 --> 00:01:33.710 through the floor, and that's how fast 00:01:33.710 --> 00:01:35.310 we want them to sink through the floor. 00:01:35.310 --> 00:01:37.310 Later on in the day we're going to start doing 00:01:37.310 --> 00:01:39.310 scoring for this game and 00:01:39.310 --> 00:01:41.310 so each enemy needs to have a scoreValue, 00:01:41.310 --> 00:01:43.310 how much they increase our score by 00:01:43.310 --> 00:01:45.310 and that is the scoreValue. 00:01:45.910 --> 00:01:47.910 And we've got the deathClip 00:01:47.910 --> 00:01:49.910 that they play when they die. 00:01:49.910 --> 00:01:51.910 We've got some private variables 00:01:51.910 --> 00:01:55.260 starting off with the animator component reference. 00:01:55.260 --> 00:01:57.260 Then we've got a reference to the 00:01:57.260 --> 00:01:59.950 audio source, we've also got a reference 00:01:59.950 --> 00:02:00.730 to the hit particles. 00:02:00.730 --> 00:02:03.520 If you remember we dragged that on as a prefab 00:02:03.520 --> 00:02:05.520 and applied it as a child object. 00:02:05.850 --> 00:02:08.810 Likewise we've got a capsule collider reference. 00:02:09.600 --> 00:02:12.860 Then we've got a pair of boolean variables. 00:02:12.860 --> 00:02:16.270 We've got IsDead, which works exactly the same as with the player. 00:02:16.270 --> 00:02:19.890 And IsSinking, so they don't immediately start sinking 00:02:19.890 --> 00:02:22.760 because we want to see the animation so we need to have 00:02:22.760 --> 00:02:24.760 separate bools to determine 00:02:24.760 --> 00:02:27.290 whether or not they're sinking and whether they're dead. 00:02:27.860 --> 00:02:30.580 Next we've got our awake function. 00:02:31.210 --> 00:02:33.390 Which is going to setup our references as usual. 00:02:33.950 --> 00:02:37.660 The first two, as we've seen before, GetComponent, 00:02:37.660 --> 00:02:40.470 the type of the component that we're going to find 00:02:40.470 --> 00:02:42.470 animator, audio source, 00:02:42.470 --> 00:02:44.470 but with hitParticles we need to 00:02:44.470 --> 00:02:48.460 find a component in the child object hitParticles. 00:02:48.840 --> 00:02:51.150 So what GetComponentInChildren will do 00:02:51.150 --> 00:02:53.150 will go through all of the children 00:02:53.150 --> 00:02:55.150 game object and then find 00:02:55.150 --> 00:02:57.720 the first particle system and return that. 00:02:57.720 --> 00:03:00.420 Again, we've got GetComponent to find the capsule collider. 00:03:01.210 --> 00:03:03.210 Then at the end of the awake function 00:03:03.210 --> 00:03:05.210 we're setting the current health to the starting health. 00:03:05.960 --> 00:03:07.960 In update 00:03:08.340 --> 00:03:10.840 all we're doing is we're checking whether or not 00:03:11.180 --> 00:03:13.180 the enemy is supposed to be sinking or not. 00:03:13.180 --> 00:03:17.070 If it is sinking then we're going to translate 00:03:17.070 --> 00:03:19.070 the transform, and that means just move it. 00:03:20.400 --> 00:03:22.400 So we're going to move it in a negative-up 00:03:22.400 --> 00:03:24.400 direction, down. 00:03:24.400 --> 00:03:27.280 and we're going to do that by the sinkSpeed 00:03:27.280 --> 00:03:31.000 per second, so that's that Time.DeltaTime thing. 00:03:31.620 --> 00:03:34.080 If we do that then we're moving per second 00:03:34.080 --> 00:03:35.620 instead of per frame. 00:03:35.620 --> 00:03:37.340 So just a quick note about translate. 00:03:37.340 --> 00:03:39.340 Previously we used move position to move 00:03:39.340 --> 00:03:41.340 this thing but we're going to be no longer using 00:03:41.340 --> 00:03:43.340 physics when the enemies die so 00:03:43.340 --> 00:03:45.340 we can go ahead and use translate without 00:03:45.340 --> 00:03:47.340 worrying about losing sync with physics. 00:03:48.790 --> 00:03:52.000 Next we've got, again very similar 00:03:52.000 --> 00:03:54.000 function to the player's health we've got 00:03:54.000 --> 00:03:56.000 a public function, so again that means it 00:03:56.000 --> 00:03:57.930 can be called from another function 00:03:57.930 --> 00:03:59.450 and that's where we'll be calling it from. 00:03:59.450 --> 00:04:00.090 Another script. 00:04:00.090 --> 00:04:01.190 Another script, sorry. 00:04:01.770 --> 00:04:03.770 But this time we've got the integer 00:04:03.770 --> 00:04:05.770 of how much damage is going to be taken 00:04:05.770 --> 00:04:08.360 but also the hitPoint, 00:04:08.360 --> 00:04:10.360 so where has it been it? 00:04:10.360 --> 00:04:12.360 And we'll be using that hitPoint to 00:04:12.360 --> 00:04:14.880 move the particle system around the enemy 00:04:14.880 --> 00:04:17.380 so that fluff is flying out wherever it gets hit. 00:04:19.000 --> 00:04:21.000 Right, so the first thing we want to do in this function 00:04:21.000 --> 00:04:24.710 is check if the enemy is dead. 00:04:24.710 --> 00:04:26.710 If it is dead then we don't need to do 00:04:26.710 --> 00:04:28.710 anything so we're going to return out of this function. 00:04:29.670 --> 00:04:32.170 Assuming we are not dead, or the enemy is not dead 00:04:32.170 --> 00:04:34.170 we can continue on with this function. 00:04:34.170 --> 00:04:36.170 Then since we've taken damage we want to play 00:04:36.170 --> 00:04:37.890 the Hurt sound effect. 00:04:38.490 --> 00:04:40.490 We'll then lose the amount of health 00:04:40.490 --> 00:04:42.050 from our current health. 00:04:42.900 --> 00:04:44.900 Next what we're going to do is 00:04:44.900 --> 00:04:48.650 find the hitParticles, so that's the particle system 00:04:48.650 --> 00:04:51.230 Find the transform that that is on 00:04:51.230 --> 00:04:54.200 and move that position to the hitPoint. 00:04:54.790 --> 00:04:57.810 So we've got a child game object 00:04:57.810 --> 00:05:00.730 finding that position and moving it to 00:05:00.730 --> 00:05:02.550 where ever we've been hit. 00:05:02.550 --> 00:05:04.790 So note that we haven't actually defined the hitPoint 00:05:04.790 --> 00:05:07.660 but it's going to be passed in to this TakeDamage function 00:05:07.660 --> 00:05:10.320 using that second argument, this vector3. 00:05:10.320 --> 00:05:13.380 So we're going to send it wherever we call TakeDamage 00:05:13.380 --> 00:05:15.030 and you'll see that a little later on. 00:05:15.840 --> 00:05:17.840 Okay, so after we've moved 00:05:17.840 --> 00:05:19.840 the position of the particle system 00:05:19.840 --> 00:05:23.100 we can then play the particle system, so the fluff starts flying out. 00:05:26.500 --> 00:05:28.500 Last in this function, we're going to check 00:05:28.500 --> 00:05:30.500 if our current health is less than 00:05:30.500 --> 00:05:32.500 or equal to 0. 00:05:32.500 --> 00:05:35.750 If we've run out of health then we'll use the Death function. 00:05:36.810 --> 00:05:38.080 So the Death function. 00:05:39.420 --> 00:05:42.360 First of all we set isDead equals to true. 00:05:43.330 --> 00:05:47.170 Then we set the capsule collider to a trigger. 00:05:47.170 --> 00:05:49.170 So what that means is because you don't 00:05:49.170 --> 00:05:51.520 actually physically hit triggers 00:05:51.890 --> 00:05:55.160 if the player is running along mowing down enemies 00:05:55.810 --> 00:05:57.810 then when they die they won't become 00:05:57.810 --> 00:05:59.810 an obstacle any more, it can keep on moving 00:05:59.810 --> 00:06:01.230 and keep on mowing through them. 00:06:02.750 --> 00:06:04.750 We set the animator trigger Dead 00:06:04.750 --> 00:06:06.750 so the enemy knows it's dead 00:06:06.750 --> 00:06:08.750 it performs it's Dead animation. 00:06:09.570 --> 00:06:11.570 And lastly we're going to set the audio 00:06:11.570 --> 00:06:14.150 source to play the Death clip. 00:06:14.150 --> 00:06:16.150 So we change the clip that it's got to play to Death 00:06:16.150 --> 00:06:17.770 and then make it play. 00:06:17.770 --> 00:06:19.770 Okay, so we've got a public function here 00:06:19.770 --> 00:06:21.970 called StartSinking and we'll discuss why it's 00:06:21.970 --> 00:06:23.970 public in a minute but for now we'll just 00:06:23.970 --> 00:06:25.380 go through what it does. 00:06:26.450 --> 00:06:28.450 In this we're going to find references 00:06:28.450 --> 00:06:29.920 to the nav mesh agent 00:06:30.710 --> 00:06:32.710 and disable it. 00:06:32.710 --> 00:06:34.710 And then we're going to find a reference to the 00:06:34.710 --> 00:06:38.080 rigidbody component and set it to isKinematic 00:06:38.080 --> 00:06:41.100 The reason we're going to set it to kinematic is 00:06:41.100 --> 00:06:43.100 that when you move a collider 00:06:43.100 --> 00:06:45.100 in the scene Unity will try and 00:06:45.100 --> 00:06:47.100 recalculate all the static geometry 00:06:47.100 --> 00:06:49.880 because it thinks 'okay, the level's changed, 00:06:49.880 --> 00:06:51.880 I need to rethink about this. 00:06:52.530 --> 00:06:54.530 But if you've got a kinematic rigidbody 00:06:54.530 --> 00:06:57.960 and you're translating this object 00:06:57.960 --> 00:07:00.860 then it will ignore it, so that's why we're doing that. 00:07:01.290 --> 00:07:05.230 Real quick here, we see GetComponent 00:07:05.230 --> 00:07:07.230 .enabled = false; 00:07:07.230 --> 00:07:09.230 so if we were trying to turn 00:07:09.230 --> 00:07:11.230 off a game object 00:07:11.230 --> 00:07:14.640 we say .setActive 00:07:14.640 --> 00:07:16.640 and in parenthesis say false. 00:07:16.640 --> 00:07:18.640 Nav mesh agent's a component so we say 00:07:18.640 --> 00:07:20.640 .enabled, so just keep that in mind. 00:07:20.640 --> 00:07:23.690 If you see .setActive = false 00:07:23.690 --> 00:07:25.690 that's a game object and we're turning the whole 00:07:25.690 --> 00:07:27.090 game object off. 00:07:27.090 --> 00:07:29.590 Here we're doing .enabled = false 00:07:29.590 --> 00:07:31.590 That means I'm not turning off the whole game object 00:07:31.590 --> 00:07:35.340 just this one component of that game object. 00:07:36.210 --> 00:07:39.670 Since we're starting to sink isSinking is true. 00:07:39.670 --> 00:07:43.300 And lastly we're going to destroy the game object 00:07:43.300 --> 00:07:46.020 after 2 seconds, so basically 00:07:46.020 --> 00:07:48.020 it's started sinking, it's going through the floor, 00:07:48.020 --> 00:07:50.020 after 2 seconds we're not going to see it any more 00:07:50.020 --> 00:07:52.370 we can get rid of it, so we'll destroy the game object. 00:07:52.370 --> 00:07:53.540 And that's the end of that function 00:07:53.540 --> 00:07:55.270 and the end of this script as well. 00:07:55.270 --> 00:07:57.270 When we're done with that we can hop back over 00:07:57.270 --> 00:07:59.270 in to Unity here. 00:08:00.850 --> 00:08:03.640 The enemy now has health and so one of the 00:08:03.640 --> 00:08:05.640 things that we want to do is we 00:08:05.640 --> 00:08:08.090 want to make the enemy's 00:08:08.090 --> 00:08:10.090 ability to attack dependent on whether or not 00:08:10.090 --> 00:08:11.550 the enemy is alive. 00:08:11.550 --> 00:08:13.550 Sorry to interrupt you Mike, I've just remembered that we've 00:08:13.550 --> 00:08:15.550 missed something out. 00:08:15.550 --> 00:08:17.550 So we had that StartSinking function 00:08:17.550 --> 00:08:19.550 and it was public 00:08:19.550 --> 00:08:21.550 but we never called it. 00:08:21.550 --> 00:08:22.950 The reason we never called it is because 00:08:22.950 --> 00:08:24.710 it's on an animation event. 00:08:25.410 --> 00:08:28.600 So all these enemies, they have an animation event 00:08:28.600 --> 00:08:30.600 where they flop and then die. 00:08:31.380 --> 00:08:33.960 And what we can do in Unity is say 00:08:33.960 --> 00:08:35.960 somewhere along the line of that animation 00:08:35.960 --> 00:08:37.960 we're going to say 'at this point 00:08:37.960 --> 00:08:40.610 try and look for this function 00:08:40.610 --> 00:08:43.270 on the game object, somewhere on the game object 00:08:43.270 --> 00:08:46.010 there will be a function called StartSinking. 00:08:46.320 --> 00:08:48.320 So it'll look for that and then if 00:08:48.320 --> 00:08:50.320 it finds it it'll play that function. 00:08:51.100 --> 00:08:53.100 This was setup already. 00:08:53.100 --> 00:08:55.100 Yes, this isn't something that you have to do, 00:08:55.100 --> 00:08:57.100 this is something that we've setup for you. 00:09:00.130 --> 00:09:03.670 So this is something that is already there, 00:09:03.670 --> 00:09:05.670 and basically what this says is at this 00:09:05.670 --> 00:09:07.670 mark, which is something that is already in the animation 00:09:07.670 --> 00:09:09.050 it's not anything that you guys have to do, 00:09:09.050 --> 00:09:11.050 it's going to attempt to call a method called 00:09:11.050 --> 00:09:12.670 StartSinking. 00:09:12.670 --> 00:09:14.670 Now up until this point there has been 00:09:14.670 --> 00:09:15.790 no such method, alright. 00:09:15.790 --> 00:09:17.790 But now we've created one and we've added 00:09:17.790 --> 00:09:19.790 it so now it knows 00:09:19.790 --> 00:09:21.790 I have a method called StartSinking and 00:09:21.790 --> 00:09:23.790 that will happen when it's time comes. 00:09:25.700 --> 00:09:27.700 Normally if the animation were to play 00:09:27.700 --> 00:09:29.700 and that function were to not be there you would 00:09:29.700 --> 00:09:31.700 get an error saying 'hey, I'm trying to call this function 00:09:31.700 --> 00:09:32.820 and one does not exist. 00:09:32.820 --> 00:09:34.820 We did not get an error because we have yet to 00:09:34.820 --> 00:09:36.970 have any way of killing the enemy. 00:09:36.970 --> 00:09:38.970 If we had we would have seen an error but 00:09:38.970 --> 00:09:40.970 if we don't then we do not see an error. 00:09:40.970 --> 00:09:45.050 The function was public and so this animation will 00:09:45.050 --> 00:09:47.960 automatically call that function when the time comes. 00:09:47.960 --> 00:09:49.960 Animation events are really useful, 00:09:49.960 --> 00:09:52.500 you can use them for all kinds of things. 00:09:52.500 --> 00:09:54.500 A real common use would be footsteps 00:09:54.500 --> 00:09:56.500 if you had a single function to call a 00:09:56.500 --> 00:09:58.500 footstep at a particular point in an animation 00:09:58.500 --> 00:10:00.500 because obviously you can pick a particular frame 00:10:00.500 --> 00:10:02.000 where you want this to happen. 00:10:02.000 --> 00:10:04.320 We've got this fairly roughly placed 00:10:04.320 --> 00:10:06.320 and it just means that as soon as he just leaps up 00:10:06.320 --> 00:10:08.320 we'll start sending him down as well 00:10:08.320 --> 00:10:10.320 as his falling animation, 00:10:10.320 --> 00:10:12.320 which actually means that we're not getting the bounce on 00:10:12.320 --> 00:10:14.320 on the floor, but we could move it along if we wanted 00:10:14.320 --> 00:10:16.320 to but we're not going to bother doing that, 00:10:16.320 --> 00:10:18.180 you can play around with that later on. 00:10:20.020 --> 00:10:22.020 Now that we have the Enemy Health script 00:10:22.020 --> 00:10:24.250 it's important for us to pair the Enemy Attack script 00:10:24.250 --> 00:10:26.250 to that so that the enemy does not 00:10:26.250 --> 00:10:28.250 attack once they're already dead. 00:10:28.250 --> 00:10:30.710 What we'd like to do is edit 00:10:30.710 --> 00:10:33.540 the Enemy Attack script and uncomment 00:10:33.540 --> 00:10:35.550 those lines that we previously saw commented. 00:10:35.550 --> 00:10:37.550 Now we could locate the script in the project view 00:10:37.550 --> 00:10:38.940 and double click it and open it 00:10:38.940 --> 00:10:40.940 but I just want to run through another way that we 00:10:40.940 --> 00:10:42.940 could open that script. 00:10:42.940 --> 00:10:46.050 What I'm going to do is click on the Zombunny, 00:10:46.050 --> 00:10:48.050 and I am going to find the 00:10:48.050 --> 00:10:50.900 Enemy Attack script, the Enemy Attack Script. 00:10:50.900 --> 00:10:55.080 And of the script components the first property is always 00:10:55.080 --> 00:10:57.080 the script itself. 00:10:57.080 --> 00:10:59.080 Every script component has a first property called 00:10:59.080 --> 00:11:02.140 Script and the value of that is itself. 00:11:02.140 --> 00:11:04.140 So if I click on it what it does is highlight 00:11:04.140 --> 00:11:06.140 for me in the project view that script. 00:11:06.140 --> 00:11:07.500 So I'm like 'hey, which script is this?', 00:11:07.500 --> 00:11:08.860 you click on it, there it is. 00:11:08.860 --> 00:11:10.860 But I can then double click on it here 00:11:10.860 --> 00:11:12.860 inside this property and it again will 00:11:12.860 --> 00:11:14.590 open up inside Mono Develop. 00:11:14.590 --> 00:11:16.590 So that's another way to open the script up. 00:11:16.590 --> 00:11:18.590 So this is now a part that you're all going to want to 00:11:18.590 --> 00:11:20.590 follow along with because we're now going to remove 00:11:20.590 --> 00:11:22.590 this commenting while we explain what 00:11:22.590 --> 00:11:24.010 the comments were for. 00:11:24.010 --> 00:11:26.010 I'm going to come up to the top here 00:11:26.010 --> 00:11:28.010 and the first bit of commenting I see 00:11:28.010 --> 00:11:30.010 is a commented out variable 00:11:30.010 --> 00:11:32.740 that references EnemyHealth. 00:11:32.740 --> 00:11:34.740 Obviously we had to comment that out before 00:11:34.740 --> 00:11:37.060 because EnemyHealth didn't exist on the enemy yet 00:11:37.060 --> 00:11:38.850 so we had to keep that commented out. 00:11:38.850 --> 00:11:40.850 Now we're going to uncomment it so we can now have a 00:11:40.850 --> 00:11:43.360 reference to the enemy health script. 00:11:43.360 --> 00:11:45.010 Next we're going to go to our awake function, 00:11:45.010 --> 00:11:46.730 and again, since we have this variable, 00:11:46.730 --> 00:11:48.730 we now have to get that reference and we dothat 00:11:48.730 --> 00:11:51.630 by saying enemyHealth = GetComponent, 00:11:51.630 --> 00:11:54.090 and the component name is the name of the script, 00:11:54.090 --> 00:11:56.090 which is EnemyHealth. 00:11:56.790 --> 00:11:58.790 And then we're going to come down here towards the bottom 00:11:58.790 --> 00:12:01.110 and we're going to notice this. 00:12:01.110 --> 00:12:05.290 And this is not the commenting you've seen before. 00:12:05.290 --> 00:12:07.290 There we go, it's about as close as it'll let me get. 00:12:07.290 --> 00:12:10.430 So this is called a Block Comment 00:12:10.430 --> 00:12:12.430 and what block comments enable me to do is 00:12:12.430 --> 00:12:14.430 comment many, many, many lines 00:12:14.430 --> 00:12:17.240 or pieces inside a line 00:12:17.240 --> 00:12:19.240 or whatever, right? So what we have here 00:12:19.240 --> 00:12:21.240 is what's called a Block Comment. 00:12:21.240 --> 00:12:23.240 And we can see that here's 00:12:23.240 --> 00:12:25.170 the start of the block comment, 00:12:25.170 --> 00:12:26.940 and here is the end of the block comment. 00:12:26.940 --> 00:12:32.410 So it's / all the way through to /. 00:12:32.410 --> 00:12:34.410 And anything between there is commented out. 00:12:34.410 --> 00:12:36.410 And you'll see that these parentheses 00:12:36.410 --> 00:12:38.620 or parenthesis is not commented out 00:12:38.620 --> 00:12:40.620 because only the things between the block 00:12:40.620 --> 00:12:41.840 comments get commented out. 00:12:41.840 --> 00:12:44.360 So what we want to do is remove the block comment. 00:12:44.360 --> 00:12:46.360 So when we start to remove this 00:12:46.360 --> 00:12:47.970 we'll see everything gets commented out and then 00:12:47.970 --> 00:12:49.210 nothing's commented out. 00:12:49.210 --> 00:12:51.210 So what this line says now 00:12:51.210 --> 00:12:52.970 if you recall previously it says 00:12:52.970 --> 00:12:54.970 'hey, if it's time to attack 00:12:54.970 --> 00:12:56.970 and the player is in range 00:12:56.970 --> 00:12:58.540 we attack the player'. 00:12:58.540 --> 00:13:00.540 Well there's one more step, one more decision 00:13:00.540 --> 00:13:02.200 you have to make, we have to say 00:13:02.200 --> 00:13:03.780 'if it's time to attack 00:13:03.780 --> 00:13:06.400 and the player is in range 00:13:06.400 --> 00:13:08.220 and we're not dead' 00:13:08.220 --> 00:13:10.060 and at that point we attack. 00:13:10.060 --> 00:13:12.060 So at this point now with all of that stuff 00:13:12.060 --> 00:13:14.060 uncommented now the enemy will attack the player 00:13:14.060 --> 00:13:16.060 correctly like they had been and they'll 00:13:16.060 --> 00:13:17.750 also stop when they're dead, 00:13:17.750 --> 00:13:21.710 which is obviously an important part of this whole dynamic. 00:13:21.710 --> 00:13:23.710 Be sure to get that stuff done. When you're finished 00:13:23.710 --> 00:13:25.320 be sure to save, 00:13:25.320 --> 00:13:27.320 save the script and return, 00:13:27.320 --> 00:13:29.850 which will bring us back in to Unity. 00:13:31.030 --> 00:13:35.560 The enemy is cool, we can leave it alone for now. 00:13:35.560 --> 00:13:37.560 Now let's go ahead and get this 00:13:37.560 --> 00:13:39.080 player ready to rock and roll. 00:13:39.080 --> 00:13:40.990 There's a few things that we're going to need to do. 00:13:40.990 --> 00:13:42.990 The player already has this mesh and the mesh 00:13:42.990 --> 00:13:44.990 has a gun, so the player is already setup 00:13:44.990 --> 00:13:46.990 with his weapon of choice so what we want to 00:13:46.990 --> 00:13:49.860 do is we want to make it behave appropriately. 00:13:49.860 --> 00:13:51.110 So there's some stuff we want to add to it. 00:13:51.110 --> 00:13:53.680 The first is we're going to add a particle component 00:13:53.680 --> 00:13:55.680 which is going to enable the gun to 00:13:55.680 --> 00:13:58.120 spit fire, which makes it kinda fun. 00:13:58.120 --> 00:14:00.120 We're also going to add the ability for 00:14:00.120 --> 00:14:03.130 it to have sound so we can hear 00:14:03.130 --> 00:14:04.150 when the player is firing. 00:14:04.150 --> 00:14:06.150 We're going to add a light so that the player 00:14:06.150 --> 00:14:08.150 illuminates the scene when firing 00:14:08.150 --> 00:14:10.150 and then we're going to add a line renderer 00:14:10.150 --> 00:14:12.150 and the line renderer is actually going to be that 00:14:12.150 --> 00:14:14.150 line that we're firing out 00:14:14.150 --> 00:14:16.150 to make it look like we're firing bullets 00:14:16.150 --> 00:14:18.150 or lasers or whatever, 00:14:18.150 --> 00:14:19.820 and all of these things are going to make this 00:14:19.820 --> 00:14:23.310 a much more fun look and feel to the game. 00:14:23.310 --> 00:14:25.310 So what we're going to do is go to the prefabs folder 00:14:25.310 --> 00:14:28.240 and we're going to locate gunParticles. 00:14:28.240 --> 00:14:31.300 Unlike the hitParticles of the enemy 00:14:31.300 --> 00:14:33.300 we are not going to click and drag this prefab 00:14:33.300 --> 00:14:35.300 anywhere near the player. 00:14:35.300 --> 00:14:37.300 Instead we are going to copy 00:14:37.300 --> 00:14:39.300 a component off of it. 00:14:39.300 --> 00:14:41.600 So we're not going to use this prefab 00:14:41.600 --> 00:14:43.600 we're just going to use a piece of this prefab. 00:14:43.600 --> 00:14:45.600 So if we look over here in the inspector 00:14:45.600 --> 00:14:47.600 we see the particle system that is 00:14:47.600 --> 00:14:50.620 a part of that gunParticles 00:14:50.620 --> 00:14:53.580 and I can click the cog, or gear 00:14:53.580 --> 00:14:57.110 right here and I can select Copy Component. 00:14:57.110 --> 00:14:59.110 I'm not actually interested in the game object or the 00:14:59.110 --> 00:15:01.570 prefab I'm interested in the component that's on it. 00:15:01.570 --> 00:15:03.570 Once I've copied that component 00:15:03.570 --> 00:15:05.570 I'm going to go to the player in my hierarchy 00:15:05.570 --> 00:15:07.450 and I'm going to expand it. 00:15:07.450 --> 00:15:10.520 And I'm going to look for GunBarrelEnd. 00:15:10.520 --> 00:15:12.520 Since the player has a collider 00:15:12.520 --> 00:15:14.870 and the player has all these pieces and parts 00:15:14.870 --> 00:15:16.870 I'm actually really interested in the 00:15:16.870 --> 00:15:18.870 tip of the gun so that I can 00:15:18.870 --> 00:15:20.870 align bullets and everything with 00:15:20.870 --> 00:15:22.870 the tip of the gun and get 00:15:22.870 --> 00:15:25.470 shooting to happen the way we want it to. 00:15:26.010 --> 00:15:29.290 With GunBarrelEnd not Gun, 00:15:29.290 --> 00:15:31.290 not Player, GunBarrelEnd selected 00:15:31.290 --> 00:15:33.290 in the hierarchy I need to 00:15:33.290 --> 00:15:35.590 locate a cog, there's always a cog 00:15:35.590 --> 00:15:37.590 in the upper right hand corner of the transform component. 00:15:38.270 --> 00:15:40.270 And I'm going to click that and I'm going to select 00:15:40.270 --> 00:15:42.270 Component As New. 00:15:42.270 --> 00:15:43.780 And there's my particles. 00:15:43.780 --> 00:15:45.780 So at this point the particles are 00:15:45.780 --> 00:15:47.780 on the end of the gun. 00:15:47.780 --> 00:15:49.410 Now they're not set to play. 00:15:49.410 --> 00:15:51.410 We going to use scripts to say when 00:15:51.410 --> 00:15:53.410 those particles should start firing off. 00:15:53.410 --> 00:15:56.400 So now we have the particle system on the gun. 00:15:56.400 --> 00:16:00.920 Now let's minimise the particle system by 00:16:00.920 --> 00:16:02.920 clicking the little arrow next to it 00:16:02.920 --> 00:16:04.920 because the particle system itself takes up so much space 00:16:04.920 --> 00:16:06.920 so we're just going to collapse that down 00:16:06.920 --> 00:16:08.620 so that we have some room to work. 00:16:08.620 --> 00:16:10.620 So we have our particle system and the next thing 00:16:10.620 --> 00:16:11.880 we want is a Line Renderer. 00:16:11.880 --> 00:16:13.880 The line renderer is going to be the visual 00:16:13.880 --> 00:16:15.880 component of shooting and we're going to add that 00:16:15.880 --> 00:16:18.670 by going to Add Component and I'm just going to type Line 00:16:18.670 --> 00:16:20.460 and find Line Renderer. 00:16:20.460 --> 00:16:21.730 I'm specifically typing in line because I can't 00:16:21.720 --> 00:16:23.240 remember where it is otherwise. 00:16:23.240 --> 00:16:25.240 Now a line renderer appears 00:16:25.240 --> 00:16:27.240 as a component in our game object 00:16:27.240 --> 00:16:29.240 and so what the line renderer is, like I say, 00:16:29.240 --> 00:16:31.240 it renders a line, and so there's some settings 00:16:31.240 --> 00:16:33.240 that we've got to do because as we can see right now 00:16:33.240 --> 00:16:35.240 that doesn't really look like a bullet and that doesn't 00:16:35.240 --> 00:16:36.410 really look like a gunshot, it just looks like 00:16:36.410 --> 00:16:38.160 a giant magenta block. 00:16:38.160 --> 00:16:40.790 So we need to set this up appropriately. 00:16:40.790 --> 00:16:42.790 The first thing we're going to do is 00:16:42.790 --> 00:16:45.950 expand the Materials drop down, and you can see here 00:16:45.950 --> 00:16:48.520 I can just expand that by clicking the Materials drop down. 00:16:48.520 --> 00:16:51.140 And I'm going to look for this Element 0. 00:16:51.140 --> 00:16:53.140 And right now the reason this is 00:16:53.140 --> 00:16:55.810 that hideous fuchsia colour is that it doesn't have 00:16:55.810 --> 00:16:57.870 a material, it doesn't know what it's supposed to look like. 00:16:57.870 --> 00:16:59.870 So what we want to do is give it 00:16:59.870 --> 00:17:01.870 a colour so we're going to use the 00:17:01.870 --> 00:17:03.610 circle selector, we're going to click on that, 00:17:03.610 --> 00:17:06.700 and we already have a material created. 00:17:06.700 --> 00:17:09.360 It's just a basic line renderer material 00:17:09.360 --> 00:17:12.650 which we have cleverly named LineRendererMaterial. 00:17:12.650 --> 00:17:14.650 So if you use the circle selector and look for 00:17:14.650 --> 00:17:17.150 that list you should see a line renderer material. 00:17:17.150 --> 00:17:19.150 Double click on that and it will apply it 00:17:19.150 --> 00:17:20.950 to the gun. 00:17:20.950 --> 00:17:25.400 What we want to do is and manage this line renderer better. 00:17:25.400 --> 00:17:27.400 As we can see it's still, while it's still a 00:17:27.400 --> 00:17:29.780 better colour, it's the colour of our laser 00:17:29.780 --> 00:17:33.230 it's still way too big, so we definitely want to manage this. 00:17:33.230 --> 00:17:35.230 I'll go ahead and collapse the material 00:17:35.230 --> 00:17:36.660 back down as we're done there. 00:17:36.660 --> 00:17:38.660 I'm going to expand Parameters and there's two things 00:17:38.660 --> 00:17:40.660 I'm interested in, the first is Start Width 00:17:40.660 --> 00:17:42.260 and the second is End Width. 00:17:42.260 --> 00:17:43.500 And what we're going to do is set these up 00:17:43.500 --> 00:17:44.910 to be the same value. 00:17:44.910 --> 00:17:46.910 Because they're the same value the laser will 00:17:46.910 --> 00:17:48.410 consider them to be the same size. 00:17:48.410 --> 00:17:50.410 If they were different values it would either flare out in a cone 00:17:50.410 --> 00:17:51.570 or narrow in or whatever. 00:17:51.570 --> 00:17:53.570 And so for each of these we're going to specify 00:17:53.570 --> 00:17:55.660 .05. 00:17:55.660 --> 00:17:59.280 That's not 0.5, that's .05. 00:17:59.280 --> 00:18:01.280 We're going to do that for both the start 00:18:01.280 --> 00:18:03.280 and the end width. 00:18:04.150 --> 00:18:06.150 Once that is done we are going 00:18:06.150 --> 00:18:09.050 to go ahead and disable this component. 00:18:09.050 --> 00:18:11.050 And the way that we're going to disable this component 00:18:11.050 --> 00:18:13.280 is with this checkbox right here. 00:18:13.280 --> 00:18:15.280 Right next to the name Line Renderer I'm 00:18:15.280 --> 00:18:16.050 going to turn it off 00:18:16.050 --> 00:18:18.050 And the reason I'm going to do that is the game doesn't 00:18:18.050 --> 00:18:20.050 start with you shooting, the game starts with you 00:18:20.050 --> 00:18:22.050 not shooting so I'm going to turn it off and only 00:18:22.050 --> 00:18:24.050 turn it on once the time comes that 00:18:24.050 --> 00:18:25.400 you have actually fired. 00:18:25.400 --> 00:18:27.400 Now when we fire the gun what we want to do is 00:18:27.400 --> 00:18:29.400 we want to have the gun flash and light things up 00:18:29.400 --> 00:18:31.400 and in order to do that we need a 00:18:31.400 --> 00:18:33.400 light so I'm going to go ahead and 00:18:33.400 --> 00:18:35.400 collapse Line Renderer and now I'm 00:18:35.400 --> 00:18:37.400 going to add a light component. 00:18:37.400 --> 00:18:40.580 So I'm going to click the Add Component button 00:18:41.270 --> 00:18:42.960 and I'm going to go to 00:18:42.960 --> 00:18:46.060 Rendering and I'm going to select Light. 00:18:46.750 --> 00:18:48.750 What that's going to do is add a light 00:18:48.750 --> 00:18:51.330 to my gun, which we can see there. 00:18:51.330 --> 00:18:53.330 What we want to do now is give this 00:18:53.330 --> 00:18:55.330 the settings that will be appropriate 00:18:55.330 --> 00:18:56.690 to this particular gun. 00:18:56.690 --> 00:18:59.680 So the first thing to do is choose a colour 00:18:59.680 --> 00:19:01.250 and we have this little eye dropper, 00:19:01.250 --> 00:19:03.250 we can use the eye dropper just like you'd expect, 00:19:03.250 --> 00:19:04.610 pick a colour out of something, 00:19:04.610 --> 00:19:06.610 or we can just click next to it 00:19:06.610 --> 00:19:09.340 and actually use the gradient/color picker 00:19:09.340 --> 00:19:10.590 to pick a color. 00:19:10.590 --> 00:19:12.590 I'm going to pick a yellowish color. 00:19:12.590 --> 00:19:15.120 If you want to pick a different color go for it. 00:19:15.120 --> 00:19:17.120 But I kind of want my light to be the same color 00:19:17.120 --> 00:19:19.120 as my line renderer so I'm going with yellow there. 00:19:20.020 --> 00:19:22.720 So now when we fire the weapon 00:19:22.720 --> 00:19:24.720 there will in fact be a yellow light. 00:19:24.720 --> 00:19:26.720 Now again like the line renderer we're not 00:19:26.720 --> 00:19:28.720 starting firing so we're going to 00:19:28.720 --> 00:19:30.720 disable the light. 00:19:31.210 --> 00:19:33.210 So we'll just uncheck it there 00:19:33.210 --> 00:19:35.210 and the light will be turned off until 00:19:35.210 --> 00:19:37.210 we turn it on when we fire. 00:19:37.210 --> 00:19:39.210 And so the last little bit we're going to 00:19:39.210 --> 00:19:41.420 add is we need an audio source 00:19:41.420 --> 00:19:44.250 so the gun can play the firing sound. 00:19:44.250 --> 00:19:47.080 So what we're going to do is click Add Component. 00:19:47.080 --> 00:19:50.750 We're going to choose Audio and Audio Source. 00:19:51.340 --> 00:19:54.970 Now once the audio source is on the game object, 00:19:54.970 --> 00:19:56.970 again just like we've done in the past we're going 00:19:56.970 --> 00:19:59.590 to use the circle select picker 00:19:59.590 --> 00:20:03.170 and we are going to locate Player GunShot. 00:20:03.170 --> 00:20:05.170 Double click to add that and we are going to 00:20:05.170 --> 00:20:07.170 uncheck Play On Wake. 00:20:07.170 --> 00:20:09.170 We also want to be sure 00:20:09.170 --> 00:20:11.170 that we do not have loop because 00:20:11.170 --> 00:20:13.170 that will just drive everybody nuts. 00:20:13.170 --> 00:20:15.570 So no play on awake, no looping, 00:20:15.570 --> 00:20:16.940 That would be completely unnecessary. 00:20:16.940 --> 00:20:19.640 At this point the gun is setup. 00:20:19.640 --> 00:20:21.640 So now we can add the scripts 00:20:21.640 --> 00:20:23.640 that's going to allow the player to attack the enemy. 00:20:23.640 --> 00:20:26.600 So to recap, the enemy has health, 00:20:26.600 --> 00:20:28.600 we've added the health script to the enemy which will 00:20:28.600 --> 00:20:31.620 control when the enemy can attack and how the enemy dies. 00:20:31.620 --> 00:20:35.880 The gun has particle effects, lights, line renderers and sound. 00:20:35.880 --> 00:20:37.880 Now the last piece of this puzzle 00:20:37.880 --> 00:20:39.880 is to give the player a script that's 00:20:39.880 --> 00:20:41.880 going to allow the player to actually 00:20:41.880 --> 00:20:44.720 fire the gun and harm the enemy. 00:20:44.720 --> 00:20:47.910 So in the Scripts - Player folder 00:20:49.660 --> 00:20:53.560 we're going to look for the PlayerShooting script. 00:20:53.560 --> 00:20:55.560 And just like the previous scripts we're going to 00:20:55.560 --> 00:20:57.950 click and drag this 00:20:59.310 --> 00:21:01.310 and we are going to place it on the GunBarrelEnd, 00:21:01.310 --> 00:21:03.950 not on the Player, 00:21:03.950 --> 00:21:05.310 on the GunBarrelEnd. 00:21:05.310 --> 00:21:07.910 So if we select the GunBarrelEnd we should see 00:21:07.910 --> 00:21:09.910 the PlayerShooting script there. 00:21:09.910 --> 00:21:11.910 So again ensure that it is not 00:21:11.910 --> 00:21:15.620 on the Player, it is on the GunBarrelEnd. 00:21:15.620 --> 00:21:17.480 And now let's go ahead and open up the PlayerShooting script. 00:21:17.480 --> 00:21:20.530 Save your scene first. And let's look at what this script does. 00:21:20.530 --> 00:21:22.530 The first thing we have is 00:21:22.530 --> 00:21:24.950 these public variable declarations here, 00:21:24.950 --> 00:21:27.040 the first is public in damagerPerShot 00:21:27.040 --> 00:21:29.040 and we can see that every bullet is going to do 00:21:29.040 --> 00:21:30.870 20 points of damage. 00:21:30.870 --> 00:21:34.030 We also have a public float timeBetweenBullets 00:21:34.030 --> 00:21:36.030 which again is going to control how quickly 00:21:36.030 --> 00:21:39.010 our gun can fire, obviously we reduce that value 00:21:39.010 --> 00:21:40.680 if we want to make our gun fire more quickly. 00:21:40.680 --> 00:21:42.680 Then we have public float range 00:21:42.680 --> 00:21:44.680 and that's how far bullets can go. 00:21:44.680 --> 00:21:47.280 In this case they're going to be able to go 100 units 00:21:47.280 --> 00:21:49.790 which is actually a really far distance. 00:21:49.790 --> 00:21:51.530 Then we've got some private variables here 00:21:51.530 --> 00:21:53.350 the first is a float timer, 00:21:53.350 --> 00:21:55.350 and the float timer, just like the enemy attack, 00:21:55.350 --> 00:21:56.880 it's going to keep everything in sync, 00:21:56.880 --> 00:21:59.560 it'll make sure we can only attack when the time is right. 00:21:59.560 --> 00:22:02.970 Then we have a Ray shootRay. 00:22:02.970 --> 00:22:04.620 If you recall we have a gun, 00:22:04.620 --> 00:22:06.620 we're firing so we're going to use this ray to actually 00:22:06.620 --> 00:22:09.920 raycast out and figure out what it is we've hit 00:22:09.920 --> 00:22:13.010 with these bullets, and that's how we're going to hit things. 00:22:13.010 --> 00:22:15.550 We then have a RaycastHit variable called shootHit, 00:22:15.550 --> 00:22:17.550 which is going to return back to us 00:22:17.550 --> 00:22:19.390 whatever it is that we've hit. 00:22:19.390 --> 00:22:22.160 We're then going to have an int shootableMask. 00:22:22.160 --> 00:22:24.790 So we remember the floorMask which dictated that the 00:22:24.790 --> 00:22:27.690 raycast from the camera could only click on the floor. 00:22:27.690 --> 00:22:29.690 So what we're going to have is a shootable mask 00:22:29.690 --> 00:22:32.550 to make sure that we can only hit shootable things, 00:22:32.550 --> 00:22:35.090 we only want to shoot things that we can actually shoot. 00:22:35.090 --> 00:22:37.090 Then we have a particle system referenced 00:22:37.090 --> 00:22:39.980 to gunParticles, you'll recall that's the particle component 00:22:39.980 --> 00:22:41.740 that we added in our previous step, 00:22:41.740 --> 00:22:43.450 so that just gives us a reference to it. 00:22:43.450 --> 00:22:45.450 Here's our reference to our line renderer called 00:22:45.450 --> 00:22:49.320 gunLine, right, so again just so we can reference this in the script. 00:22:49.320 --> 00:22:52.370 Our audio source reference called gunAudio. 00:22:52.370 --> 00:22:54.960 Our light reference called gunLight. 00:22:54.960 --> 00:22:58.430 And then our float effectsDisplayTime, which is 00:22:58.430 --> 00:23:00.220 how long these effects are going to be 00:23:00.220 --> 00:23:02.560 viewable before they disappear. 00:23:02.560 --> 00:23:04.560 In our awake function we're going to setup all of our 00:23:04.560 --> 00:23:06.560 references so we want to 00:23:06.560 --> 00:23:09.490 set out shootableMask to the appropriate values 00:23:09.490 --> 00:23:12.530 by saying LayerMask.GetMask ("Shootable") ; 00:23:12.530 --> 00:23:14.530 What this is going to return back to us is 00:23:14.530 --> 00:23:16.530 the number of our shootable layer. 00:23:16.530 --> 00:23:19.700 You'll recall the level or the obstacles and everything 00:23:19.700 --> 00:23:22.650 is on the shootable layer and the Zombunny we 00:23:22.650 --> 00:23:24.170 created is also on the shootable layer. 00:23:24.170 --> 00:23:26.170 So by setting up this mask we can shoot pretty 00:23:26.170 --> 00:23:28.170 much anything that should be shootable. 00:23:28.170 --> 00:23:30.780 Then we have the gun particles which we're going to 00:23:30.780 --> 00:23:34.410 get access to by saying GetComponent 00:23:34.410 --> 00:23:36.910 Then we're going to do the same for line, audio and light, 00:23:36.910 --> 00:23:39.010 just gunLine = GetComponent 00:23:39.010 --> 00:23:41.010 gunAudio = GetComponent 00:23:41.010 --> 00:23:43.010 gunLight = GetComponent 00:23:43.010 --> 00:23:45.010 which is just giving us a reference 00:23:45.010 --> 00:23:46.730 so we can directly access those. 00:23:46.730 --> 00:23:48.730 Now in the update function is where we 00:23:48.730 --> 00:23:51.270 control whether or not it is time to shoot. 00:23:51.270 --> 00:23:53.270 So very much like with the enemy attacking 00:23:53.270 --> 00:23:55.270 here we have a function that's going to manage that. 00:23:55.270 --> 00:23:57.270 So the first thing we're going to do is 00:23:57.270 --> 00:23:59.270 accumulate your time and we do that by 00:23:59.270 --> 00:24:01.270 saying time += Time.deltaTime. 00:24:01.270 --> 00:24:03.270 So we'll basically increase in size 00:24:03.270 --> 00:24:05.270 as time progresses. 00:24:05.270 --> 00:24:06.610 Then we're going to say 00:24:06.610 --> 00:24:12.740 if(Input.GetButton ("Fire1") and you might be thinking 'what is Fire1?' 00:24:12.740 --> 00:24:14.200 Remember earlier we talked about the 00:24:14.200 --> 00:24:17.580 Input.GetAxisHorizontal, Input.GetAxisVerticle, 00:24:17.580 --> 00:24:21.520 these input axis that are built in to Unity for us 00:24:21.520 --> 00:24:23.400 Fire1 is one of those. 00:24:23.400 --> 00:24:25.400 Fire1 automatically maps to the 00:24:25.400 --> 00:24:27.720 left control on your keyboard or 00:24:27.720 --> 00:24:30.080 your mouse0 which is your left mouse button. 00:24:30.080 --> 00:24:31.430 So that happens for you automatically. 00:24:31.430 --> 00:24:33.430 You can override it here but if you don't do anything 00:24:33.430 --> 00:24:35.430 that's built in to Unity, so 00:24:35.430 --> 00:24:37.430 so you can always say Fire1 and that's how you talk 00:24:37.430 --> 00:24:38.920 about the left mouse button. 00:24:39.830 --> 00:24:41.830 So what we're saying is 00:24:41.830 --> 00:24:43.830 if the player has clicked the left mouse button 00:24:43.830 --> 00:24:47.490 or pressed the left control key 00:24:47.490 --> 00:24:49.150 and 00:24:49.150 --> 00:24:52.290 timer is greater than the delay between our shots. 00:24:52.290 --> 00:24:54.290 So if it's time to shoot and the player wants to shoot 00:24:54.290 --> 00:24:55.490 by clicking the button, 00:24:55.490 --> 00:24:57.490 then we're going to call our function Shoot. 00:24:57.490 --> 00:24:59.490 Shoot is a function we've written further down and 00:24:59.490 --> 00:25:00.900 we'll talk about that in a second. 00:25:00.900 --> 00:25:03.690 So if it's time and you're pressing the button we're going to shoot. 00:25:03.690 --> 00:25:05.690 Then we're going to say if the timer 00:25:05.690 --> 00:25:07.690 is greater than or equal to the 00:25:07.690 --> 00:25:09.690 timeBetterBullets times the 00:25:09.690 --> 00:25:13.630 effectDisplayTime what we'll do is disable the effects. 00:25:13.630 --> 00:25:15.630 So what that means is if we've fired 00:25:15.630 --> 00:25:17.630 and then enough time has progressed we're going 00:25:17.630 --> 00:25:20.440 to turn the light and the line renderer and everything back off. 00:25:20.440 --> 00:25:23.180 So we don't leave it on consistently. 00:25:23.180 --> 00:25:26.030 So the DisableEffects function is public 00:25:26.030 --> 00:25:29.130 which means again it can be A) referenced 00:25:29.130 --> 00:25:33.530 by another script, B) referenced by another script on another game object 00:25:33.530 --> 00:25:36.610 C) accessed by animation events and so on and so forth. 00:25:36.610 --> 00:25:38.870 Public basically gives us a lot of access to it. 00:25:38.870 --> 00:25:40.870 And the function basically says 00:25:40.870 --> 00:25:43.550 the light and the line renderer, 00:25:43.550 --> 00:25:44.990 just disable both of those. 00:25:44.990 --> 00:25:47.080 Again not SetActive but Enabled = false 00:25:47.080 --> 00:25:49.250 because they're components. 00:25:49.250 --> 00:25:51.250 Now the Shoot function itself. 00:25:51.250 --> 00:25:53.250 This is where we do the physics 00:25:53.250 --> 00:25:55.250 of actually firing the bullet and it's 00:25:55.250 --> 00:25:57.820 actually a fairly complex function here 00:25:57.820 --> 00:25:59.820 so we'll step through it nice and easy. 00:25:59.820 --> 00:26:01.820 The first thing we're going to do is reset the timer 00:26:01.820 --> 00:26:03.820 back to 0 because we're firing now and we're going to 00:26:03.820 --> 00:26:06.560 reset the amount of time we have to wait between firing. 00:26:06.560 --> 00:26:08.930 The very next thing we're going to do is play the audio 00:26:08.930 --> 00:26:10.750 and then we're turning the light on. 00:26:10.750 --> 00:26:12.440 Now we don't need to do anything with the light 00:26:12.440 --> 00:26:15.070 or the audio, we basically need to say 00:26:15.070 --> 00:26:17.070 Then what we're going to say is if the 00:26:17.070 --> 00:26:19.810 particles are still playing 00:26:19.810 --> 00:26:21.810 stop them and then start them again. 00:26:21.810 --> 00:26:23.810 What you don't want to happen is you don't 00:26:23.810 --> 00:26:25.810 want to go to play the particles 00:26:25.810 --> 00:26:27.810 have them already be playing 00:26:27.810 --> 00:26:29.810 thus they don't replay and we get 00:26:29.810 --> 00:26:32.600 a disconnect between the visuals of firing 00:26:32.600 --> 00:26:35.790 and the actual raycasting and physics that's happening. 00:26:35.790 --> 00:26:38.780 So we say if the particles are playing stop and start again. 00:26:38.780 --> 00:26:40.780 Also if that were to happen the stop 00:26:40.780 --> 00:26:42.780 and start would be so far your eye wouldn't even pick it up. 00:26:42.780 --> 00:26:46.810 It would just feel like you're just firing really fast. 00:26:46.810 --> 00:26:50.810 Then we're going to say 'let's turn on our line renderer', 00:26:50.810 --> 00:26:53.340 which is the actual visual element of the bullet. 00:26:53.340 --> 00:26:54.910 So we turn that on, so we say 00:26:54.910 --> 00:26:56.600 gunLine.enabled = true. 00:26:56.600 --> 00:26:59.590 Then here comes the tricky part about using lines, 00:26:59.590 --> 00:27:01.270 lines have 2 points right? 00:27:01.270 --> 00:27:02.870 One end and the other end. 00:27:02.870 --> 00:27:04.460 Well the first end we know, it's the end of the gun, 00:27:04.460 --> 00:27:06.080 it's the barrel of the gun, 00:27:06.080 --> 00:27:09.820 so we access that by saying gunLine.SetPosition 0. 00:27:09.820 --> 00:27:11.820 Computers start counting at 0 00:27:11.820 --> 00:27:13.820 so when we say SetPosition 0 we mean 00:27:13.820 --> 00:27:15.820 the first position of the line, 00:27:15.820 --> 00:27:17.820 which is right at the barrel of our gun. 00:27:17.820 --> 00:27:20.330 So we specify that as transform.position 00:27:20.330 --> 00:27:22.330 since the script is on the barrel of the gun. 00:27:22.330 --> 00:27:24.330 So that's the first point. 00:27:24.330 --> 00:27:26.330 But what's the second point? 00:27:26.330 --> 00:27:28.970 We don't know yet, that's the part we need to calculate. 00:27:28.970 --> 00:27:30.750 And that's the part that requires a bit of physics 00:27:30.750 --> 00:27:32.280 and a bit of raycasting. 00:27:32.280 --> 00:27:35.360 So if you recall we create a variable called shootRay, 00:27:35.360 --> 00:27:37.360 which is the raycast from our gun 00:27:37.360 --> 00:27:39.360 and so the first thing we want to do is 00:27:39.360 --> 00:27:41.360 setup this ray so that we can utilise it. 00:27:41.360 --> 00:27:43.360 The ray is going to start 00:27:43.360 --> 00:27:45.360 at the tip of the gun, so 00:27:45.360 --> 00:27:48.590 shootRay.origin = transform.position; 00:27:48.590 --> 00:27:50.590 That's where the ray is starting off. 00:27:50.590 --> 00:27:52.590 A ray starts at one point and 00:27:52.590 --> 00:27:54.590 goes in some direction, so we need to specify 00:27:54.590 --> 00:27:56.590 a direction for our ray, and we say 00:27:56.590 --> 00:28:00.760 shootRay.direction = transform.forward; 00:28:00.760 --> 00:28:03.520 and this is something James has talked about previously, 00:28:03.520 --> 00:28:06.410 we generally treat forward in the Z axis 00:28:06.410 --> 00:28:08.410 as forward, positive on Z axis as forward. 00:28:08.410 --> 00:28:12.490 So as the gun is pointing directly away from the player 00:28:12.490 --> 00:28:13.910 that is forward. 00:28:13.910 --> 00:28:15.910 So if I say transform.forward what I'm saying 00:28:15.910 --> 00:28:17.910 is 'that way', 00:28:17.910 --> 00:28:19.910 so that the bullet will travel 00:28:19.910 --> 00:28:24.550 directly along the line of this gun barrel and this gun. 00:28:24.550 --> 00:28:27.140 So we have our ray setup, at the tip of the gun 00:28:27.140 --> 00:28:29.140 that way, right, forward, so now we need to 00:28:29.140 --> 00:28:31.650 actually use physics to fire it. 00:28:31.650 --> 00:28:33.650 And so let me tell you what's going to happen 00:28:33.650 --> 00:28:35.220 and then we'll walk through the code. 00:28:35.220 --> 00:28:37.220 The idea is that we are going to fire a ray 00:28:37.220 --> 00:28:39.220 forward 100 units, because that's 00:28:39.220 --> 00:28:40.690 how far we say we can fire it. 00:28:40.690 --> 00:28:44.370 If it hits something, what ever it hits is going to be returned 00:28:44.370 --> 00:28:46.370 back to us and we're going to say 'the other end of 00:28:46.370 --> 00:28:48.370 the line is there', whatever it is we hit 00:28:48.370 --> 00:28:50.370 that's the other end and we draw the line. 00:28:50.370 --> 00:28:52.370 If we don't hit anything, 00:28:52.370 --> 00:28:54.370 we still want to fire, but if we don't hit anything 00:28:54.370 --> 00:28:56.010 what we're going to do is say 00:28:56.010 --> 00:28:57.480 'where's the other end of this line? 00:28:57.480 --> 00:28:59.480 It's just way out there' 00:28:59.480 --> 00:29:01.480 and it's actually such a long line 00:29:01.480 --> 00:29:03.480 there's literally no way to see 00:29:03.480 --> 00:29:04.810 the end of it so you just think 00:29:04.810 --> 00:29:06.810 'cool, he's just firing', you don't really notice. 00:29:06.810 --> 00:29:09.550 So we need to say shoot a ray, if it hits something 00:29:09.550 --> 00:29:11.550 that's what we hit, and if it doesn't 00:29:11.550 --> 00:29:13.550 then just draw a really long line. 00:29:13.550 --> 00:29:15.030 We do that by saying 00:29:15.030 --> 00:29:18.960 if(Physics.Raycast, so this again is that raycast function, 00:29:18.960 --> 00:29:20.810 and we're parsing in to it the ray that we specify, 00:29:20.810 --> 00:29:22.480 we're saying 'that way'. 00:29:22.480 --> 00:29:24.480 And then we're going to say 'give us information 00:29:24.480 --> 00:29:26.690 about what it is that we've hit' 00:29:26.690 --> 00:29:28.690 and that's why we have this Out keyword 00:29:28.690 --> 00:29:29.970 and the shootHit variable. 00:29:29.970 --> 00:29:31.970 If we hit something the variable shootHit will be 00:29:31.970 --> 00:29:33.970 like 'this is what you hit' 00:29:33.970 --> 00:29:36.780 cool, we need that information so it's important to have. 00:29:36.780 --> 00:29:38.780 Then we specify the range, and the range 00:29:38.780 --> 00:29:40.780 is 100f, it's something that we specified 00:29:40.780 --> 00:29:42.780 up at the top, it's a variable. 00:29:42.780 --> 00:29:45.090 Finally we parse in our shootable mask. 00:29:45.090 --> 00:29:47.090 Meaning this ray can only hit 00:29:47.090 --> 00:29:49.090 the things we want this ray to be able to hit, 00:29:49.090 --> 00:29:50.380 shootable things. 00:29:50.380 --> 00:29:53.030 We're not interested in anything else. 00:29:53.030 --> 00:29:56.160 If that ray hits something 00:29:56.160 --> 00:29:58.160 we're going to go in to this code. 00:29:58.160 --> 00:30:01.220 if it hits something what we're going to do 00:30:01.220 --> 00:30:03.220 is we are going to get from 00:30:03.220 --> 00:30:05.500 it the enemy health script. 00:30:05.500 --> 00:30:07.030 I'm going to talk about that here in a second. 00:30:07.030 --> 00:30:08.180 So what we're going to say is 00:30:08.180 --> 00:30:10.180 shootHit was going to have whatever 00:30:10.180 --> 00:30:12.180 it is we hit, we know we hit something. 00:30:12.180 --> 00:30:13.740 So we're going to say 00:30:13.740 --> 00:30:17.580 shootHit.collider.GetComponent 00:30:17.580 --> 00:30:19.580 So whatever it is I shoot I'm going to say 00:30:19.580 --> 00:30:21.580 give me your enemy health script and I'm going to store it. 00:30:21.580 --> 00:30:23.580 We need the enemy health script so we can 00:30:23.580 --> 00:30:26.980 reduce the life of the enemy. 00:30:26.980 --> 00:30:28.980 But what if we shot a wall? 00:30:28.980 --> 00:30:30.590 Or the ground? 00:30:30.590 --> 00:30:31.890 Or some Legos? 00:30:31.890 --> 00:30:33.890 Those do not have an enemy health script, 00:30:33.890 --> 00:30:36.260 those are not able to 00:30:36.260 --> 00:30:38.260 have health taken away from them. 00:30:38.260 --> 00:30:40.640 So if we attempt to access that enemy health script 00:30:40.640 --> 00:30:42.640 right away it's not going to work 00:30:42.640 --> 00:30:44.640 and we're going to get an error and the whole thing's 00:30:44.640 --> 00:30:45.820 going to stop working. 00:30:45.820 --> 00:30:47.820 So what we need to do next is 00:30:47.820 --> 00:30:50.100 we need to say if the enemy health script 00:30:50.100 --> 00:30:51.540 does not equal null, 00:30:51.540 --> 00:30:53.540 this is specifically in here to 00:30:53.540 --> 00:30:55.540 show you that this 00:30:55.540 --> 00:30:58.370 function will work even if that component does not 00:30:58.370 --> 00:31:01.290 It's just going to give you null, and null means nothing. 00:31:01.290 --> 00:31:03.940 It means one does not exist. 00:31:03.940 --> 00:31:05.940 So now we need to check it. We need today 00:31:05.940 --> 00:31:07.940 'hey, by the way, if that 00:31:07.940 --> 00:31:10.480 actually exists we're going to do 00:31:10.480 --> 00:31:12.830 enemyHealth.TakeDamage. 00:31:12.830 --> 00:31:14.830 We're going to parse in our damagePerShot 00:31:14.830 --> 00:31:16.830 and exactly where we hit it, 00:31:16.830 --> 00:31:18.830 that's a really nice thing about raycasting too, 00:31:18.830 --> 00:31:20.830 instead of just having a generic effect 00:31:20.830 --> 00:31:22.830 if we shoot on the shoulder that's where 00:31:22.830 --> 00:31:24.830 we hit it, the shoulder, wherever, 00:31:24.830 --> 00:31:26.830 we get to put the effects exactly 00:31:26.830 --> 00:31:28.830 where we hit it, which is pretty neat. 00:31:28.830 --> 00:31:30.830 If you remember in the enemy health script 00:31:30.830 --> 00:31:32.830 TakeDamage, if I just jump back to that, 00:31:32.830 --> 00:31:34.830 very briefly. 00:31:35.340 --> 00:31:40.070 We can see that there is a vector3 hitPoint. 00:31:40.070 --> 00:31:42.070 So you can see that TakeDamage here 00:31:42.070 --> 00:31:44.070 needs to have a vector3, needs to have a position 00:31:44.070 --> 00:31:46.070 in order to move the hitParticles to 00:31:46.070 --> 00:31:48.070 where they need to be 00:31:48.070 --> 00:31:50.070 so in PlayerShooting that's exactly what we 00:31:50.070 --> 00:31:51.640 provide using that raycast. 00:31:51.640 --> 00:31:56.400 Whether or not whatever we've shot has an enemy health script, 00:31:56.400 --> 00:31:59.120 regardless of whatever it is we still hit something 00:31:59.120 --> 00:32:04.170 and thus we say gunLine.SetPosition (1), meaning the second point 00:32:04.170 --> 00:32:06.170 and we parse in that point that we hit. 00:32:06.170 --> 00:32:08.170 So whether we're hitting an enemy or the wall or 00:32:08.170 --> 00:32:09.710 some Legos or whatever, 00:32:09.710 --> 00:32:11.410 we now have our line 00:32:11.410 --> 00:32:14.330 the beginning of the gun to the object that we hit. 00:32:14.330 --> 00:32:15.280 1 line. 00:32:15.280 --> 00:32:17.280 When we were prototyping this game 00:32:17.280 --> 00:32:19.280 we just had the raycast shoot by 00:32:19.280 --> 00:32:21.280 100 units but then we realised that actually it makes more 00:32:21.280 --> 00:32:23.280 sense if the gunLine stops at 00:32:23.280 --> 00:32:25.280 that point, we could expand upon 00:32:25.280 --> 00:32:27.280 the idea to have effects like ricochets 00:32:27.280 --> 00:32:29.280 and things like that but we didn't want to get too bogged down. 00:32:29.280 --> 00:32:31.280 So actually what this is saying is 00:32:31.280 --> 00:32:33.280 if we hit something 00:32:33.280 --> 00:32:35.580 then we end that line renderers second 00:32:35.580 --> 00:32:37.580 point or it's end point at 00:32:37.580 --> 00:32:39.580 the point at which the ray hit it, 00:32:39.580 --> 00:32:41.580 so when we shoot a bit of the environment 00:32:41.580 --> 00:32:43.580 it's going to stop at that point, 00:32:43.580 --> 00:32:45.580 likewise with the enemies. 00:32:45.580 --> 00:32:47.580 So all of that happens 00:32:47.580 --> 00:32:49.580 if we hit something. 00:32:49.580 --> 00:32:52.260 What if we don't hit something? That's our else. 00:32:52.260 --> 00:32:55.170 We're going to say gunLine.SetPosition 00:32:55.170 --> 00:32:57.170 1 again because it's the second point, 00:32:57.170 --> 00:32:59.170 the second part of the line, and what we're 00:32:59.170 --> 00:33:04.660 going to say is shootRay.origin, so where the ray starts, 00:33:04.660 --> 00:33:09.260 + shootRay.direction * range; 00:33:09.260 --> 00:33:11.260 So what that's going to mean is it'll take 00:33:11.260 --> 00:33:15.170 the point and then it's going to multiply (0, 0, 1) 00:33:15.170 --> 00:33:17.170 because that's forward in the Z direction, 00:33:17.170 --> 00:33:19.170 by 100, which is the range, and it's going to say 00:33:19.170 --> 00:33:21.170 this point and then take it way down there 00:33:21.170 --> 00:33:23.170 and then that point, and then it's going to draw a line. 00:33:23.170 --> 00:33:25.170 So it's really simple, it'll say 00:33:25.170 --> 00:33:28.340 one end, the other end, we're going to draw a line between it. 00:33:28.340 --> 00:33:30.340 So a lot of physics stuff in there but basically 00:33:30.340 --> 00:33:32.340 we're just attempting to point at something 00:33:32.340 --> 00:33:34.340 and if we're successful at 00:33:34.340 --> 00:33:36.340 pointing at something we're going to draw a line to it 00:33:36.340 --> 00:33:38.970 and take damage from it, otherwise we're just going to draw the line. 00:33:38.970 --> 00:33:40.970 So the else there effectively 00:33:40.970 --> 00:33:42.970 is giving us the ability to shoot in 00:33:42.970 --> 00:33:44.700 a direction if we don't hit something on the 00:33:44.700 --> 00:33:45.710 on the shootable layer. 00:33:45.710 --> 00:33:48.360 So if you remember that the level is basically a box, 00:33:48.360 --> 00:33:50.360 if you're aiming around in the other direction 00:33:50.360 --> 00:33:52.360 and you're effectively raycasting off 00:33:52.360 --> 00:33:54.360 of the screen you still want to see the 00:33:54.360 --> 00:33:56.360 gun fire so we're just giving it a range 00:33:56.360 --> 00:33:58.360 of 100, which is outside of the camera 00:33:58.360 --> 00:34:01.800 frustum, the view that you can see so we're 00:34:01.800 --> 00:34:04.400 just spraying bullets around even though we don't hit anything. 00:34:06.970 --> 00:34:08.970 What we'll do now that the gun barrel 00:34:08.970 --> 00:34:10.970 script is on there is save our scene 00:34:10.970 --> 00:34:12.220 if you haven't saved in a while. 00:34:12.220 --> 00:34:14.220 Now with the player selected 00:34:14.220 --> 00:34:16.220 in the hierarchy we're going to 00:34:16.220 --> 00:34:18.220 look at the Apply button in the upper 00:34:18.220 --> 00:34:19.180 right hand corner 00:34:19.180 --> 00:34:21.180 You'll recall we clicked and dragged the player 00:34:21.180 --> 00:34:23.180 and turned it in to a prefab but the 00:34:23.180 --> 00:34:25.180 prefab that we have on file is now 00:34:25.180 --> 00:34:27.910 slightly different than the prefab we have in our scene. 00:34:27.910 --> 00:34:29.910 The one on file doesn't have all the 00:34:29.910 --> 00:34:30.830 changes we just made 00:34:30.830 --> 00:34:32.830 If we would like to update the one we have 00:34:32.830 --> 00:34:34.830 on file we have to click the Apply button 00:34:34.830 --> 00:34:36.830 in the upper right hand corner. 00:34:36.830 --> 00:34:39.160 That will apply all of the changes we've made 00:34:39.160 --> 00:34:41.430 to the copy we have on file, 00:34:41.430 --> 00:34:43.430 thus saving it, if you will. 00:34:43.430 --> 00:34:45.430 The way that you notice the difference between 00:34:45.430 --> 00:34:48.010 a prefab and a prefab that 00:34:48.010 --> 00:34:50.010 needs updating is that somethings will appear 00:34:50.010 --> 00:34:52.010 in bold, so if you note the difference between 00:34:52.010 --> 00:34:54.010 Player Movement's properties and 00:34:54.010 --> 00:34:56.010 Player Health's which is added after 00:34:56.010 --> 00:34:58.010 that we made it in to a prefab 00:34:58.010 --> 00:35:01.400 you'll see that these are in bold and when I update 00:35:01.400 --> 00:35:04.370 or apply the changes and look back down 00:35:04.370 --> 00:35:06.370 some of them change so that the things 00:35:06.370 --> 00:35:08.850 that are still bold are things that are in the scene. 00:35:09.330 --> 00:35:11.330 So now we know that's up to date we can carry on 00:35:11.330 --> 00:35:13.330 using it in other scenes and if 00:35:13.330 --> 00:35:15.920 there are instances of this prefab elsewhere 00:35:15.920 --> 00:35:17.920 then you would see those changes are 00:35:17.920 --> 00:35:19.700 automatically applied. 00:35:19.700 --> 00:35:21.830 Let's go ahead and everyone 00:35:21.830 --> 00:35:23.830 turn your volume down because we're about to test this. 00:35:23.830 --> 00:35:25.830 Cool, let's see how loud it is. 00:35:28.650 --> 00:35:29.640 Oh yeah! 00:35:30.970 --> 00:35:31.710 That's what I'm talking about, 00:35:31.710 --> 00:35:33.080 that's an American gun right there. 00:35:36.120 --> 00:35:38.120 Just to reiterate, do not worry about 00:35:38.120 --> 00:35:40.120 this error that you will see appearing at the 00:35:40.120 --> 00:35:42.120 bottom of the screen, it's deliberate, 00:35:42.120 --> 00:35:45.150 this actual mistake is a genuine mistake 00:35:45.150 --> 00:35:47.940 that we want to show you how to fix. 00:35:48.600 --> 00:35:50.600 What you will notice is that at the 00:35:50.600 --> 00:35:53.410 bottom of the screen you have an error. 00:35:53.410 --> 00:35:55.800 So just to give you some insight on 00:35:55.800 --> 00:35:57.800 catching errors in Unity. 00:35:57.800 --> 00:35:59.800 Sometimes there'll be a compile error, 00:35:59.800 --> 00:36:01.800 in that you've written something wrong in a script 00:36:01.800 --> 00:36:03.800 and the bottom of the screen 00:36:03.800 --> 00:36:06.810 will show you where and what in that script has happened. 00:36:07.520 --> 00:36:10.950 And this is all contained within the console. 00:36:10.950 --> 00:36:12.950 So if you go to Window - Console 00:36:12.950 --> 00:36:14.950 you will see that there are a lot 00:36:14.950 --> 00:36:17.760 of errors saying all about the same thing, 00:36:17.760 --> 00:36:20.200 'set destination can only be called 00:36:20.200 --> 00:36:22.200 on an active agent that has been 00:36:22.200 --> 00:36:24.200 placed on a nav mesh'. 00:36:24.200 --> 00:36:26.200 So remember that the word 'agent' 00:36:26.200 --> 00:36:28.200 is referring to the enemy, 00:36:28.200 --> 00:36:31.320 the enemy has that nav mesh agent component 00:36:31.320 --> 00:36:33.780 and set destination is the function of code 00:36:33.780 --> 00:36:36.750 that we used to tell it to seek out the player. 00:36:36.750 --> 00:36:40.290 So we said nav.setDestination player.position. 00:36:41.260 --> 00:36:43.260 What is actually happening there is that there is 00:36:43.260 --> 00:36:46.350 a problem in our Enemy Movement script. 00:36:47.390 --> 00:36:49.390 What we can do is double click on that 00:36:49.390 --> 00:36:51.390 error in the console. 00:36:51.390 --> 00:36:53.390 Watch that one more time, I double click that and it 00:36:53.390 --> 00:36:56.550 takes me to what's causing that error. 00:36:57.230 --> 00:36:59.230 And if you recall earlier we mentioned 00:36:59.230 --> 00:37:01.230 that there are some bits of code that are 00:37:01.230 --> 00:37:04.440 commented out, that are disabled currently in this. 00:37:04.440 --> 00:37:07.400 When we first made the enemy chase after you 00:37:07.400 --> 00:37:09.400 we didn't have a concept of 00:37:09.400 --> 00:37:11.400 the enemy's health or the player's health. 00:37:11.400 --> 00:37:13.400 So having those things in our script 00:37:13.400 --> 00:37:15.400 wouldn't have made any sense. 00:37:16.010 --> 00:37:19.000 So now we do have those things. 00:37:19.000 --> 00:37:22.560 The player can be killed or the enemy can be killed 00:37:22.560 --> 00:37:25.340 and if it is then 00:37:25.340 --> 00:37:27.080 setting it's destination on the nav mesh 00:37:27.080 --> 00:37:29.080 doesn't make any sense any more 00:37:29.080 --> 00:37:31.080 so it's creating an error. 00:37:31.080 --> 00:37:33.080 So now what we can do is we can 00:37:33.080 --> 00:37:35.080 bring back those references to 00:37:35.080 --> 00:37:37.740 the enemy's health and the player's health 00:37:37.740 --> 00:37:41.940 and every time we do an update 00:37:41.940 --> 00:37:43.940 we want to check if both the player and the enemy 00:37:43.940 --> 00:37:46.220 are alive before we say 00:37:47.030 --> 00:37:49.030 setDesination, because if they're not 00:37:49.030 --> 00:37:50.220 then we're in trouble. 00:37:50.220 --> 00:37:51.850 I'm going to use a shortcut here 00:37:51.850 --> 00:37:54.950 I'll teach you which is command / 00:37:54.950 --> 00:37:57.990 which is a way of commenting out and entire line. 00:37:57.990 --> 00:37:59.990 That's control / for you PC folk. 00:37:59.990 --> 00:38:02.770 Yes, for PC, and we can do all of this at once. 00:38:03.540 --> 00:38:06.180 We've re-enabled this if statement and 00:38:06.180 --> 00:38:08.180 else structure around this to just check 00:38:08.180 --> 00:38:10.180 those different scripts, so it's checking the 00:38:10.180 --> 00:38:12.180 enemy's health, it's checking the player's health 00:38:12.180 --> 00:38:14.180 and if they're both more than 0 then it should 00:38:14.180 --> 00:38:17.270 keep trying to seek out the player. 00:38:17.270 --> 00:38:20.220 But if they're not then we switch off the nav mesh agent. 00:38:20.220 --> 00:38:23.370 So the error that we were having in Unity 00:38:23.370 --> 00:38:26.310 is saying that setDestination is effectively invalid 00:38:26.310 --> 00:38:28.310 and we're solving that by checking 00:38:28.310 --> 00:38:31.090 if the player is actually dead to do it. 00:38:31.090 --> 00:38:33.090 And likewise if the enemy has enough health 00:38:33.090 --> 00:38:34.750 to be moving around. 00:38:34.750 --> 00:38:36.750 If not we switch off that component 00:38:36.750 --> 00:38:40.340 by using .enabled = false. 00:38:41.190 --> 00:38:44.440 So uncomment those lines in Enemy Movement, 00:38:44.440 --> 00:38:47.790 save the script and then return to Unity. 00:38:47.790 --> 00:38:49.790 Basically all of Enemy Movement should 00:38:49.790 --> 00:38:51.790 now be enabled, you shouldn't have any comments 00:38:51.790 --> 00:38:53.790 in it at all and that should get you to the 00:38:53.790 --> 00:38:55.450 same point as us. 00:38:55.450 --> 00:38:58.080 Okay, so the last part of this 00:38:58.080 --> 00:39:01.750 is to look at the Player Health script. 00:39:02.490 --> 00:39:04.490 What we're going to do is 00:39:04.490 --> 00:39:06.490 reopen the Player Health script, 00:39:06.490 --> 00:39:08.370 so if you double click on it 00:39:08.370 --> 00:39:10.370 so go in to Scripts - Player 00:39:10.370 --> 00:39:12.370 double click on the icon to open it. 00:39:13.560 --> 00:39:15.560 And you'll see that 00:39:16.410 --> 00:39:18.410 on the player's death 00:39:19.510 --> 00:39:21.090 we've got some commented stuff here, 00:39:21.090 --> 00:39:23.090 so we want to be able to put that back in 00:39:23.090 --> 00:39:24.950 so that when the player dies 00:39:24.950 --> 00:39:26.440 he can no longer shoot his gun. 00:39:26.440 --> 00:39:29.170 But we haven't got our references to PlayerShooting yet. 00:39:29.170 --> 00:39:31.170 Because it's turning red it's saying 00:39:31.170 --> 00:39:33.170 'I don't know what PlayerShooting is and I 00:39:33.170 --> 00:39:34.370 don't know what DisableEffects is'. 00:39:34.370 --> 00:39:36.370 It can't find the reference to that so it's 00:39:36.370 --> 00:39:38.830 turning red to alert us that there's a problem. 00:39:38.830 --> 00:39:40.830 So on line 19 00:39:41.300 --> 00:39:44.600 PlayerShooting, reference to the PlayerShooting script 00:39:44.600 --> 00:39:46.320 which we've named the same. 00:39:46.320 --> 00:39:48.030 So this can cause some confusion. 00:39:48.030 --> 00:39:50.030 We've got PlayerMovement, PlayerMovement 00:39:50.030 --> 00:39:52.030 remember this is the type, which is the name 00:39:52.030 --> 00:39:54.600 of the script, and this is the name of the variable. 00:39:54.600 --> 00:39:56.570 Names of variables can of course be anything 00:39:56.570 --> 00:39:58.570 you want them to be but if I name PlayerMovement 00:39:58.570 --> 00:40:01.700 Blah I will have to write Blah anywhere else. 00:40:01.700 --> 00:40:03.150 But we're not going to do that. 00:40:03.150 --> 00:40:05.960 We've named it something sensible, which means it's going to 00:40:05.960 --> 00:40:08.550 sync up with the other instances and work. 00:40:08.550 --> 00:40:10.550 So the last thing we need to uncomment is 00:40:10.550 --> 00:40:12.550 the GetComponentInChildren 00:40:12.550 --> 00:40:14.550 for PlayerShooting because remember 00:40:14.550 --> 00:40:16.550 that the PlayerShooting script is on the 00:40:16.550 --> 00:40:18.790 GunBarrelEnd, it's not on the Player 00:40:18.790 --> 00:40:21.290 so we need to find that component in the children. 00:40:21.290 --> 00:40:23.290 And now we've got our references and we've already 00:40:23.290 --> 00:40:28.100 uncommented the codes about shooting when dead. 00:40:28.100 --> 00:40:30.100 They're no longer red. 00:40:30.100 --> 00:40:32.100 So it should all work. 00:40:32.100 --> 00:40:34.100 Make sure that you save your script. 00:40:34.100 --> 00:40:37.240 It's very important, and switch back to Unity. 00:40:37.890 --> 00:40:40.900 You can then go ahead and play 00:40:41.470 --> 00:40:42.520 and test your game. 00:40:44.780 --> 00:40:46.780 Now we can kill them, there's no more error 00:40:46.780 --> 00:40:49.060 about the nav mesh destination. 00:40:49.830 --> 00:40:51.830 It's a very easy game, 1 enemy. 00:40:53.370 --> 00:40:55.660 And we can also be killed by them. 00:41:00.530 --> 00:41:02.530 So that is the end of phase 7.