WEBVTT 00:00:01.761 --> 00:00:04.074 In this phase we're going to create 00:00:04.074 --> 00:00:06.864 this shell prefab, it looks like this 00:00:06.864 --> 00:00:08.864 and has a number of different components, 00:00:08.864 --> 00:00:11.928 it's got a collider, a rigidbody, it has a light component, 00:00:12.802 --> 00:00:15.764 and the idea with this is that obviously we'll 00:00:15.764 --> 00:00:18.883 fire it from the tank, but to demonstrate what it does, 00:00:18.883 --> 00:00:20.883 it has an overlapped sphere 00:00:20.883 --> 00:00:23.601 this is a kind of sphere of damage that it can 00:00:24.100 --> 00:00:26.100 give to any of the tanks that i lands near. 00:00:26.474 --> 00:00:28.474 So in this video what I'm doing is slowly moving it 00:00:28.474 --> 00:00:30.474 closer to the tank and what you should notice is that 00:00:30.474 --> 00:00:33.031 that health is getting reduced by more 00:00:33.031 --> 00:00:35.031 the closer that that bullet happens to be. 00:00:36.016 --> 00:00:38.016 Or at least it will do in a moment. 00:00:38.016 --> 00:00:40.016 Off we go, moving it closer. 00:00:40.016 --> 00:00:42.016 The closer it is the more damage that will be applied. 00:00:42.016 --> 00:00:44.016 Obviously if you hit a tank dead-on then 00:00:44.016 --> 00:00:46.016 that will be a huge amount of damage. 00:00:48.139 --> 00:00:50.139 That's the shell that we're going to make and then in the next 00:00:50.139 --> 00:00:52.139 phase after that we'll handle the actual shooting, 00:00:52.139 --> 00:00:54.139 but first off we're going to handle the damage, 00:00:54.139 --> 00:00:56.139 or the thing that's going to parse damage to 00:00:56.139 --> 00:00:58.139 the TankHealth script that we did before. 00:00:58.749 --> 00:01:00.749 What I would like everyone to do is look in 00:01:00.749 --> 00:01:02.749 the Models folder, in this folder 00:01:02.749 --> 00:01:05.426 you will find a model called Shell. 00:01:06.647 --> 00:01:09.663 If you select it it will look like this in the inspector. 00:01:09.663 --> 00:01:11.663 A little note, if you want to inspect something, 00:01:11.663 --> 00:01:13.663 drag around it, you can just drag in that preview window 00:01:13.663 --> 00:01:15.663 at the bottom of the inspector. 00:01:15.663 --> 00:01:18.400 So what we'll do is drag that in to the hierarchy. 00:01:18.969 --> 00:01:20.969 If we drag and drop that model from 00:01:20.969 --> 00:01:22.620 the project to the hierarchy. 00:01:23.385 --> 00:01:25.385 Move your cursor over the scene view, 00:01:25.385 --> 00:01:27.385 press F to frame selected 00:01:27.385 --> 00:01:29.385 and you'll notice it's at the origin, roughly, 00:01:29.385 --> 00:01:31.385 so it's near to 0. 00:01:31.385 --> 00:01:33.385 So all I'm going to do is just to drag mine 00:01:33.385 --> 00:01:35.385 up slightly so it's somewhere I can 00:01:35.385 --> 00:01:37.848 see it and work on it, something like that is fine. 00:01:39.313 --> 00:01:41.313 Remember because we're creating a prefab that 00:01:41.313 --> 00:01:43.313 we're going to fire with the tank this isn't something that's 00:01:43.313 --> 00:01:45.828 going to be in the scene when the game starts 00:01:45.828 --> 00:01:47.828 just like the tank, so we don't need to worry about 00:01:47.828 --> 00:01:49.828 where it's positioned for now. 00:01:52.019 --> 00:01:54.019 So because we are making 00:01:54.019 --> 00:01:56.019 this something that's going to interact with other objects 00:01:56.019 --> 00:01:59.450 it needs to have a collider. 00:01:59.450 --> 00:02:01.450 We'll use primitive colliders because they're 00:02:01.450 --> 00:02:04.468 nice and performant, and given the shape of this 00:02:04.468 --> 00:02:06.468 we'll use a capsule collider. 00:02:06.468 --> 00:02:08.468 So with that shell still selected 00:02:08.468 --> 00:02:11.279 in the inspector we'll hit the Add Component button 00:02:11.279 --> 00:02:13.279 and I'm just going to type Caps 00:02:13.279 --> 00:02:15.279 for Capsule and then 00:02:15.279 --> 00:02:17.279 click on it to confirm, or hit Return. 00:02:18.472 --> 00:02:19.998 With that capsule collider, 00:02:20.611 --> 00:02:22.611 because when you are firing things 00:02:22.611 --> 00:02:25.729 like a projectile as we are. 00:02:26.589 --> 00:02:28.589 We don't want things to be bouncing off one another 00:02:28.589 --> 00:02:30.589 and we don't expect this tank shell to 00:02:30.589 --> 00:02:32.589 ever hit something and push it back, 00:02:32.589 --> 00:02:34.589 we're expecting it to hit something, 00:02:34.589 --> 00:02:36.589 create a call back in the script and then create 00:02:36.589 --> 00:02:38.589 an explosion force. 00:02:38.589 --> 00:02:40.589 So we don't want any physical interactions. 00:02:41.061 --> 00:02:44.134 And when we don't want those we use what's called triggers. 00:02:44.134 --> 00:02:46.134 If you hear someone refer to a trigger 00:02:46.134 --> 00:02:48.134 or a trigger zone, 00:02:48.134 --> 00:02:50.134 or a trigger area they're usually referring to 00:02:50.134 --> 00:02:52.134 a collider that's been checked 00:02:52.134 --> 00:02:54.134 as a trigger. 00:02:54.134 --> 00:02:56.865 So if you check the Is Trigger checkbox on that, 00:02:57.406 --> 00:03:00.193 what that basically means is that it doesn't have 00:03:00.193 --> 00:03:02.193 it doesn't have a solid surface to it 00:03:02.193 --> 00:03:04.193 it can intersect, but if can send a 00:03:04.193 --> 00:03:05.476 call back to the script saying 00:03:05.476 --> 00:03:07.476 'I've now entered another trigger' or 00:03:07.476 --> 00:03:09.476 'I've entered another collider'. 00:03:09.476 --> 00:03:11.476 So often trigger colliders are used for things like 00:03:11.476 --> 00:03:13.476 doors in games, like when you put a big 00:03:13.476 --> 00:03:15.476 cubic collider in front of 00:03:15.476 --> 00:03:17.476 a doorway and then when the character enters that way 00:03:17.476 --> 00:03:19.476 you automatically open the door. 00:03:19.476 --> 00:03:22.196 It's a pretty classic use of that kind of collider. 00:03:23.168 --> 00:03:25.168 Once you've added that component, the next thing 00:03:25.168 --> 00:03:27.168 we want to do is actually set it up. 00:03:29.068 --> 00:03:31.068 So we need to change the centre, 00:03:31.068 --> 00:03:34.944 so the centre value for that is (0, 0, 0.2). 00:03:36.567 --> 00:03:39.134 And the radius is 0.15. 00:03:41.714 --> 00:03:43.714 And the height is 0.55. 00:03:46.446 --> 00:03:48.726 Crucially you'll notice that it's not 00:03:48.726 --> 00:03:52.713 looking in the right direction, so we set the direction 00:03:52.713 --> 00:03:55.737 to Z axis, and then it should 00:03:55.737 --> 00:03:58.716 pretty much match our shell perfectly. 00:03:58.716 --> 00:04:01.534 Or to an amount of precision that would never cause issues. 00:04:02.227 --> 00:04:04.227 It should look like that. 00:04:05.142 --> 00:04:07.647 And then finally because this is a physics object 00:04:07.647 --> 00:04:09.647 we need it to be under control of 00:04:09.647 --> 00:04:11.968 the physics engine, and so what are we going to add? 00:04:12.576 --> 00:04:14.576 A rigidbody, there we go, nice and loud. 00:04:16.504 --> 00:04:18.504 So add our rigidbody component. 00:04:20.279 --> 00:04:21.232 And it's good to go. 00:04:21.232 --> 00:04:23.232 So these shells are going to fire 00:04:23.232 --> 00:04:25.232 a projectile, they're not going to just keep going straight forward, 00:04:25.232 --> 00:04:27.232 they're going to fall with gravity over time. 00:04:27.925 --> 00:04:29.925 So just a quick recap of those two points. 00:04:30.840 --> 00:04:34.770 Okay, so we've found the model named Shell in the Models folder. 00:04:35.741 --> 00:04:37.741 We dragged it in to the hierarchy. 00:04:38.407 --> 00:04:41.236 We've added a capsule collider component to the shell 00:04:41.849 --> 00:04:43.849 and we've made sure it's a trigger so that that 00:04:43.849 --> 00:04:45.849 trigger won't bounce off of anything 00:04:45.849 --> 00:04:48.479 it's just going to pass straight through stuff 00:04:48.479 --> 00:04:50.479 and then immediately be destroyed by the script 00:04:50.479 --> 00:04:52.187 that we're going to write. 00:04:52.187 --> 00:04:54.861 We've checked Is Trigger to make sure that is the case. 00:04:57.585 --> 00:05:00.103 Then we set the direction to Z axis 00:05:00.103 --> 00:05:02.103 and we changed the centre, so the centre is 00:05:02.103 --> 00:05:04.103 basically where that originates from so you can 00:05:04.103 --> 00:05:06.842 kind of offset your colliders if you need to, art-wise. 00:05:07.434 --> 00:05:11.698 And the radius we've set to 0.15, the radius of the capsule. 00:05:11.698 --> 00:05:13.698 And the height to 0.55 00:05:13.698 --> 00:05:15.698 And we added a rigidbody to make sure that it's 00:05:15.698 --> 00:05:18.390 under the control of the physics engine. 00:05:21.719 --> 00:05:23.719 So we've got the shell 00:05:23.719 --> 00:05:25.719 and currently the shell can fall down 00:05:25.719 --> 00:05:27.719 and nothing is going to really happen. 00:05:27.719 --> 00:05:29.719 It's just going to go straight through the ground 00:05:29.719 --> 00:05:31.719 because it's not going to stop, it hasn't got a 00:05:31.719 --> 00:05:33.192 physical presence, like I said. 00:05:33.192 --> 00:05:35.611 So we don't want that, we want it to actually 00:05:35.611 --> 00:05:38.233 explode and do stuff, so we've created 00:05:38.233 --> 00:05:40.233 a shell explosion for you. 00:05:40.885 --> 00:05:42.935 So if you look in the Prefabs folder 00:05:43.477 --> 00:05:46.121 you will find a prefab called ShellExplosion. 00:05:47.702 --> 00:05:49.115 So what we're going to do with that is make it 00:05:49.115 --> 00:05:50.796 a child of the shell. 00:05:51.350 --> 00:05:53.350 So if you drag from the Prefabs folder and drop 00:05:53.350 --> 00:05:54.575 it on to the shell 00:05:55.711 --> 00:05:56.949 It should look like that. 00:05:58.002 --> 00:06:00.002 And when we simulate it 00:06:01.056 --> 00:06:02.088 it looks like this. 00:06:02.088 --> 00:06:05.159 A cloud of dust that kind of falls over time. 00:06:09.791 --> 00:06:11.791 Okay and then the only other thing that we need 00:06:11.791 --> 00:06:13.791 to do with that is to 00:06:13.791 --> 00:06:15.791 add an audio source, 00:06:15.791 --> 00:06:17.791 so similar to the tank exploding, 00:06:17.791 --> 00:06:20.373 it needs to have visual effects and an audio effect. 00:06:20.887 --> 00:06:22.887 So I'm going to click the Add Component button 00:06:22.887 --> 00:06:24.887 and I'm going to choose Audio Source. 00:06:25.941 --> 00:06:27.941 And then just like before we need an 00:06:27.941 --> 00:06:29.941 audio clip, so I'm going to use the 00:06:29.941 --> 00:06:31.941 circle select next to audio clip 00:06:32.468 --> 00:06:35.154 and choose ShellExplosion to assign it. 00:06:37.179 --> 00:06:39.179 Now we don't want this to play as soon as the shell 00:06:39.179 --> 00:06:41.900 gets instantiated, as soon as the explosion appears, 00:06:42.607 --> 00:06:44.607 so we're going to uncheck Play On Awake. 00:06:45.952 --> 00:06:48.928 A quick recap, so we've got our ShellExplosion prefab, 00:06:48.928 --> 00:06:52.020 we had to make it a child of the Shell game object 00:06:52.020 --> 00:06:54.020 so it's going to travel with it and then do it's 00:06:54.020 --> 00:06:56.020 thing as soon as it hits something. 00:06:56.020 --> 00:06:58.020 We added an audio source to that 00:06:58.020 --> 00:07:01.019 so that it's able to playback sound and we've used 00:07:01.019 --> 00:07:04.499 circle select to set the audio clip to ShellExplosion. 00:07:04.499 --> 00:07:06.499 We don't want it to play as soon as it enters 00:07:06.499 --> 00:07:08.499 the world so we only want it to play 00:07:08.499 --> 00:07:11.814 as soon as it impacts, so we uncheck Play On Awake. 00:07:15.575 --> 00:07:17.575 Okay, the final thing we're going to do with the shell 00:07:17.575 --> 00:07:20.728 so reselect the shell, the parent object, 00:07:20.728 --> 00:07:22.299 is to add a light. 00:07:22.299 --> 00:07:24.299 So as this travels across the 00:07:24.299 --> 00:07:26.711 game board we want this to 00:07:26.711 --> 00:07:28.419 light up the environment 00:07:28.419 --> 00:07:30.419 and it helps pick out the shell, because obviously 00:07:30.419 --> 00:07:32.419 you've got a gold shell, you've got orange sand, 00:07:32.419 --> 00:07:36.352 it's kind of hard to see it and the light helps pick it out 00:07:36.352 --> 00:07:39.107 so instead of creating a point light game object 00:07:39.107 --> 00:07:42.137 we can just add that same light. 00:07:42.137 --> 00:07:44.137 So if you've new to Unity, what you'll notice 00:07:44.137 --> 00:07:46.137 if you go to Game Object - Create 00:07:46.137 --> 00:07:48.137 is that you've got all these readymade 00:07:48.137 --> 00:07:50.824 prefabbed game objects with several components on them. 00:07:50.824 --> 00:07:52.824 So for example if you choose, 00:07:52.824 --> 00:07:54.824 if there was a cube, then you've got 00:07:54.824 --> 00:07:56.824 a bunch of different components on it. 00:07:56.824 --> 00:07:59.776 But you could also create a light 00:08:00.270 --> 00:08:02.270 which has just the light component on it. 00:08:02.599 --> 00:08:04.599 We don't need to do that, we're just going to put the 00:08:04.599 --> 00:08:06.599 component directly on to the shell, 00:08:06.599 --> 00:08:08.599 so I got to Add Component - Light 00:08:09.401 --> 00:08:11.401 So now we have to actually handle 00:08:11.401 --> 00:08:13.401 how that behaves, how it's going to 00:08:13.401 --> 00:08:15.873 impact and what it's going to do when it hits 00:08:15.873 --> 00:08:17.873 a tank or when it hits the environment and the 00:08:17.873 --> 00:08:20.736 kind of forces it's going to apply to other tanks in the game. 00:08:21.621 --> 00:08:23.621 So what we're going to do is to look 00:08:23.621 --> 00:08:25.621 in the Scripts folder and 00:08:25.621 --> 00:08:27.621 find the Shell folder. 00:08:27.621 --> 00:08:31.353 In that you will find a script called ShellExplosion. 00:08:32.752 --> 00:08:35.744 So we're going to drag and drop ShellExplosion on to the Shell 00:08:35.744 --> 00:08:37.744 game object in the hierarchy. 00:08:38.505 --> 00:08:40.154 And then you should see 00:08:40.772 --> 00:08:42.772 that along with everything else, the rigidbody, 00:08:42.772 --> 00:08:45.305 the capsule collider, the light that we just added, 00:08:45.305 --> 00:08:47.783 you've now got Shell Explosion (Script) 00:08:47.783 --> 00:08:50.561 and we're going to double click on that to open it up for editing. 00:08:54.305 --> 00:08:57.246 Okay, so in this one we don't have any private 00:08:57.246 --> 00:09:00.403 variables but we do have a bunch of public ones. 00:09:00.403 --> 00:09:02.403 So the first one is a layer mask. 00:09:02.403 --> 00:09:04.403 Now you might remember at the beginning of the day 00:09:04.403 --> 00:09:07.292 we put all the tanks on the Players layer. 00:09:07.292 --> 00:09:09.292 So that means that when we cause an 00:09:09.292 --> 00:09:11.292 explosion from the shell we can just 00:09:11.292 --> 00:09:13.292 check for everything that's on the Players layer 00:09:13.292 --> 00:09:17.436 and see which tanks we've hit rather than trying to affect everything. 00:09:17.436 --> 00:09:19.436 So that will be available in the inspector 00:09:19.436 --> 00:09:21.436 and we can choose which layer we want to affect. 00:09:22.341 --> 00:09:25.810 The next is the explosion particle, 00:09:25.810 --> 00:09:28.751 remember we just dragged on that as a child game object 00:09:28.751 --> 00:09:30.751 we're going to get a reference to that so we can 00:09:30.751 --> 00:09:32.751 play the particles when the shell explodes. 00:09:33.492 --> 00:09:35.492 Likewise there's an audio source reference, 00:09:35.492 --> 00:09:38.093 so that's for getting a reference to that audio source 00:09:38.093 --> 00:09:39.543 so we can play a sound. 00:09:39.543 --> 00:09:41.543 Then we've got a bunch of floats, 00:09:41.543 --> 00:09:43.543 so the first one is the most 00:09:43.543 --> 00:09:45.543 damage that it can possibly do, 00:09:45.543 --> 00:09:47.206 the maximum damage, and we're setting that to 100. 00:09:47.206 --> 00:09:49.879 so if you get a perfect direct hit 00:09:49.879 --> 00:09:51.879 then you'll kill the tank outright. 00:09:52.198 --> 00:09:54.198 But you can't get a perfect hit because 00:09:54.198 --> 00:09:56.198 it's never going to be exactly on the position 00:09:56.198 --> 00:09:58.198 of the tank because the collider is covering it. 00:09:59.474 --> 00:10:01.474 So you're going to get close but no cigar. 00:10:01.474 --> 00:10:03.474 The next one is the amount of force 00:10:03.474 --> 00:10:06.139 that the explosion is going to do right at it's centre. 00:10:06.916 --> 00:10:08.916 Likewise as the explosion radius 00:10:08.916 --> 00:10:10.916 gets further out the tank will be 00:10:10.916 --> 00:10:13.393 less affected by the explosion, you'll feel less force. 00:10:14.747 --> 00:10:16.747 We've got the max lifetime, so we don't want 00:10:16.747 --> 00:10:18.747 shells to exist forever 00:10:18.747 --> 00:10:20.747 so if they happen to miss everything 00:10:20.747 --> 00:10:22.747 we don't want them to stay in the scene forever so 00:10:22.747 --> 00:10:24.108 we give them a lifetime, 00:10:24.108 --> 00:10:26.108 and at the start of the scene we'll say 00:10:26.510 --> 00:10:28.510 if it's not dead after 2 seconds just kill it anyway. 00:10:29.287 --> 00:10:31.287 So it's pretty unlikely you'll ever see that, 00:10:31.287 --> 00:10:33.287 it would either hit the ground or 00:10:33.287 --> 00:10:35.287 some other object before that ever happened, 00:10:35.287 --> 00:10:37.287 but it's just a failsafe that we'll write in. 00:10:37.745 --> 00:10:39.745 So the last public variable is 00:10:39.745 --> 00:10:41.745 the explosion radius. 00:10:41.745 --> 00:10:43.745 And that is literally how far 00:10:43.745 --> 00:10:46.532 from the shell it's going to affect when it explodes. 00:10:48.391 --> 00:10:50.391 Okay, so let's look at these functions 00:10:50.391 --> 00:10:52.391 and as we're just mentioning 00:10:52.391 --> 00:10:54.391 in the Start function we're saying destroy 00:10:54.391 --> 00:10:56.391 the game object 00:10:56.391 --> 00:10:58.391 after the max lifetime. 00:10:58.391 --> 00:11:01.237 So that's saying when you instantiate this 00:11:01.654 --> 00:11:04.564 if it's still alive after 2 seconds, destroy it. 00:11:06.048 --> 00:11:08.835 And now we're going to have to write some scripts. 00:11:10.902 --> 00:11:12.473 So the first thing we want to do, 00:11:12.473 --> 00:11:14.473 when OnTriggerEnter is called, so you remember we 00:11:14.473 --> 00:11:17.798 put Is Trigger as True, that means that 00:11:17.798 --> 00:11:19.798 OnTriggerEnter is called when something 00:11:19.798 --> 00:11:21.032 bumps in to this. 00:11:21.032 --> 00:11:23.032 So this function will 00:11:23.032 --> 00:11:25.032 happen whenever it intersects with anything 00:11:25.032 --> 00:11:27.032 and then we need to check what it's 00:11:27.032 --> 00:11:29.032 intersected with and what to do as a result. 00:11:29.032 --> 00:11:31.032 So we have this argument 00:11:31.032 --> 00:11:33.409 which we've just called other which is of type Collider. 00:11:33.409 --> 00:11:35.409 So it's going to say 'okay, we've intersected 00:11:35.409 --> 00:11:38.129 with something else, it's a collider, what do we do about it?' 00:11:39.226 --> 00:11:41.226 In this case we don't actually care what we're bumping in to 00:11:41.226 --> 00:11:43.226 just that it's a collider, 00:11:43.226 --> 00:11:45.226 so we should explode. 00:11:45.226 --> 00:11:46.441 But what do we do first? 00:11:46.441 --> 00:11:48.441 First of all we need to gather up all of the 00:11:48.441 --> 00:11:50.441 colliders that we need to affect. 00:11:50.441 --> 00:11:52.441 So we need an array of colliders 00:11:52.441 --> 00:11:54.842 and you'll remember that's the square brackets after a collider. 00:11:55.327 --> 00:11:57.327 And we'll call that array Colliders. 00:11:58.076 --> 00:12:00.076 And we're going to set that to the return of 00:12:00.076 --> 00:12:02.076 a function called OverlapSphere. 00:12:02.076 --> 00:12:05.547 So that's Physics.OverlapSphere 00:12:08.168 --> 00:12:10.944 And what that's going to do is similar to a ray cast, 00:12:10.944 --> 00:12:14.008 it's going to create an imaginary sphere 00:12:14.812 --> 00:12:17.281 and everything that overlaps with that sphere 00:12:17.281 --> 00:12:20.150 or is inside the sphere is going to be collected. 00:12:20.871 --> 00:12:22.871 And we want that sphere to be at 00:12:22.871 --> 00:12:24.871 the shell's location, so transform.position 00:12:24.871 --> 00:12:26.871 for it's position. 00:12:27.412 --> 00:12:29.412 It's going to have a radius that we've already 00:12:29.412 --> 00:12:31.121 got as an ExplosionRadius. 00:12:31.620 --> 00:12:33.620 And we're going to use that TankMask 00:12:33.620 --> 00:12:35.620 so that it's only picking up the tanks. 00:12:40.183 --> 00:12:42.183 Okay, so just a quick note about layers. 00:12:42.183 --> 00:12:44.183 Layers we use for various different things 00:12:44.183 --> 00:12:46.183 in Unity but ostensibly they're 00:12:46.183 --> 00:12:48.183 just for grouping things together in 00:12:48.183 --> 00:12:50.183 some way that you want to use 00:12:50.183 --> 00:12:52.772 to either include of exclude them from something. 00:12:52.772 --> 00:12:54.772 In this case we're using it as a way of 00:12:54.772 --> 00:12:57.706 only including or sort of masking out 00:12:57.706 --> 00:12:59.706 anything but tanks, which is why we've called this 00:12:59.706 --> 00:13:03.925 variable TankMask, because it's essentially a layer on 00:13:03.925 --> 00:13:05.827 which only the tanks exist. 00:13:05.827 --> 00:13:07.827 But don't think of it in any visual terms, 00:13:07.827 --> 00:13:10.341 if anyone's in to Photoshop or anything like that 00:13:10.341 --> 00:13:13.168 there's no kind of layering in that sense. 00:13:13.168 --> 00:13:15.728 Okay, so now we've got all our colliders 00:13:15.728 --> 00:13:17.728 we need to go through them all and 00:13:17.728 --> 00:13:19.728 decide what we're going to do. 00:13:20.186 --> 00:13:22.186 So for that we need to 00:13:22.186 --> 00:13:24.186 loop through them all using a For Loop. 00:13:24.644 --> 00:13:26.644 So this is the same format as we saw before 00:13:27.587 --> 00:13:30.242 for(int i = 0 00:13:30.963 --> 00:13:33.243 i is less than colliders.length 00:13:33.243 --> 00:13:35.777 so this is, colliders is the array that we're going 00:13:35.777 --> 00:13:38.083 through so it's less than colliders.Length. 00:13:38.083 --> 00:13:41.804 And then i++ means that we add to i for every loop. 00:13:43.274 --> 00:13:47.069 Then open and close brackets? 00:13:47.069 --> 00:13:49.069 Brackets? Yes brackets. 00:13:49.568 --> 00:13:51.568 And then we can 00:13:51.568 --> 00:13:53.888 use whatever we do in that for loop 00:13:53.888 --> 00:13:55.888 to affect each of the colliders. 00:13:55.888 --> 00:13:57.888 So the first thing we want to do is check 00:13:57.888 --> 00:13:59.888 whether the thing that we've hit has 00:13:59.888 --> 00:14:01.888 a rigidbody, so what we do is we 00:14:01.888 --> 00:14:04.334 create a rigidbody variable 00:14:04.334 --> 00:14:06.334 which we'll call targetRigidbody 00:14:06.861 --> 00:14:11.315 and we'll set that equal to colliders[i], so that's 00:14:11.315 --> 00:14:13.315 the collider at the position that we're 00:14:13.315 --> 00:14:15.315 currently iterating through in the array, 00:14:16.577 --> 00:14:20.031 .GetComponent, 00:14:20.031 --> 00:14:22.031 so we'll use the angled brackets for rigidbody. 00:14:23.723 --> 00:14:25.723 So what that's going to do is 00:14:25.723 --> 00:14:27.723 get that collider that we've just gone through 00:14:27.723 --> 00:14:30.357 the array and try and find a rigidbody on it. 00:14:31.286 --> 00:14:33.286 Next we need to know if it's actually 00:14:33.286 --> 00:14:36.101 found a rigidbody or if it's just found nothing. 00:14:37.297 --> 00:14:40.112 So what we're going to do is say if 00:14:41.835 --> 00:14:43.835 not target(!Rigidbody) 00:14:43.835 --> 00:14:47.124 so what that's going to do is say 00:14:47.124 --> 00:14:49.515 if there's nothing there then what do I do? 00:14:50.028 --> 00:14:52.028 And if there's nothing there we want to continue 00:14:52.028 --> 00:14:54.028 on to the next loop, on the the next collider. 00:14:54.028 --> 00:14:56.028 You'll remember it's going back to the 00:14:56.028 --> 00:14:58.028 start of the for loop and carrying on looking 00:14:58.028 --> 00:15:00.677 for any collider that's got a rigidbody 00:15:00.677 --> 00:15:03.422 it's not continuing through a list of commands. 00:15:03.422 --> 00:15:05.422 So bear in mind that this is a bit of a 00:15:05.422 --> 00:15:07.422 safety catch kind of thing because we're making it 00:15:07.422 --> 00:15:09.422 everything that's on the Players layer anyway. 00:15:09.783 --> 00:15:11.783 So everything should have a collider 00:15:12.324 --> 00:15:14.324 and everything that has a collider there will also 00:15:14.324 --> 00:15:17.310 have a rigidbody, so we should always find something 00:15:17.310 --> 00:15:19.310 but it's good to check just in case because otherwise 00:15:19.310 --> 00:15:21.310 you're going to get errors. 00:15:21.934 --> 00:15:23.934 So now we know we've got a rigidbody what 00:15:23.934 --> 00:15:26.353 we can do is we can add a force to that rigidbody 00:15:26.353 --> 00:15:32.280 so targitRigidbody.AddExplosionForce ( 00:15:33.754 --> 00:15:35.754 and the force we want to add is the 00:15:35.754 --> 00:15:37.754 ExplosionForce that we've got as a public variable. 00:15:37.754 --> 00:15:40.679 So that's m_ExplosionForce. 00:15:41.428 --> 00:15:44.562 Then comma, so we're on to the next parameter. 00:15:44.562 --> 00:15:46.562 And the position that that explosion 00:15:46.562 --> 00:15:49.598 force is emanating from is transform.position. 00:15:50.375 --> 00:15:52.375 So that's position of the shell. 00:15:53.387 --> 00:15:55.387 And then the last parameter that we're going to use is 00:15:55.387 --> 00:15:57.387 the radius, so how far away 00:15:57.387 --> 00:16:00.412 that explosion force can reach. 00:16:05.835 --> 00:16:09.234 Okay, so we've added the force now 00:16:09.234 --> 00:16:11.234 so the tank is going to move but we still need 00:16:11.234 --> 00:16:13.234 to actually cause some damage to it. 00:16:13.900 --> 00:16:15.579 So we're going to do something very similar. 00:16:15.579 --> 00:16:17.579 We're going to create a TankHealth variable, 00:16:18.522 --> 00:16:20.522 which we'll call targetHealth. 00:16:22.672 --> 00:16:24.672 So just to qualify that, notice that when we've 00:16:24.672 --> 00:16:26.672 made these other variables they've been familiar 00:16:26.672 --> 00:16:28.672 components to things that you've 00:16:28.672 --> 00:16:30.672 added using the Add Component button. 00:16:31.185 --> 00:16:33.185 This time we're making a variable 00:16:33.185 --> 00:16:35.534 of type TankHealth. 00:16:35.534 --> 00:16:38.608 So that just means an instance of the TankHealth script 00:16:38.608 --> 00:16:41.610 on a tank, let's go back from the top. 00:16:41.610 --> 00:16:43.181 So we've gathered all the colliders we could find 00:16:43.181 --> 00:16:45.797 within the imaginary sphere, the overlap sphere. 00:16:46.366 --> 00:16:48.145 And we're looping through 00:16:48.630 --> 00:16:50.630 that list, that array, 00:16:50.630 --> 00:16:53.490 to the amount that there is, colliders found. 00:16:53.490 --> 00:16:55.490 And then with each one we're 00:16:55.490 --> 00:16:57.490 checking that there's a rigidbody and if there is 00:16:57.490 --> 00:16:59.267 we're adding a force, 00:16:59.267 --> 00:17:01.267 and then we want to actually also 00:17:01.267 --> 00:17:03.267 take damage on those tanks. 00:17:03.586 --> 00:17:05.586 So we're addressing a particular 00:17:05.586 --> 00:17:07.586 instance of that TankHealth script so we can 00:17:07.586 --> 00:17:09.586 talk to it and reduce health with it. 00:17:10.793 --> 00:17:13.913 Okay, so we've got TankHealth targetHealth 00:17:13.913 --> 00:17:16.358 and we're going to set that equal to the targetRigidbody. 00:17:17.149 --> 00:17:19.149 GetComponent TankHealth. 00:17:19.482 --> 00:17:21.775 So we've got the rigidbody, we know that now 00:17:22.231 --> 00:17:24.231 because we were able to add a force to it. 00:17:24.675 --> 00:17:26.675 So on the same game object as that 00:17:27.258 --> 00:17:28.831 get the component TankHealth. 00:17:28.831 --> 00:17:31.790 And again we should know that 00:17:31.790 --> 00:17:33.790 there's definitely going to be a TankHealth here 00:17:33.790 --> 00:17:35.790 but we're going to check just to make sure. 00:17:35.790 --> 00:17:37.790 Because if somebody wants to extend the game and they 00:17:37.790 --> 00:17:41.106 make something that's on the Players layer that doesn't have TankHealth 00:17:41.106 --> 00:17:43.106 then this will still work. 00:17:43.106 --> 00:17:45.801 So again instead of if(!rigidbody) we've got 00:17:45.801 --> 00:17:49.890 if(!targetHealth) and then continue. 00:17:49.890 --> 00:17:51.890 So if there's no targetHealth continue 00:17:51.890 --> 00:17:53.890 on to the next iteration. 00:17:54.292 --> 00:17:56.292 So a quick note about this line here. 00:17:56.292 --> 00:17:58.292 So you'll notice that we've talked about 00:17:58.292 --> 00:18:00.292 the fact that we need a reference to this instance 00:18:00.292 --> 00:18:02.292 of the TankHealth script. 00:18:02.292 --> 00:18:04.292 So you might wondering 'well what's that got to do with 00:18:04.292 --> 00:18:07.031 the rigidbody, what's health got to do with physics?' 00:18:07.031 --> 00:18:09.031 Nothing at all, the point is that we've found 00:18:09.031 --> 00:18:11.449 a particular game object 00:18:11.449 --> 00:18:13.449 the fact that we're calling it targetRigidbody 00:18:13.449 --> 00:18:15.449 is not hugely important, we'e just referring to 00:18:15.449 --> 00:18:18.073 the same game object that we've stored in that. 00:18:18.073 --> 00:18:20.073 Often when we talk about rigidbodies 00:18:20.073 --> 00:18:22.972 we'll say 'this is a rigidbody object'. 00:18:22.972 --> 00:18:26.157 All things in the hierarchy are what we call game objects. 00:18:26.157 --> 00:18:28.157 But often what people will say is 00:18:28.157 --> 00:18:29.146 'that's a rigidbody object'. 00:18:29.146 --> 00:18:31.146 It just means it's a game object with that 00:18:31.146 --> 00:18:32.827 particular component attached. 00:18:32.827 --> 00:18:35.591 So when we're talking about the target rigidbody 00:18:35.591 --> 00:18:37.591 we just mean it's the tank game object that 00:18:37.591 --> 00:18:40.455 we know has a rigidbody attached to it. 00:18:40.455 --> 00:18:42.455 So we've done that check and we can then 00:18:42.455 --> 00:18:44.455 say we know it's the right game object so 00:18:44.455 --> 00:18:46.455 we know it's got the TankHealth script on it 00:18:46.455 --> 00:18:48.062 and we'll carry on from there. 00:18:48.649 --> 00:18:51.450 Just to reiterate what Will was saying there, 00:18:51.450 --> 00:18:53.450 you don't need a game object to find 00:18:53.450 --> 00:18:55.450 a component on it, you can use 00:18:55.450 --> 00:18:58.927 a component reference to find another component. 00:18:58.927 --> 00:19:00.927 So that's what's actually happening there. 00:19:00.927 --> 00:19:02.927 You've got the rigidbody component, we want to find another 00:19:02.927 --> 00:19:04.927 component on the same game object, 00:19:04.927 --> 00:19:06.927 that's what we're doing. 00:19:07.454 --> 00:19:09.454 Okay so we know that we've got 00:19:09.454 --> 00:19:11.454 targetHealth because we've not come out of the loop yet 00:19:11.454 --> 00:19:13.454 so let's create the damage, 00:19:13.454 --> 00:19:17.880 so float damage is equal to 00:19:17.880 --> 00:19:19.880 this CalculateDamage function which you can see at 00:19:19.880 --> 00:19:21.574 the bottom of the screen there, 00:19:21.574 --> 00:19:22.841 which we will write in a minute, 00:19:22.841 --> 00:19:24.841 but what we need to do is give it the 00:19:24.841 --> 00:19:26.841 position of the target, 00:19:26.841 --> 00:19:29.536 because we need to know how far away is that target 00:19:29.536 --> 00:19:31.536 from the shell when it explodes 00:19:31.536 --> 00:19:33.536 so we know how much damage it needs to take. 00:19:34.576 --> 00:19:36.576 So you can actually get the 00:19:36.576 --> 00:19:39.475 position of something from the rigidbody as well. 00:19:39.475 --> 00:19:41.782 If you remember back from the TankMovement script 00:19:41.782 --> 00:19:44.857 we were doing things relative to the rigidbody's position. 00:19:44.857 --> 00:19:46.857 That's the position that we're going to use here. 00:19:48.175 --> 00:19:50.175 So we've got the damage 00:19:50.175 --> 00:19:52.175 now we need to apply the damage. 00:19:52.175 --> 00:19:54.342 So we've got the TargetHealth script, 00:19:55.409 --> 00:19:57.409 and we need to call the takeDamage function 00:19:57.409 --> 00:19:59.409 that we made earlier. 00:20:01.406 --> 00:20:04.281 And we're going to parse in the amount of damage. 00:20:08.123 --> 00:20:10.527 So that's all that we need to do for that for loop, 00:20:10.527 --> 00:20:12.527 that's all that we need to do for each of the 00:20:12.527 --> 00:20:14.527 targets that we've got, but we still need to 00:20:14.527 --> 00:20:16.527 make the shell explode 00:20:16.527 --> 00:20:18.876 and play the correct audio and stuff. 00:20:19.581 --> 00:20:21.581 Yeah, another thing to mention about this is 00:20:21.581 --> 00:20:22.891 we're creating a float called Damage, 00:20:22.891 --> 00:20:24.891 we've got this CalculateDamage, currently if we play this 00:20:24.891 --> 00:20:26.891 nothing would happen because we don't 00:20:26.891 --> 00:20:28.891 have anything going on in this CalculateDamage 00:20:28.891 --> 00:20:30.361 function just yet. 00:20:30.361 --> 00:20:32.998 But all we're doing is just setting it up so when we do 00:20:32.998 --> 00:20:35.777 we're going to parse that straight to the TakeDamage function. 00:20:35.777 --> 00:20:38.507 If you remember when we looked at that TankHealth earlier 00:20:39.228 --> 00:20:41.228 our TakeDamage function there is public, 00:20:41.228 --> 00:20:43.228 that's how we're able to just reference it by saying 00:20:43.228 --> 00:20:45.228 .TakeDamage. 00:20:46.920 --> 00:20:50.077 Public being the accessibility level of that thing. 00:20:51.298 --> 00:20:53.298 Okay, we've gone through all the colliders 00:20:53.298 --> 00:20:55.298 that we need to do, so now we need to 00:20:55.298 --> 00:20:58.992 sort out the particles and the audio. 00:20:58.992 --> 00:21:03.236 So when we detonate the shell, when it explodes 00:21:03.236 --> 00:21:04.737 we want to remove it from the scene, 00:21:04.737 --> 00:21:06.737 we don't want it to hang around. 00:21:06.737 --> 00:21:08.737 However if we remove it then the particles 00:21:08.737 --> 00:21:10.737 are also going to be removed because they're children 00:21:11.361 --> 00:21:12.638 and the audio source is also going to be removed 00:21:12.638 --> 00:21:13.986 because that's a child. 00:21:14.721 --> 00:21:16.721 So we want to make sure that those 00:21:16.721 --> 00:21:19.787 still play even after we've destroyed the shell. 00:21:20.550 --> 00:21:22.550 So what we're going to do to do that 00:21:22.550 --> 00:21:23.876 is to unparent them. 00:21:24.675 --> 00:21:27.651 So it will come in as a complete game object 00:21:27.651 --> 00:21:30.672 it'll explode and they will decouple themselves 00:21:30.672 --> 00:21:32.672 so the shell explosion will no longer 00:21:32.672 --> 00:21:34.672 be part of the shell. 00:21:35.185 --> 00:21:37.185 And we're going to do that by setting the 00:21:37.185 --> 00:21:39.185 transform's parent to null. 00:21:39.185 --> 00:21:41.185 So null just means nothing. 00:21:41.546 --> 00:21:43.546 That means it would just be loose in the list 00:21:43.546 --> 00:21:44.553 of game objects in the hierarchy. 00:21:44.553 --> 00:21:46.553 So we have detached it now, 00:21:46.553 --> 00:21:49.178 so next we need to play the 00:21:49.178 --> 00:21:55.855 particle effect, so m_ExplosionParticles.Play (); 00:21:57.757 --> 00:21:59.757 And likewise we want to play the audio 00:21:59.757 --> 00:22:01.757 source so very similarly 00:22:02.659 --> 00:22:05.066 m_ExplosionAudio.Play (); 00:22:06.911 --> 00:22:08.911 So then we've just got a couple of more things 00:22:08.911 --> 00:22:10.702 we've got to do in this function. 00:22:10.702 --> 00:22:12.702 We need to destroy the particles and we 00:22:12.702 --> 00:22:14.702 need to destroy the shell. 00:22:14.702 --> 00:22:16.702 So to destroy the particles we're going to use 00:22:16.702 --> 00:22:18.702 the same trick as we did in the Start function, 00:22:18.702 --> 00:22:20.702 we're going to give it a delay. 00:22:21.229 --> 00:22:27.289 So we're going to say Destroy(m_ExplosionParticles.gameObject 00:22:27.289 --> 00:22:29.289 We don't want to destroy the particles because 00:22:29.289 --> 00:22:30.596 that will destroy the component. 00:22:30.596 --> 00:22:33.562 We want to destroy the game object that those particles are on. 00:22:34.741 --> 00:22:36.741 And we're going to do that after 00:22:36.741 --> 00:22:38.741 the duration of the particle system. 00:22:39.434 --> 00:22:41.434 So you remember before we said that it's going to be 00:22:41.434 --> 00:22:43.434 2 seconds, which was a number that decided on. 00:22:44.114 --> 00:22:46.114 Well particle systems, one of their properties 00:22:46.114 --> 00:22:47.766 is how long they last for. 00:22:47.766 --> 00:22:49.766 So we're just going to say destroy it 00:22:50.390 --> 00:22:52.390 after how long it lasts for. 00:22:52.390 --> 00:22:54.390 Yeah, so not to be confused with the lifetime of 00:22:54.390 --> 00:22:57.618 each particle, but the duration is the entire system. 00:22:57.618 --> 00:23:00.501 So this, as you can see, autocomplete is showing us here 00:23:00.783 --> 00:23:02.564 Optional amount of time before destroying the object. 00:23:02.564 --> 00:23:04.674 We can simply just refer to 00:23:04.674 --> 00:23:06.674 ExplosionParticles.duration 00:23:07.603 --> 00:23:09.603 and that will simply wait for that to finish and then destroy it. 00:23:09.603 --> 00:23:11.254 based on that. 00:23:11.254 --> 00:23:13.254 Which is great, because you can then go and 00:23:13.254 --> 00:23:15.254 adjust the duration and this code will still take 00:23:15.254 --> 00:23:16.822 care of it for you. 00:23:17.448 --> 00:23:19.448 And as James was saying, the last thing we need to do 00:23:19.448 --> 00:23:21.448 once we'd destroyed the explosion, or once 00:23:21.448 --> 00:23:23.448 we've triggered a destroy to occur once 00:23:23.448 --> 00:23:25.197 it's finished playing the explosion 00:23:25.197 --> 00:23:27.197 is actually to remove the shell as well. 00:23:27.724 --> 00:23:29.724 So to do that all we do is 00:23:29.724 --> 00:23:32.238 Destroy (gameObject) and that will destroy the 00:23:32.238 --> 00:23:34.238 game object that this is on. 00:23:34.238 --> 00:23:36.238 As ever with scripting everything is just 00:23:36.238 --> 00:23:38.238 happening straight away, it's not like we're 00:23:38.238 --> 00:23:40.238 waiting for one thing to happen and then something 00:23:40.238 --> 00:23:42.983 else to happens, so theoretically you might 00:23:42.983 --> 00:23:45.611 consider that you would get rid of the shell and then 00:23:45.611 --> 00:23:48.249 the explosion but it really doesn't matter in this instance. 00:23:49.455 --> 00:23:52.280 Okay, so, the last thing that we need 00:23:52.280 --> 00:23:54.280 to do in this script is complete that 00:23:54.280 --> 00:23:56.280 CalculateDamage function. 00:23:57.583 --> 00:23:59.180 So in this CalculateDamage function 00:23:59.180 --> 00:24:01.973 currently we've just got return 0f and that's 00:24:01.973 --> 00:24:03.973 purely just so it doesn't spew errors 00:24:03.973 --> 00:24:05.973 while we're working on the rest of the stuff. 00:24:06.514 --> 00:24:09.276 So we're going to get rid of that to start off with, we don't need it. 00:24:10.344 --> 00:24:12.344 And the next thing we're going to do is 00:24:12.344 --> 00:24:15.554 create a vector from the target to the shell. 00:24:15.939 --> 00:24:17.842 So that is going to be 00:24:17.842 --> 00:24:19.842 Vector3 explosionToTarget. 00:24:27.055 --> 00:24:29.055 and that is equal to 00:24:29.055 --> 00:24:32.689 the target's position, minus the shell's position, 00:24:32.689 --> 00:24:34.689 which is transform.position. 00:24:37.810 --> 00:24:39.407 So if you imagine a big circle on the ground, 00:24:39.407 --> 00:24:42.560 that's the explosion, at the centre of it is the shell, 00:24:42.560 --> 00:24:45.047 and somewhere in that circle is the target. 00:24:45.699 --> 00:24:47.699 We're finding a vector between the two. 00:24:49.960 --> 00:24:51.960 The next thing we need to do is find out 00:24:51.960 --> 00:24:53.639 how long that vector is, 00:24:53.639 --> 00:24:55.206 the magnitude of that vector. 00:24:55.206 --> 00:24:59.765 So what we're going to do is say float explosionDistance 00:25:00.645 --> 00:25:05.663 is equal to explosionToTarget.magnitude. 00:25:11.918 --> 00:25:13.918 Okay, so now we've found that distance, 00:25:13.918 --> 00:25:17.880 which would be somewhere between 0 and the radius. 00:25:18.732 --> 00:25:22.011 We want to know what the relative distance is. 00:25:22.011 --> 00:25:25.261 Because we want a number from 0 to 1 really, 00:25:25.939 --> 00:25:29.703 rather than a number between 0 and some radius that we found. 00:25:29.703 --> 00:25:31.703 So the relative distance 00:25:31.703 --> 00:25:33.703 we know the maximum it's going to be is 00:25:33.703 --> 00:25:36.693 the radius and the minimum is going to be 0. 00:25:37.423 --> 00:25:39.676 So what we need to do is calculate a number that 00:25:39.676 --> 00:25:42.383 starts off at 0 and ends up at 1. 00:25:42.383 --> 00:25:45.980 We do that by dividing by the explosion radius. 00:25:45.980 --> 00:25:50.368 So relative distance is 00:25:50.368 --> 00:25:54.749 (m_ExplosionRadius - explosionDistance) 00:25:55.623 --> 00:25:57.623 then close the parenthesis 00:25:57.623 --> 00:26:00.355 and divide all of that by the ExplosionRadius. 00:26:02.463 --> 00:26:04.463 So when you think about that 00:26:05.087 --> 00:26:08.245 if the explosion distance is very long, 00:26:08.245 --> 00:26:10.578 IE it's close to the explosion radius 00:26:12.104 --> 00:26:15.428 then the explosion radius minus something that's 00:26:15.428 --> 00:26:17.428 very close to the explosion radius is going to be 00:26:17.428 --> 00:26:20.750 very very small, divided by the explosion radius is going 00:26:20.750 --> 00:26:22.750 to be very very small so it'll be near zero. 00:26:24.484 --> 00:26:27.352 However, if the explosion distance is very small 00:26:27.352 --> 00:26:29.352 then the explosion radius minus 00:26:29.352 --> 00:26:31.821 something very small, let's say zero 00:26:31.821 --> 00:26:33.821 divided by the explosion radius is going to be 1. 00:26:35.000 --> 00:26:37.000 So we're going to end up with a number which 00:26:37.000 --> 00:26:39.000 starts off at 1 near the centre and 00:26:39.000 --> 00:26:41.000 trails down to 0 at the edge. 00:26:42.456 --> 00:26:44.120 And that's how we're going to calculate how 00:26:44.120 --> 00:26:45.539 much damage is done. 00:26:45.539 --> 00:26:47.539 So we create a float called damage. 00:26:47.539 --> 00:26:50.105 And that's equal to the relative distance 00:26:50.618 --> 00:26:53.046 multiplied by the MaxDamage 00:26:53.046 --> 00:26:55.046 m_MaxDamage. 00:26:59.747 --> 00:27:02.315 So when the relative distance is 1 00:27:02.315 --> 00:27:04.315 and is very close to the centre, 00:27:04.315 --> 00:27:06.315 then you're going to deal the maximum damage. 00:27:06.315 --> 00:27:08.315 When it's close to the edge, it's close to 0 you're going 00:27:08.315 --> 00:27:09.824 to do very little damage. 00:27:10.579 --> 00:27:14.120 Okay, so as illustrated in the slide 00:27:14.120 --> 00:27:16.120 we have our overlap sphere, 00:27:16.120 --> 00:27:18.120 we have our tank right at the very edge of it, 00:27:18.120 --> 00:27:20.120 very minimal or maybe 0 damage, 00:27:20.120 --> 00:27:23.514 and then something right in the middle of that impact 00:27:23.514 --> 00:27:25.514 such damage, many ow. 00:27:25.805 --> 00:27:28.002 We're not finished yet with this function. 00:27:28.734 --> 00:27:30.734 What we need to do next is make sure 00:27:30.734 --> 00:27:33.564 that the damage is not negative. 00:27:33.564 --> 00:27:35.564 So we're going to do that by saying damage is 00:27:35.564 --> 00:27:44.799 equal to Mathf.max of 0f and Damage. 00:27:45.939 --> 00:27:47.939 So what that's doing is saying if it's negative 00:27:47.939 --> 00:27:50.968 then set it to 0, otherwise keep it as it is. 00:27:51.731 --> 00:27:53.731 And then finally we can return that damage. 00:27:55.617 --> 00:27:57.285 Why is it that we're 00:27:57.285 --> 00:27:59.285 making sure it's not negative? 00:27:59.285 --> 00:28:01.285 Well there is literally an edge 00:28:01.285 --> 00:28:03.285 case in this scenario. 00:28:08.167 --> 00:28:11.353 In the case that the tank is 00:28:11.353 --> 00:28:13.353 right on the very edge it's position 00:28:13.353 --> 00:28:15.353 is actually outside of 00:28:15.353 --> 00:28:18.645 the overlap sphere but it's collider is within it. 00:28:18.645 --> 00:28:21.364 So it's going to get picked up by that collider's array 00:28:21.960 --> 00:28:24.922 and then it's going to be calculated in with everything else 00:28:24.922 --> 00:28:26.922 but we've only caught the edge of it, so, 00:28:27.740 --> 00:28:29.285 we're actually going to calculate a relative distance 00:28:29.285 --> 00:28:33.180 that's negative, because you're starting off at 1 in the centre 00:28:33.180 --> 00:28:35.180 going down to 0 at the outside 00:28:35.180 --> 00:28:37.180 and then it'll be negative beyond that. 00:28:37.554 --> 00:28:39.069 So we don't want negative damage, 00:28:39.069 --> 00:28:40.563 we don't want to shoot things and make them more healthy, 00:28:40.563 --> 00:28:42.563 it's just not the way. 00:28:43.520 --> 00:28:45.520 So that's why we're doing that. 00:28:45.520 --> 00:28:47.520 Shall we put the script back on and 00:28:47.520 --> 00:28:49.520 finish it off? 00:28:52.100 --> 00:28:54.100 Okay, so if you've saved your script switch 00:28:54.100 --> 00:28:57.269 back to the editor, hopefully there won't be any errors. 00:28:59.266 --> 00:29:01.266 And then we need to of course populate 00:29:01.266 --> 00:29:03.631 our script with all those public variables. 00:29:04.084 --> 00:29:06.084 So interestingly something you won't have seen before, 00:29:08.095 --> 00:29:10.095 is the first public variable, it's this 00:29:10.095 --> 00:29:12.095 Tank Mask, so remember it's of type 00:29:12.095 --> 00:29:14.457 layer mask and we called it Tank Mask. 00:29:14.457 --> 00:29:16.457 And what that basically gives you is a repeat of 00:29:16.457 --> 00:29:18.457 all of Unity's layer system. 00:29:19.234 --> 00:29:21.234 So you'll notice it's the same list that you get in the 00:29:21.234 --> 00:29:23.234 Layer drop down at the top. 00:29:23.234 --> 00:29:25.234 This Tank Mask 00:29:25.234 --> 00:29:27.566 is where we're going to select Players. 00:29:28.621 --> 00:29:30.955 It's the only thing we want it to actually effect. 00:29:30.955 --> 00:29:32.955 so select the Players layer from the 00:29:32.955 --> 00:29:35.867 Tank Mask variable or drop down. 00:29:37.961 --> 00:29:41.216 Then Explosion Particles is the child object 00:29:41.216 --> 00:29:43.216 that's underneath the shell. 00:29:44.270 --> 00:29:47.237 So you can drag a shell explosion on to 00:29:47.237 --> 00:29:49.237 Explosion Particles, you'll notice in the 00:29:49.237 --> 00:29:51.794 parenthesis it says Particle Systems. 00:29:52.182 --> 00:29:54.294 And for Explosion Audio it's the same 00:29:54.294 --> 00:29:57.468 object because that also has an audio source. 00:29:57.468 --> 00:30:00.605 But this time when you drag it you'll notice it picks that component. 00:30:01.008 --> 00:30:03.008 So Unity is smart that way, it knows which component 00:30:03.008 --> 00:30:05.008 you're referring to if you drag an object that's got that 00:30:05.008 --> 00:30:08.061 component it'll say 'great, okay, that must be what you mean'. 00:30:08.061 --> 00:30:10.061 And assign that particular component 00:30:10.061 --> 00:30:12.061 from the game object that you drag in. 00:30:12.657 --> 00:30:14.657 Then the other stuff that we've setup there, 00:30:15.045 --> 00:30:17.045 you know, it's setup based on 00:30:17.045 --> 00:30:19.045 something that we wanted to make, if we get to the 00:30:19.045 --> 00:30:21.045 end of the day and you want to have a huge 00:30:21.045 --> 00:30:23.045 amount of damage, you want to have one shot kills 00:30:23.045 --> 00:30:25.045 and you just up the maximum damage 00:30:25.433 --> 00:30:27.433 or add the explosion force and the tank 00:30:27.433 --> 00:30:29.433 just gets flung all the way off the screen. 00:30:29.433 --> 00:30:31.433 You can do that stuff. 00:30:31.433 --> 00:30:33.433 Obviously up the explosion radius as well. 00:30:34.168 --> 00:30:35.522 You can play around with that. 00:30:35.522 --> 00:30:37.522 For now let's stick with these values 00:30:37.522 --> 00:30:39.522 and see how we like that. 00:30:39.522 --> 00:30:41.522 And then the last thing we're going to do is to save the 00:30:41.522 --> 00:30:43.522 shell as a prefab, as I said before we don't want 00:30:43.522 --> 00:30:46.305 it to be in the game when it begins. 00:30:46.305 --> 00:30:48.305 So I'm going to select my Prefabs folder. 00:30:48.846 --> 00:30:50.846 I'm going to drag my shell and drop it in to 00:30:50.846 --> 00:30:52.846 the empty space in that folder. 00:30:54.954 --> 00:30:56.954 So really we should check if this works, so I'm just going 00:30:56.954 --> 00:30:58.954 to briefly save my scene and demonstrate 00:30:58.954 --> 00:31:00.954 what you should be seeing. 00:31:00.954 --> 00:31:02.954 So if I press Play right now 00:31:04.410 --> 00:31:06.410 my shell should fall down 00:31:06.410 --> 00:31:08.410 and because it's pretty near the tank 00:31:08.410 --> 00:31:10.410 take off quite a lot of damage. 00:31:10.715 --> 00:31:13.324 Take off a lot of health, sorry, apply a lot of damage. 00:31:14.435 --> 00:31:16.281 We've applied the shell explosion, 00:31:16.281 --> 00:31:19.149 in order to populate the particle effect 00:31:19.149 --> 00:31:22.251 and the audio source on to those two different variables. 00:31:22.251 --> 00:31:24.251 And then we've made it in to a prefab 00:31:24.251 --> 00:31:26.251 so now, as mentioned before, 00:31:26.251 --> 00:31:28.251 we don't need that shell in the game. 00:31:28.736 --> 00:31:30.736 Delete it out of your scene. 00:31:30.736 --> 00:31:33.633 Delete on PC, command-backspace on Mac 00:31:33.633 --> 00:31:35.898 and then save your scene. 00:31:35.898 --> 00:31:37.898 One more time, select the shell. 00:31:38.550 --> 00:31:40.550 delete it and then save your scene. 00:31:40.550 --> 00:31:42.759 Okay, so that's the end of phase 5. 00:31:44.077 --> 00:31:46.077 Then the next phase we're going to deal with 00:31:46.077 --> 00:31:48.691 actually shooting these projectiles. 00:31:48.691 --> 00:31:50.981 A little bit of physics and a bit more UI.