1 00:00:01,761 --> 00:00:04,074 In this phase we're going to create 2 00:00:04,074 --> 00:00:06,864 this shell prefab, it looks like this 3 00:00:06,864 --> 00:00:08,864 and has a number of different components, 4 00:00:08,864 --> 00:00:11,928 it's got a collider, a rigidbody, it has a light component, 5 00:00:12,802 --> 00:00:15,764 and the idea with this is that obviously we'll 6 00:00:15,764 --> 00:00:18,883 fire it from the tank, but to demonstrate what it does, 7 00:00:18,883 --> 00:00:20,883 it has an overlapped sphere 8 00:00:20,883 --> 00:00:23,601 this is a kind of sphere of damage that it can 9 00:00:24,100 --> 00:00:26,100 give to any of the tanks that i lands near. 10 00:00:26,474 --> 00:00:28,474 So in this video what I'm doing is slowly moving it 11 00:00:28,474 --> 00:00:30,474 closer to the tank and what you should notice is that 12 00:00:30,474 --> 00:00:33,031 that health is getting reduced by more 13 00:00:33,031 --> 00:00:35,031 the closer that that bullet happens to be. 14 00:00:36,016 --> 00:00:38,016 Or at least it will do in a moment. 15 00:00:38,016 --> 00:00:40,016 Off we go, moving it closer. 16 00:00:40,016 --> 00:00:42,016 The closer it is the more damage that will be applied. 17 00:00:42,016 --> 00:00:44,016 Obviously if you hit a tank dead-on then 18 00:00:44,016 --> 00:00:46,016 that will be a huge amount of damage. 19 00:00:48,139 --> 00:00:50,139 That's the shell that we're going to make and then in the next 20 00:00:50,139 --> 00:00:52,139 phase after that we'll handle the actual shooting, 21 00:00:52,139 --> 00:00:54,139 but first off we're going to handle the damage, 22 00:00:54,139 --> 00:00:56,139 or the thing that's going to parse damage to 23 00:00:56,139 --> 00:00:58,139 the TankHealth script that we did before. 24 00:00:58,749 --> 00:01:00,749 What I would like everyone to do is look in 25 00:01:00,749 --> 00:01:02,749 the Models folder, in this folder 26 00:01:02,749 --> 00:01:05,426 you will find a model called Shell. 27 00:01:06,647 --> 00:01:09,663 If you select it it will look like this in the inspector. 28 00:01:09,663 --> 00:01:11,663 A little note, if you want to inspect something, 29 00:01:11,663 --> 00:01:13,663 drag around it, you can just drag in that preview window 30 00:01:13,663 --> 00:01:15,663 at the bottom of the inspector. 31 00:01:15,663 --> 00:01:18,400 So what we'll do is drag that in to the hierarchy. 32 00:01:18,969 --> 00:01:20,969 If we drag and drop that model from 33 00:01:20,969 --> 00:01:22,620 the project to the hierarchy. 34 00:01:23,385 --> 00:01:25,385 Move your cursor over the scene view, 35 00:01:25,385 --> 00:01:27,385 press F to frame selected 36 00:01:27,385 --> 00:01:29,385 and you'll notice it's at the origin, roughly, 37 00:01:29,385 --> 00:01:31,385 so it's near to 0. 38 00:01:31,385 --> 00:01:33,385 So all I'm going to do is just to drag mine 39 00:01:33,385 --> 00:01:35,385 up slightly so it's somewhere I can 40 00:01:35,385 --> 00:01:37,848 see it and work on it, something like that is fine. 41 00:01:39,313 --> 00:01:41,313 Remember because we're creating a prefab that 42 00:01:41,313 --> 00:01:43,313 we're going to fire with the tank this isn't something that's 43 00:01:43,313 --> 00:01:45,828 going to be in the scene when the game starts 44 00:01:45,828 --> 00:01:47,828 just like the tank, so we don't need to worry about 45 00:01:47,828 --> 00:01:49,828 where it's positioned for now. 46 00:01:52,019 --> 00:01:54,019 So because we are making 47 00:01:54,019 --> 00:01:56,019 this something that's going to interact with other objects 48 00:01:56,019 --> 00:01:59,450 it needs to have a collider. 49 00:01:59,450 --> 00:02:01,450 We'll use primitive colliders because they're 50 00:02:01,450 --> 00:02:04,468 nice and performant, and given the shape of this 51 00:02:04,468 --> 00:02:06,468 we'll use a capsule collider. 52 00:02:06,468 --> 00:02:08,468 So with that shell still selected 53 00:02:08,468 --> 00:02:11,279 in the inspector we'll hit the Add Component button 54 00:02:11,279 --> 00:02:13,279 and I'm just going to type Caps 55 00:02:13,279 --> 00:02:15,279 for Capsule and then 56 00:02:15,279 --> 00:02:17,279 click on it to confirm, or hit Return. 57 00:02:18,472 --> 00:02:19,998 With that capsule collider, 58 00:02:20,611 --> 00:02:22,611 because when you are firing things 59 00:02:22,611 --> 00:02:25,729 like a projectile as we are. 60 00:02:26,589 --> 00:02:28,589 We don't want things to be bouncing off one another 61 00:02:28,589 --> 00:02:30,589 and we don't expect this tank shell to 62 00:02:30,589 --> 00:02:32,589 ever hit something and push it back, 63 00:02:32,589 --> 00:02:34,589 we're expecting it to hit something, 64 00:02:34,589 --> 00:02:36,589 create a call back in the script and then create 65 00:02:36,589 --> 00:02:38,589 an explosion force. 66 00:02:38,589 --> 00:02:40,589 So we don't want any physical interactions. 67 00:02:41,061 --> 00:02:44,134 And when we don't want those we use what's called triggers. 68 00:02:44,134 --> 00:02:46,134 If you hear someone refer to a trigger 69 00:02:46,134 --> 00:02:48,134 or a trigger zone, 70 00:02:48,134 --> 00:02:50,134 or a trigger area they're usually referring to 71 00:02:50,134 --> 00:02:52,134 a collider that's been checked 72 00:02:52,134 --> 00:02:54,134 as a trigger. 73 00:02:54,134 --> 00:02:56,865 So if you check the Is Trigger checkbox on that, 74 00:02:57,406 --> 00:03:00,193 what that basically means is that it doesn't have 75 00:03:00,193 --> 00:03:02,193 it doesn't have a solid surface to it 76 00:03:02,193 --> 00:03:04,193 it can intersect, but if can send a 77 00:03:04,193 --> 00:03:05,476 call back to the script saying 78 00:03:05,476 --> 00:03:07,476 'I've now entered another trigger' or 79 00:03:07,476 --> 00:03:09,476 'I've entered another collider'. 80 00:03:09,476 --> 00:03:11,476 So often trigger colliders are used for things like 81 00:03:11,476 --> 00:03:13,476 doors in games, like when you put a big 82 00:03:13,476 --> 00:03:15,476 cubic collider in front of 83 00:03:15,476 --> 00:03:17,476 a doorway and then when the character enters that way 84 00:03:17,476 --> 00:03:19,476 you automatically open the door. 85 00:03:19,476 --> 00:03:22,196 It's a pretty classic use of that kind of collider. 86 00:03:23,168 --> 00:03:25,168 Once you've added that component, the next thing 87 00:03:25,168 --> 00:03:27,168 we want to do is actually set it up. 88 00:03:29,068 --> 00:03:31,068 So we need to change the centre, 89 00:03:31,068 --> 00:03:34,944 so the centre value for that is (0, 0, 0.2). 90 00:03:36,567 --> 00:03:39,134 And the radius is 0.15. 91 00:03:41,714 --> 00:03:43,714 And the height is 0.55. 92 00:03:46,446 --> 00:03:48,726 Crucially you'll notice that it's not 93 00:03:48,726 --> 00:03:52,713 looking in the right direction, so we set the direction 94 00:03:52,713 --> 00:03:55,737 to Z axis, and then it should 95 00:03:55,737 --> 00:03:58,716 pretty much match our shell perfectly. 96 00:03:58,716 --> 00:04:01,534 Or to an amount of precision that would never cause issues. 97 00:04:02,227 --> 00:04:04,227 It should look like that. 98 00:04:05,142 --> 00:04:07,647 And then finally because this is a physics object 99 00:04:07,647 --> 00:04:09,647 we need it to be under control of 100 00:04:09,647 --> 00:04:11,968 the physics engine, and so what are we going to add? 101 00:04:12,576 --> 00:04:14,576 A rigidbody, there we go, nice and loud. 102 00:04:16,504 --> 00:04:18,504 So add our rigidbody component. 103 00:04:20,279 --> 00:04:21,232 And it's good to go. 104 00:04:21,232 --> 00:04:23,232 So these shells are going to fire 105 00:04:23,232 --> 00:04:25,232 a projectile, they're not going to just keep going straight forward, 106 00:04:25,232 --> 00:04:27,232 they're going to fall with gravity over time. 107 00:04:27,925 --> 00:04:29,925 So just a quick recap of those two points. 108 00:04:30,840 --> 00:04:34,770 Okay, so we've found the model named Shell in the Models folder. 109 00:04:35,741 --> 00:04:37,741 We dragged it in to the hierarchy. 110 00:04:38,407 --> 00:04:41,236 We've added a capsule collider component to the shell 111 00:04:41,849 --> 00:04:43,849 and we've made sure it's a trigger so that that 112 00:04:43,849 --> 00:04:45,849 trigger won't bounce off of anything 113 00:04:45,849 --> 00:04:48,479 it's just going to pass straight through stuff 114 00:04:48,479 --> 00:04:50,479 and then immediately be destroyed by the script 115 00:04:50,479 --> 00:04:52,187 that we're going to write. 116 00:04:52,187 --> 00:04:54,861 We've checked Is Trigger to make sure that is the case. 117 00:04:57,585 --> 00:05:00,103 Then we set the direction to Z axis 118 00:05:00,103 --> 00:05:02,103 and we changed the centre, so the centre is 119 00:05:02,103 --> 00:05:04,103 basically where that originates from so you can 120 00:05:04,103 --> 00:05:06,842 kind of offset your colliders if you need to, art-wise. 121 00:05:07,434 --> 00:05:11,698 And the radius we've set to 0.15, the radius of the capsule. 122 00:05:11,698 --> 00:05:13,698 And the height to 0.55 123 00:05:13,698 --> 00:05:15,698 And we added a rigidbody to make sure that it's 124 00:05:15,698 --> 00:05:18,390 under the control of the physics engine. 125 00:05:21,719 --> 00:05:23,719 So we've got the shell 126 00:05:23,719 --> 00:05:25,719 and currently the shell can fall down 127 00:05:25,719 --> 00:05:27,719 and nothing is going to really happen. 128 00:05:27,719 --> 00:05:29,719 It's just going to go straight through the ground 129 00:05:29,719 --> 00:05:31,719 because it's not going to stop, it hasn't got a 130 00:05:31,719 --> 00:05:33,192 physical presence, like I said. 131 00:05:33,192 --> 00:05:35,611 So we don't want that, we want it to actually 132 00:05:35,611 --> 00:05:38,233 explode and do stuff, so we've created 133 00:05:38,233 --> 00:05:40,233 a shell explosion for you. 134 00:05:40,885 --> 00:05:42,935 So if you look in the Prefabs folder 135 00:05:43,477 --> 00:05:46,121 you will find a prefab called ShellExplosion. 136 00:05:47,702 --> 00:05:49,115 So what we're going to do with that is make it 137 00:05:49,115 --> 00:05:50,796 a child of the shell. 138 00:05:51,350 --> 00:05:53,350 So if you drag from the Prefabs folder and drop 139 00:05:53,350 --> 00:05:54,575 it on to the shell 140 00:05:55,711 --> 00:05:56,949 It should look like that. 141 00:05:58,002 --> 00:06:00,002 And when we simulate it 142 00:06:01,056 --> 00:06:02,088 it looks like this. 143 00:06:02,088 --> 00:06:05,159 A cloud of dust that kind of falls over time. 144 00:06:09,791 --> 00:06:11,791 Okay and then the only other thing that we need 145 00:06:11,791 --> 00:06:13,791 to do with that is to 146 00:06:13,791 --> 00:06:15,791 add an audio source, 147 00:06:15,791 --> 00:06:17,791 so similar to the tank exploding, 148 00:06:17,791 --> 00:06:20,373 it needs to have visual effects and an audio effect. 149 00:06:20,887 --> 00:06:22,887 So I'm going to click the Add Component button 150 00:06:22,887 --> 00:06:24,887 and I'm going to choose Audio Source. 151 00:06:25,941 --> 00:06:27,941 And then just like before we need an 152 00:06:27,941 --> 00:06:29,941 audio clip, so I'm going to use the 153 00:06:29,941 --> 00:06:31,941 circle select next to audio clip 154 00:06:32,468 --> 00:06:35,154 and choose ShellExplosion to assign it. 155 00:06:37,179 --> 00:06:39,179 Now we don't want this to play as soon as the shell 156 00:06:39,179 --> 00:06:41,900 gets instantiated, as soon as the explosion appears, 157 00:06:42,607 --> 00:06:44,607 so we're going to uncheck Play On Awake. 158 00:06:45,952 --> 00:06:48,928 A quick recap, so we've got our ShellExplosion prefab, 159 00:06:48,928 --> 00:06:52,020 we had to make it a child of the Shell game object 160 00:06:52,020 --> 00:06:54,020 so it's going to travel with it and then do it's 161 00:06:54,020 --> 00:06:56,020 thing as soon as it hits something. 162 00:06:56,020 --> 00:06:58,020 We added an audio source to that 163 00:06:58,020 --> 00:07:01,019 so that it's able to playback sound and we've used 164 00:07:01,019 --> 00:07:04,499 circle select to set the audio clip to ShellExplosion. 165 00:07:04,499 --> 00:07:06,499 We don't want it to play as soon as it enters 166 00:07:06,499 --> 00:07:08,499 the world so we only want it to play 167 00:07:08,499 --> 00:07:11,814 as soon as it impacts, so we uncheck Play On Awake. 168 00:07:15,575 --> 00:07:17,575 Okay, the final thing we're going to do with the shell 169 00:07:17,575 --> 00:07:20,728 so reselect the shell, the parent object, 170 00:07:20,728 --> 00:07:22,299 is to add a light. 171 00:07:22,299 --> 00:07:24,299 So as this travels across the 172 00:07:24,299 --> 00:07:26,711 game board we want this to 173 00:07:26,711 --> 00:07:28,419 light up the environment 174 00:07:28,419 --> 00:07:30,419 and it helps pick out the shell, because obviously 175 00:07:30,419 --> 00:07:32,419 you've got a gold shell, you've got orange sand, 176 00:07:32,419 --> 00:07:36,352 it's kind of hard to see it and the light helps pick it out 177 00:07:36,352 --> 00:07:39,107 so instead of creating a point light game object 178 00:07:39,107 --> 00:07:42,137 we can just add that same light. 179 00:07:42,137 --> 00:07:44,137 So if you've new to Unity, what you'll notice 180 00:07:44,137 --> 00:07:46,137 if you go to Game Object - Create 181 00:07:46,137 --> 00:07:48,137 is that you've got all these readymade 182 00:07:48,137 --> 00:07:50,824 prefabbed game objects with several components on them. 183 00:07:50,824 --> 00:07:52,824 So for example if you choose, 184 00:07:52,824 --> 00:07:54,824 if there was a cube, then you've got 185 00:07:54,824 --> 00:07:56,824 a bunch of different components on it. 186 00:07:56,824 --> 00:07:59,776 But you could also create a light 187 00:08:00,270 --> 00:08:02,270 which has just the light component on it. 188 00:08:02,599 --> 00:08:04,599 We don't need to do that, we're just going to put the 189 00:08:04,599 --> 00:08:06,599 component directly on to the shell, 190 00:08:06,599 --> 00:08:08,599 so I got to Add Component - Light 191 00:08:09,401 --> 00:08:11,401 So now we have to actually handle 192 00:08:11,401 --> 00:08:13,401 how that behaves, how it's going to 193 00:08:13,401 --> 00:08:15,873 impact and what it's going to do when it hits 194 00:08:15,873 --> 00:08:17,873 a tank or when it hits the environment and the 195 00:08:17,873 --> 00:08:20,736 kind of forces it's going to apply to other tanks in the game. 196 00:08:21,621 --> 00:08:23,621 So what we're going to do is to look 197 00:08:23,621 --> 00:08:25,621 in the Scripts folder and 198 00:08:25,621 --> 00:08:27,621 find the Shell folder. 199 00:08:27,621 --> 00:08:31,353 In that you will find a script called ShellExplosion. 200 00:08:32,752 --> 00:08:35,744 So we're going to drag and drop ShellExplosion on to the Shell 201 00:08:35,744 --> 00:08:37,744 game object in the hierarchy. 202 00:08:38,505 --> 00:08:40,154 And then you should see 203 00:08:40,772 --> 00:08:42,772 that along with everything else, the rigidbody, 204 00:08:42,772 --> 00:08:45,305 the capsule collider, the light that we just added, 205 00:08:45,305 --> 00:08:47,783 you've now got Shell Explosion (Script) 206 00:08:47,783 --> 00:08:50,561 and we're going to double click on that to open it up for editing. 207 00:08:54,305 --> 00:08:57,246 Okay, so in this one we don't have any private 208 00:08:57,246 --> 00:09:00,403 variables but we do have a bunch of public ones. 209 00:09:00,403 --> 00:09:02,403 So the first one is a layer mask. 210 00:09:02,403 --> 00:09:04,403 Now you might remember at the beginning of the day 211 00:09:04,403 --> 00:09:07,292 we put all the tanks on the Players layer. 212 00:09:07,292 --> 00:09:09,292 So that means that when we cause an 213 00:09:09,292 --> 00:09:11,292 explosion from the shell we can just 214 00:09:11,292 --> 00:09:13,292 check for everything that's on the Players layer 215 00:09:13,292 --> 00:09:17,436 and see which tanks we've hit rather than trying to affect everything. 216 00:09:17,436 --> 00:09:19,436 So that will be available in the inspector 217 00:09:19,436 --> 00:09:21,436 and we can choose which layer we want to affect. 218 00:09:22,341 --> 00:09:25,810 The next is the explosion particle, 219 00:09:25,810 --> 00:09:28,751 remember we just dragged on that as a child game object 220 00:09:28,751 --> 00:09:30,751 we're going to get a reference to that so we can 221 00:09:30,751 --> 00:09:32,751 play the particles when the shell explodes. 222 00:09:33,492 --> 00:09:35,492 Likewise there's an audio source reference, 223 00:09:35,492 --> 00:09:38,093 so that's for getting a reference to that audio source 224 00:09:38,093 --> 00:09:39,543 so we can play a sound. 225 00:09:39,543 --> 00:09:41,543 Then we've got a bunch of floats, 226 00:09:41,543 --> 00:09:43,543 so the first one is the most 227 00:09:43,543 --> 00:09:45,543 damage that it can possibly do, 228 00:09:45,543 --> 00:09:47,206 the maximum damage, and we're setting that to 100. 229 00:09:47,206 --> 00:09:49,879 so if you get a perfect direct hit 230 00:09:49,879 --> 00:09:51,879 then you'll kill the tank outright. 231 00:09:52,198 --> 00:09:54,198 But you can't get a perfect hit because 232 00:09:54,198 --> 00:09:56,198 it's never going to be exactly on the position 233 00:09:56,198 --> 00:09:58,198 of the tank because the collider is covering it. 234 00:09:59,474 --> 00:10:01,474 So you're going to get close but no cigar. 235 00:10:01,474 --> 00:10:03,474 The next one is the amount of force 236 00:10:03,474 --> 00:10:06,139 that the explosion is going to do right at it's centre. 237 00:10:06,916 --> 00:10:08,916 Likewise as the explosion radius 238 00:10:08,916 --> 00:10:10,916 gets further out the tank will be 239 00:10:10,916 --> 00:10:13,393 less affected by the explosion, you'll feel less force. 240 00:10:14,747 --> 00:10:16,747 We've got the max lifetime, so we don't want 241 00:10:16,747 --> 00:10:18,747 shells to exist forever 242 00:10:18,747 --> 00:10:20,747 so if they happen to miss everything 243 00:10:20,747 --> 00:10:22,747 we don't want them to stay in the scene forever so 244 00:10:22,747 --> 00:10:24,108 we give them a lifetime, 245 00:10:24,108 --> 00:10:26,108 and at the start of the scene we'll say 246 00:10:26,510 --> 00:10:28,510 if it's not dead after 2 seconds just kill it anyway. 247 00:10:29,287 --> 00:10:31,287 So it's pretty unlikely you'll ever see that, 248 00:10:31,287 --> 00:10:33,287 it would either hit the ground or 249 00:10:33,287 --> 00:10:35,287 some other object before that ever happened, 250 00:10:35,287 --> 00:10:37,287 but it's just a failsafe that we'll write in. 251 00:10:37,745 --> 00:10:39,745 So the last public variable is 252 00:10:39,745 --> 00:10:41,745 the explosion radius. 253 00:10:41,745 --> 00:10:43,745 And that is literally how far 254 00:10:43,745 --> 00:10:46,532 from the shell it's going to affect when it explodes. 255 00:10:48,391 --> 00:10:50,391 Okay, so let's look at these functions 256 00:10:50,391 --> 00:10:52,391 and as we're just mentioning 257 00:10:52,391 --> 00:10:54,391 in the Start function we're saying destroy 258 00:10:54,391 --> 00:10:56,391 the game object 259 00:10:56,391 --> 00:10:58,391 after the max lifetime. 260 00:10:58,391 --> 00:11:01,237 So that's saying when you instantiate this 261 00:11:01,654 --> 00:11:04,564 if it's still alive after 2 seconds, destroy it. 262 00:11:06,048 --> 00:11:08,835 And now we're going to have to write some scripts. 263 00:11:10,902 --> 00:11:12,473 So the first thing we want to do, 264 00:11:12,473 --> 00:11:14,473 when OnTriggerEnter is called, so you remember we 265 00:11:14,473 --> 00:11:17,798 put Is Trigger as True, that means that 266 00:11:17,798 --> 00:11:19,798 OnTriggerEnter is called when something 267 00:11:19,798 --> 00:11:21,032 bumps in to this. 268 00:11:21,032 --> 00:11:23,032 So this function will 269 00:11:23,032 --> 00:11:25,032 happen whenever it intersects with anything 270 00:11:25,032 --> 00:11:27,032 and then we need to check what it's 271 00:11:27,032 --> 00:11:29,032 intersected with and what to do as a result. 272 00:11:29,032 --> 00:11:31,032 So we have this argument 273 00:11:31,032 --> 00:11:33,409 which we've just called other which is of type Collider. 274 00:11:33,409 --> 00:11:35,409 So it's going to say 'okay, we've intersected 275 00:11:35,409 --> 00:11:38,129 with something else, it's a collider, what do we do about it?' 276 00:11:39,226 --> 00:11:41,226 In this case we don't actually care what we're bumping in to 277 00:11:41,226 --> 00:11:43,226 just that it's a collider, 278 00:11:43,226 --> 00:11:45,226 so we should explode. 279 00:11:45,226 --> 00:11:46,441 But what do we do first? 280 00:11:46,441 --> 00:11:48,441 First of all we need to gather up all of the 281 00:11:48,441 --> 00:11:50,441 colliders that we need to affect. 282 00:11:50,441 --> 00:11:52,441 So we need an array of colliders 283 00:11:52,441 --> 00:11:54,842 and you'll remember that's the square brackets after a collider. 284 00:11:55,327 --> 00:11:57,327 And we'll call that array Colliders. 285 00:11:58,076 --> 00:12:00,076 And we're going to set that to the return of 286 00:12:00,076 --> 00:12:02,076 a function called OverlapSphere. 287 00:12:02,076 --> 00:12:05,547 So that's Physics.OverlapSphere 288 00:12:08,168 --> 00:12:10,944 And what that's going to do is similar to a ray cast, 289 00:12:10,944 --> 00:12:14,008 it's going to create an imaginary sphere 290 00:12:14,812 --> 00:12:17,281 and everything that overlaps with that sphere 291 00:12:17,281 --> 00:12:20,150 or is inside the sphere is going to be collected. 292 00:12:20,871 --> 00:12:22,871 And we want that sphere to be at 293 00:12:22,871 --> 00:12:24,871 the shell's location, so transform.position 294 00:12:24,871 --> 00:12:26,871 for it's position. 295 00:12:27,412 --> 00:12:29,412 It's going to have a radius that we've already 296 00:12:29,412 --> 00:12:31,121 got as an ExplosionRadius. 297 00:12:31,620 --> 00:12:33,620 And we're going to use that TankMask 298 00:12:33,620 --> 00:12:35,620 so that it's only picking up the tanks. 299 00:12:40,183 --> 00:12:42,183 Okay, so just a quick note about layers. 300 00:12:42,183 --> 00:12:44,183 Layers we use for various different things 301 00:12:44,183 --> 00:12:46,183 in Unity but ostensibly they're 302 00:12:46,183 --> 00:12:48,183 just for grouping things together in 303 00:12:48,183 --> 00:12:50,183 some way that you want to use 304 00:12:50,183 --> 00:12:52,772 to either include of exclude them from something. 305 00:12:52,772 --> 00:12:54,772 In this case we're using it as a way of 306 00:12:54,772 --> 00:12:57,706 only including or sort of masking out 307 00:12:57,706 --> 00:12:59,706 anything but tanks, which is why we've called this 308 00:12:59,706 --> 00:13:03,925 variable TankMask, because it's essentially a layer on 309 00:13:03,925 --> 00:13:05,827 which only the tanks exist. 310 00:13:05,827 --> 00:13:07,827 But don't think of it in any visual terms, 311 00:13:07,827 --> 00:13:10,341 if anyone's in to Photoshop or anything like that 312 00:13:10,341 --> 00:13:13,168 there's no kind of layering in that sense. 313 00:13:13,168 --> 00:13:15,728 Okay, so now we've got all our colliders 314 00:13:15,728 --> 00:13:17,728 we need to go through them all and 315 00:13:17,728 --> 00:13:19,728 decide what we're going to do. 316 00:13:20,186 --> 00:13:22,186 So for that we need to 317 00:13:22,186 --> 00:13:24,186 loop through them all using a For Loop. 318 00:13:24,644 --> 00:13:26,644 So this is the same format as we saw before 319 00:13:27,587 --> 00:13:30,242 for(int i = 0 320 00:13:30,963 --> 00:13:33,243 i is less than colliders.length 321 00:13:33,243 --> 00:13:35,777 so this is, colliders is the array that we're going 322 00:13:35,777 --> 00:13:38,083 through so it's less than colliders.Length. 323 00:13:38,083 --> 00:13:41,804 And then i++ means that we add to i for every loop. 324 00:13:43,274 --> 00:13:47,069 Then open and close brackets? 325 00:13:47,069 --> 00:13:49,069 Brackets? Yes brackets. 326 00:13:49,568 --> 00:13:51,568 And then we can 327 00:13:51,568 --> 00:13:53,888 use whatever we do in that for loop 328 00:13:53,888 --> 00:13:55,888 to affect each of the colliders. 329 00:13:55,888 --> 00:13:57,888 So the first thing we want to do is check 330 00:13:57,888 --> 00:13:59,888 whether the thing that we've hit has 331 00:13:59,888 --> 00:14:01,888 a rigidbody, so what we do is we 332 00:14:01,888 --> 00:14:04,334 create a rigidbody variable 333 00:14:04,334 --> 00:14:06,334 which we'll call targetRigidbody 334 00:14:06,861 --> 00:14:11,315 and we'll set that equal to colliders[i], so that's 335 00:14:11,315 --> 00:14:13,315 the collider at the position that we're 336 00:14:13,315 --> 00:14:15,315 currently iterating through in the array, 337 00:14:16,577 --> 00:14:20,031 .GetComponent, 338 00:14:20,031 --> 00:14:22,031 so we'll use the angled brackets for rigidbody. 339 00:14:23,723 --> 00:14:25,723 So what that's going to do is 340 00:14:25,723 --> 00:14:27,723 get that collider that we've just gone through 341 00:14:27,723 --> 00:14:30,357 the array and try and find a rigidbody on it. 342 00:14:31,286 --> 00:14:33,286 Next we need to know if it's actually 343 00:14:33,286 --> 00:14:36,101 found a rigidbody or if it's just found nothing. 344 00:14:37,297 --> 00:14:40,112 So what we're going to do is say if 345 00:14:41,835 --> 00:14:43,835 not target(!Rigidbody) 346 00:14:43,835 --> 00:14:47,124 so what that's going to do is say 347 00:14:47,124 --> 00:14:49,515 if there's nothing there then what do I do? 348 00:14:50,028 --> 00:14:52,028 And if there's nothing there we want to continue 349 00:14:52,028 --> 00:14:54,028 on to the next loop, on the the next collider. 350 00:14:54,028 --> 00:14:56,028 You'll remember it's going back to the 351 00:14:56,028 --> 00:14:58,028 start of the for loop and carrying on looking 352 00:14:58,028 --> 00:15:00,677 for any collider that's got a rigidbody 353 00:15:00,677 --> 00:15:03,422 it's not continuing through a list of commands. 354 00:15:03,422 --> 00:15:05,422 So bear in mind that this is a bit of a 355 00:15:05,422 --> 00:15:07,422 safety catch kind of thing because we're making it 356 00:15:07,422 --> 00:15:09,422 everything that's on the Players layer anyway. 357 00:15:09,783 --> 00:15:11,783 So everything should have a collider 358 00:15:12,324 --> 00:15:14,324 and everything that has a collider there will also 359 00:15:14,324 --> 00:15:17,310 have a rigidbody, so we should always find something 360 00:15:17,310 --> 00:15:19,310 but it's good to check just in case because otherwise 361 00:15:19,310 --> 00:15:21,310 you're going to get errors. 362 00:15:21,934 --> 00:15:23,934 So now we know we've got a rigidbody what 363 00:15:23,934 --> 00:15:26,353 we can do is we can add a force to that rigidbody 364 00:15:26,353 --> 00:15:32,280 so targitRigidbody.AddExplosionForce ( 365 00:15:33,754 --> 00:15:35,754 and the force we want to add is the 366 00:15:35,754 --> 00:15:37,754 ExplosionForce that we've got as a public variable. 367 00:15:37,754 --> 00:15:40,679 So that's m_ExplosionForce. 368 00:15:41,428 --> 00:15:44,562 Then comma, so we're on to the next parameter. 369 00:15:44,562 --> 00:15:46,562 And the position that that explosion 370 00:15:46,562 --> 00:15:49,598 force is emanating from is transform.position. 371 00:15:50,375 --> 00:15:52,375 So that's position of the shell. 372 00:15:53,387 --> 00:15:55,387 And then the last parameter that we're going to use is 373 00:15:55,387 --> 00:15:57,387 the radius, so how far away 374 00:15:57,387 --> 00:16:00,412 that explosion force can reach. 375 00:16:05,835 --> 00:16:09,234 Okay, so we've added the force now 376 00:16:09,234 --> 00:16:11,234 so the tank is going to move but we still need 377 00:16:11,234 --> 00:16:13,234 to actually cause some damage to it. 378 00:16:13,900 --> 00:16:15,579 So we're going to do something very similar. 379 00:16:15,579 --> 00:16:17,579 We're going to create a TankHealth variable, 380 00:16:18,522 --> 00:16:20,522 which we'll call targetHealth. 381 00:16:22,672 --> 00:16:24,672 So just to qualify that, notice that when we've 382 00:16:24,672 --> 00:16:26,672 made these other variables they've been familiar 383 00:16:26,672 --> 00:16:28,672 components to things that you've 384 00:16:28,672 --> 00:16:30,672 added using the Add Component button. 385 00:16:31,185 --> 00:16:33,185 This time we're making a variable 386 00:16:33,185 --> 00:16:35,534 of type TankHealth. 387 00:16:35,534 --> 00:16:38,608 So that just means an instance of the TankHealth script 388 00:16:38,608 --> 00:16:41,610 on a tank, let's go back from the top. 389 00:16:41,610 --> 00:16:43,181 So we've gathered all the colliders we could find 390 00:16:43,181 --> 00:16:45,797 within the imaginary sphere, the overlap sphere. 391 00:16:46,366 --> 00:16:48,145 And we're looping through 392 00:16:48,630 --> 00:16:50,630 that list, that array, 393 00:16:50,630 --> 00:16:53,490 to the amount that there is, colliders found. 394 00:16:53,490 --> 00:16:55,490 And then with each one we're 395 00:16:55,490 --> 00:16:57,490 checking that there's a rigidbody and if there is 396 00:16:57,490 --> 00:16:59,267 we're adding a force, 397 00:16:59,267 --> 00:17:01,267 and then we want to actually also 398 00:17:01,267 --> 00:17:03,267 take damage on those tanks. 399 00:17:03,586 --> 00:17:05,586 So we're addressing a particular 400 00:17:05,586 --> 00:17:07,586 instance of that TankHealth script so we can 401 00:17:07,586 --> 00:17:09,586 talk to it and reduce health with it. 402 00:17:10,793 --> 00:17:13,913 Okay, so we've got TankHealth targetHealth 403 00:17:13,913 --> 00:17:16,358 and we're going to set that equal to the targetRigidbody. 404 00:17:17,149 --> 00:17:19,149 GetComponent TankHealth. 405 00:17:19,482 --> 00:17:21,775 So we've got the rigidbody, we know that now 406 00:17:22,231 --> 00:17:24,231 because we were able to add a force to it. 407 00:17:24,675 --> 00:17:26,675 So on the same game object as that 408 00:17:27,258 --> 00:17:28,831 get the component TankHealth. 409 00:17:28,831 --> 00:17:31,790 And again we should know that 410 00:17:31,790 --> 00:17:33,790 there's definitely going to be a TankHealth here 411 00:17:33,790 --> 00:17:35,790 but we're going to check just to make sure. 412 00:17:35,790 --> 00:17:37,790 Because if somebody wants to extend the game and they 413 00:17:37,790 --> 00:17:41,106 make something that's on the Players layer that doesn't have TankHealth 414 00:17:41,106 --> 00:17:43,106 then this will still work. 415 00:17:43,106 --> 00:17:45,801 So again instead of if(!rigidbody) we've got 416 00:17:45,801 --> 00:17:49,890 if(!targetHealth) and then continue. 417 00:17:49,890 --> 00:17:51,890 So if there's no targetHealth continue 418 00:17:51,890 --> 00:17:53,890 on to the next iteration. 419 00:17:54,292 --> 00:17:56,292 So a quick note about this line here. 420 00:17:56,292 --> 00:17:58,292 So you'll notice that we've talked about 421 00:17:58,292 --> 00:18:00,292 the fact that we need a reference to this instance 422 00:18:00,292 --> 00:18:02,292 of the TankHealth script. 423 00:18:02,292 --> 00:18:04,292 So you might wondering 'well what's that got to do with 424 00:18:04,292 --> 00:18:07,031 the rigidbody, what's health got to do with physics?' 425 00:18:07,031 --> 00:18:09,031 Nothing at all, the point is that we've found 426 00:18:09,031 --> 00:18:11,449 a particular game object 427 00:18:11,449 --> 00:18:13,449 the fact that we're calling it targetRigidbody 428 00:18:13,449 --> 00:18:15,449 is not hugely important, we'e just referring to 429 00:18:15,449 --> 00:18:18,073 the same game object that we've stored in that. 430 00:18:18,073 --> 00:18:20,073 Often when we talk about rigidbodies 431 00:18:20,073 --> 00:18:22,972 we'll say 'this is a rigidbody object'. 432 00:18:22,972 --> 00:18:26,157 All things in the hierarchy are what we call game objects. 433 00:18:26,157 --> 00:18:28,157 But often what people will say is 434 00:18:28,157 --> 00:18:29,146 'that's a rigidbody object'. 435 00:18:29,146 --> 00:18:31,146 It just means it's a game object with that 436 00:18:31,146 --> 00:18:32,827 particular component attached. 437 00:18:32,827 --> 00:18:35,591 So when we're talking about the target rigidbody 438 00:18:35,591 --> 00:18:37,591 we just mean it's the tank game object that 439 00:18:37,591 --> 00:18:40,455 we know has a rigidbody attached to it. 440 00:18:40,455 --> 00:18:42,455 So we've done that check and we can then 441 00:18:42,455 --> 00:18:44,455 say we know it's the right game object so 442 00:18:44,455 --> 00:18:46,455 we know it's got the TankHealth script on it 443 00:18:46,455 --> 00:18:48,062 and we'll carry on from there. 444 00:18:48,649 --> 00:18:51,450 Just to reiterate what Will was saying there, 445 00:18:51,450 --> 00:18:53,450 you don't need a game object to find 446 00:18:53,450 --> 00:18:55,450 a component on it, you can use 447 00:18:55,450 --> 00:18:58,927 a component reference to find another component. 448 00:18:58,927 --> 00:19:00,927 So that's what's actually happening there. 449 00:19:00,927 --> 00:19:02,927 You've got the rigidbody component, we want to find another 450 00:19:02,927 --> 00:19:04,927 component on the same game object, 451 00:19:04,927 --> 00:19:06,927 that's what we're doing. 452 00:19:07,454 --> 00:19:09,454 Okay so we know that we've got 453 00:19:09,454 --> 00:19:11,454 targetHealth because we've not come out of the loop yet 454 00:19:11,454 --> 00:19:13,454 so let's create the damage, 455 00:19:13,454 --> 00:19:17,880 so float damage is equal to 456 00:19:17,880 --> 00:19:19,880 this CalculateDamage function which you can see at 457 00:19:19,880 --> 00:19:21,574 the bottom of the screen there, 458 00:19:21,574 --> 00:19:22,841 which we will write in a minute, 459 00:19:22,841 --> 00:19:24,841 but what we need to do is give it the 460 00:19:24,841 --> 00:19:26,841 position of the target, 461 00:19:26,841 --> 00:19:29,536 because we need to know how far away is that target 462 00:19:29,536 --> 00:19:31,536 from the shell when it explodes 463 00:19:31,536 --> 00:19:33,536 so we know how much damage it needs to take. 464 00:19:34,576 --> 00:19:36,576 So you can actually get the 465 00:19:36,576 --> 00:19:39,475 position of something from the rigidbody as well. 466 00:19:39,475 --> 00:19:41,782 If you remember back from the TankMovement script 467 00:19:41,782 --> 00:19:44,857 we were doing things relative to the rigidbody's position. 468 00:19:44,857 --> 00:19:46,857 That's the position that we're going to use here. 469 00:19:48,175 --> 00:19:50,175 So we've got the damage 470 00:19:50,175 --> 00:19:52,175 now we need to apply the damage. 471 00:19:52,175 --> 00:19:54,342 So we've got the TargetHealth script, 472 00:19:55,409 --> 00:19:57,409 and we need to call the takeDamage function 473 00:19:57,409 --> 00:19:59,409 that we made earlier. 474 00:20:01,406 --> 00:20:04,281 And we're going to parse in the amount of damage. 475 00:20:08,123 --> 00:20:10,527 So that's all that we need to do for that for loop, 476 00:20:10,527 --> 00:20:12,527 that's all that we need to do for each of the 477 00:20:12,527 --> 00:20:14,527 targets that we've got, but we still need to 478 00:20:14,527 --> 00:20:16,527 make the shell explode 479 00:20:16,527 --> 00:20:18,876 and play the correct audio and stuff. 480 00:20:19,581 --> 00:20:21,581 Yeah, another thing to mention about this is 481 00:20:21,581 --> 00:20:22,891 we're creating a float called Damage, 482 00:20:22,891 --> 00:20:24,891 we've got this CalculateDamage, currently if we play this 483 00:20:24,891 --> 00:20:26,891 nothing would happen because we don't 484 00:20:26,891 --> 00:20:28,891 have anything going on in this CalculateDamage 485 00:20:28,891 --> 00:20:30,361 function just yet. 486 00:20:30,361 --> 00:20:32,998 But all we're doing is just setting it up so when we do 487 00:20:32,998 --> 00:20:35,777 we're going to parse that straight to the TakeDamage function. 488 00:20:35,777 --> 00:20:38,507 If you remember when we looked at that TankHealth earlier 489 00:20:39,228 --> 00:20:41,228 our TakeDamage function there is public, 490 00:20:41,228 --> 00:20:43,228 that's how we're able to just reference it by saying 491 00:20:43,228 --> 00:20:45,228 .TakeDamage. 492 00:20:46,920 --> 00:20:50,077 Public being the accessibility level of that thing. 493 00:20:51,298 --> 00:20:53,298 Okay, we've gone through all the colliders 494 00:20:53,298 --> 00:20:55,298 that we need to do, so now we need to 495 00:20:55,298 --> 00:20:58,992 sort out the particles and the audio. 496 00:20:58,992 --> 00:21:03,236 So when we detonate the shell, when it explodes 497 00:21:03,236 --> 00:21:04,737 we want to remove it from the scene, 498 00:21:04,737 --> 00:21:06,737 we don't want it to hang around. 499 00:21:06,737 --> 00:21:08,737 However if we remove it then the particles 500 00:21:08,737 --> 00:21:10,737 are also going to be removed because they're children 501 00:21:11,361 --> 00:21:12,638 and the audio source is also going to be removed 502 00:21:12,638 --> 00:21:13,986 because that's a child. 503 00:21:14,721 --> 00:21:16,721 So we want to make sure that those 504 00:21:16,721 --> 00:21:19,787 still play even after we've destroyed the shell. 505 00:21:20,550 --> 00:21:22,550 So what we're going to do to do that 506 00:21:22,550 --> 00:21:23,876 is to unparent them. 507 00:21:24,675 --> 00:21:27,651 So it will come in as a complete game object 508 00:21:27,651 --> 00:21:30,672 it'll explode and they will decouple themselves 509 00:21:30,672 --> 00:21:32,672 so the shell explosion will no longer 510 00:21:32,672 --> 00:21:34,672 be part of the shell. 511 00:21:35,185 --> 00:21:37,185 And we're going to do that by setting the 512 00:21:37,185 --> 00:21:39,185 transform's parent to null. 513 00:21:39,185 --> 00:21:41,185 So null just means nothing. 514 00:21:41,546 --> 00:21:43,546 That means it would just be loose in the list 515 00:21:43,546 --> 00:21:44,553 of game objects in the hierarchy. 516 00:21:44,553 --> 00:21:46,553 So we have detached it now, 517 00:21:46,553 --> 00:21:49,178 so next we need to play the 518 00:21:49,178 --> 00:21:55,855 particle effect, so m_ExplosionParticles.Play (); 519 00:21:57,757 --> 00:21:59,757 And likewise we want to play the audio 520 00:21:59,757 --> 00:22:01,757 source so very similarly 521 00:22:02,659 --> 00:22:05,066 m_ExplosionAudio.Play (); 522 00:22:06,911 --> 00:22:08,911 So then we've just got a couple of more things 523 00:22:08,911 --> 00:22:10,702 we've got to do in this function. 524 00:22:10,702 --> 00:22:12,702 We need to destroy the particles and we 525 00:22:12,702 --> 00:22:14,702 need to destroy the shell. 526 00:22:14,702 --> 00:22:16,702 So to destroy the particles we're going to use 527 00:22:16,702 --> 00:22:18,702 the same trick as we did in the Start function, 528 00:22:18,702 --> 00:22:20,702 we're going to give it a delay. 529 00:22:21,229 --> 00:22:27,289 So we're going to say Destroy(m_ExplosionParticles.gameObject 530 00:22:27,289 --> 00:22:29,289 We don't want to destroy the particles because 531 00:22:29,289 --> 00:22:30,596 that will destroy the component. 532 00:22:30,596 --> 00:22:33,562 We want to destroy the game object that those particles are on. 533 00:22:34,741 --> 00:22:36,741 And we're going to do that after 534 00:22:36,741 --> 00:22:38,741 the duration of the particle system. 535 00:22:39,434 --> 00:22:41,434 So you remember before we said that it's going to be 536 00:22:41,434 --> 00:22:43,434 2 seconds, which was a number that decided on. 537 00:22:44,114 --> 00:22:46,114 Well particle systems, one of their properties 538 00:22:46,114 --> 00:22:47,766 is how long they last for. 539 00:22:47,766 --> 00:22:49,766 So we're just going to say destroy it 540 00:22:50,390 --> 00:22:52,390 after how long it lasts for. 541 00:22:52,390 --> 00:22:54,390 Yeah, so not to be confused with the lifetime of 542 00:22:54,390 --> 00:22:57,618 each particle, but the duration is the entire system. 543 00:22:57,618 --> 00:23:00,501 So this, as you can see, autocomplete is showing us here 544 00:23:00,783 --> 00:23:02,564 Optional amount of time before destroying the object. 545 00:23:02,564 --> 00:23:04,674 We can simply just refer to 546 00:23:04,674 --> 00:23:06,674 ExplosionParticles.duration 547 00:23:07,603 --> 00:23:09,603 and that will simply wait for that to finish and then destroy it. 548 00:23:09,603 --> 00:23:11,254 based on that. 549 00:23:11,254 --> 00:23:13,254 Which is great, because you can then go and 550 00:23:13,254 --> 00:23:15,254 adjust the duration and this code will still take 551 00:23:15,254 --> 00:23:16,822 care of it for you. 552 00:23:17,448 --> 00:23:19,448 And as James was saying, the last thing we need to do 553 00:23:19,448 --> 00:23:21,448 once we'd destroyed the explosion, or once 554 00:23:21,448 --> 00:23:23,448 we've triggered a destroy to occur once 555 00:23:23,448 --> 00:23:25,197 it's finished playing the explosion 556 00:23:25,197 --> 00:23:27,197 is actually to remove the shell as well. 557 00:23:27,724 --> 00:23:29,724 So to do that all we do is 558 00:23:29,724 --> 00:23:32,238 Destroy (gameObject) and that will destroy the 559 00:23:32,238 --> 00:23:34,238 game object that this is on. 560 00:23:34,238 --> 00:23:36,238 As ever with scripting everything is just 561 00:23:36,238 --> 00:23:38,238 happening straight away, it's not like we're 562 00:23:38,238 --> 00:23:40,238 waiting for one thing to happen and then something 563 00:23:40,238 --> 00:23:42,983 else to happens, so theoretically you might 564 00:23:42,983 --> 00:23:45,611 consider that you would get rid of the shell and then 565 00:23:45,611 --> 00:23:48,249 the explosion but it really doesn't matter in this instance. 566 00:23:49,455 --> 00:23:52,280 Okay, so, the last thing that we need 567 00:23:52,280 --> 00:23:54,280 to do in this script is complete that 568 00:23:54,280 --> 00:23:56,280 CalculateDamage function. 569 00:23:57,583 --> 00:23:59,180 So in this CalculateDamage function 570 00:23:59,180 --> 00:24:01,973 currently we've just got return 0f and that's 571 00:24:01,973 --> 00:24:03,973 purely just so it doesn't spew errors 572 00:24:03,973 --> 00:24:05,973 while we're working on the rest of the stuff. 573 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. 574 00:24:10,344 --> 00:24:12,344 And the next thing we're going to do is 575 00:24:12,344 --> 00:24:15,554 create a vector from the target to the shell. 576 00:24:15,939 --> 00:24:17,842 So that is going to be 577 00:24:17,842 --> 00:24:19,842 Vector3 explosionToTarget. 578 00:24:27,055 --> 00:24:29,055 and that is equal to 579 00:24:29,055 --> 00:24:32,689 the target's position, minus the shell's position, 580 00:24:32,689 --> 00:24:34,689 which is transform.position. 581 00:24:37,810 --> 00:24:39,407 So if you imagine a big circle on the ground, 582 00:24:39,407 --> 00:24:42,560 that's the explosion, at the centre of it is the shell, 583 00:24:42,560 --> 00:24:45,047 and somewhere in that circle is the target. 584 00:24:45,699 --> 00:24:47,699 We're finding a vector between the two. 585 00:24:49,960 --> 00:24:51,960 The next thing we need to do is find out 586 00:24:51,960 --> 00:24:53,639 how long that vector is, 587 00:24:53,639 --> 00:24:55,206 the magnitude of that vector. 588 00:24:55,206 --> 00:24:59,765 So what we're going to do is say float explosionDistance 589 00:25:00,645 --> 00:25:05,663 is equal to explosionToTarget.magnitude. 590 00:25:11,918 --> 00:25:13,918 Okay, so now we've found that distance, 591 00:25:13,918 --> 00:25:17,880 which would be somewhere between 0 and the radius. 592 00:25:18,732 --> 00:25:22,011 We want to know what the relative distance is. 593 00:25:22,011 --> 00:25:25,261 Because we want a number from 0 to 1 really, 594 00:25:25,939 --> 00:25:29,703 rather than a number between 0 and some radius that we found. 595 00:25:29,703 --> 00:25:31,703 So the relative distance 596 00:25:31,703 --> 00:25:33,703 we know the maximum it's going to be is 597 00:25:33,703 --> 00:25:36,693 the radius and the minimum is going to be 0. 598 00:25:37,423 --> 00:25:39,676 So what we need to do is calculate a number that 599 00:25:39,676 --> 00:25:42,383 starts off at 0 and ends up at 1. 600 00:25:42,383 --> 00:25:45,980 We do that by dividing by the explosion radius. 601 00:25:45,980 --> 00:25:50,368 So relative distance is 602 00:25:50,368 --> 00:25:54,749 (m_ExplosionRadius - explosionDistance) 603 00:25:55,623 --> 00:25:57,623 then close the parenthesis 604 00:25:57,623 --> 00:26:00,355 and divide all of that by the ExplosionRadius. 605 00:26:02,463 --> 00:26:04,463 So when you think about that 606 00:26:05,087 --> 00:26:08,245 if the explosion distance is very long, 607 00:26:08,245 --> 00:26:10,578 IE it's close to the explosion radius 608 00:26:12,104 --> 00:26:15,428 then the explosion radius minus something that's 609 00:26:15,428 --> 00:26:17,428 very close to the explosion radius is going to be 610 00:26:17,428 --> 00:26:20,750 very very small, divided by the explosion radius is going 611 00:26:20,750 --> 00:26:22,750 to be very very small so it'll be near zero. 612 00:26:24,484 --> 00:26:27,352 However, if the explosion distance is very small 613 00:26:27,352 --> 00:26:29,352 then the explosion radius minus 614 00:26:29,352 --> 00:26:31,821 something very small, let's say zero 615 00:26:31,821 --> 00:26:33,821 divided by the explosion radius is going to be 1. 616 00:26:35,000 --> 00:26:37,000 So we're going to end up with a number which 617 00:26:37,000 --> 00:26:39,000 starts off at 1 near the centre and 618 00:26:39,000 --> 00:26:41,000 trails down to 0 at the edge. 619 00:26:42,456 --> 00:26:44,120 And that's how we're going to calculate how 620 00:26:44,120 --> 00:26:45,539 much damage is done. 621 00:26:45,539 --> 00:26:47,539 So we create a float called damage. 622 00:26:47,539 --> 00:26:50,105 And that's equal to the relative distance 623 00:26:50,618 --> 00:26:53,046 multiplied by the MaxDamage 624 00:26:53,046 --> 00:26:55,046 m_MaxDamage. 625 00:26:59,747 --> 00:27:02,315 So when the relative distance is 1 626 00:27:02,315 --> 00:27:04,315 and is very close to the centre, 627 00:27:04,315 --> 00:27:06,315 then you're going to deal the maximum damage. 628 00:27:06,315 --> 00:27:08,315 When it's close to the edge, it's close to 0 you're going 629 00:27:08,315 --> 00:27:09,824 to do very little damage. 630 00:27:10,579 --> 00:27:14,120 Okay, so as illustrated in the slide 631 00:27:14,120 --> 00:27:16,120 we have our overlap sphere, 632 00:27:16,120 --> 00:27:18,120 we have our tank right at the very edge of it, 633 00:27:18,120 --> 00:27:20,120 very minimal or maybe 0 damage, 634 00:27:20,120 --> 00:27:23,514 and then something right in the middle of that impact 635 00:27:23,514 --> 00:27:25,514 such damage, many ow. 636 00:27:25,805 --> 00:27:28,002 We're not finished yet with this function. 637 00:27:28,734 --> 00:27:30,734 What we need to do next is make sure 638 00:27:30,734 --> 00:27:33,564 that the damage is not negative. 639 00:27:33,564 --> 00:27:35,564 So we're going to do that by saying damage is 640 00:27:35,564 --> 00:27:44,799 equal to Mathf.max of 0f and Damage. 641 00:27:45,939 --> 00:27:47,939 So what that's doing is saying if it's negative 642 00:27:47,939 --> 00:27:50,968 then set it to 0, otherwise keep it as it is. 643 00:27:51,731 --> 00:27:53,731 And then finally we can return that damage. 644 00:27:55,617 --> 00:27:57,285 Why is it that we're 645 00:27:57,285 --> 00:27:59,285 making sure it's not negative? 646 00:27:59,285 --> 00:28:01,285 Well there is literally an edge 647 00:28:01,285 --> 00:28:03,285 case in this scenario. 648 00:28:08,167 --> 00:28:11,353 In the case that the tank is 649 00:28:11,353 --> 00:28:13,353 right on the very edge it's position 650 00:28:13,353 --> 00:28:15,353 is actually outside of 651 00:28:15,353 --> 00:28:18,645 the overlap sphere but it's collider is within it. 652 00:28:18,645 --> 00:28:21,364 So it's going to get picked up by that collider's array 653 00:28:21,960 --> 00:28:24,922 and then it's going to be calculated in with everything else 654 00:28:24,922 --> 00:28:26,922 but we've only caught the edge of it, so, 655 00:28:27,740 --> 00:28:29,285 we're actually going to calculate a relative distance 656 00:28:29,285 --> 00:28:33,180 that's negative, because you're starting off at 1 in the centre 657 00:28:33,180 --> 00:28:35,180 going down to 0 at the outside 658 00:28:35,180 --> 00:28:37,180 and then it'll be negative beyond that. 659 00:28:37,554 --> 00:28:39,069 So we don't want negative damage, 660 00:28:39,069 --> 00:28:40,563 we don't want to shoot things and make them more healthy, 661 00:28:40,563 --> 00:28:42,563 it's just not the way. 662 00:28:43,520 --> 00:28:45,520 So that's why we're doing that. 663 00:28:45,520 --> 00:28:47,520 Shall we put the script back on and 664 00:28:47,520 --> 00:28:49,520 finish it off? 665 00:28:52,100 --> 00:28:54,100 Okay, so if you've saved your script switch 666 00:28:54,100 --> 00:28:57,269 back to the editor, hopefully there won't be any errors. 667 00:28:59,266 --> 00:29:01,266 And then we need to of course populate 668 00:29:01,266 --> 00:29:03,631 our script with all those public variables. 669 00:29:04,084 --> 00:29:06,084 So interestingly something you won't have seen before, 670 00:29:08,095 --> 00:29:10,095 is the first public variable, it's this 671 00:29:10,095 --> 00:29:12,095 Tank Mask, so remember it's of type 672 00:29:12,095 --> 00:29:14,457 layer mask and we called it Tank Mask. 673 00:29:14,457 --> 00:29:16,457 And what that basically gives you is a repeat of 674 00:29:16,457 --> 00:29:18,457 all of Unity's layer system. 675 00:29:19,234 --> 00:29:21,234 So you'll notice it's the same list that you get in the 676 00:29:21,234 --> 00:29:23,234 Layer drop down at the top. 677 00:29:23,234 --> 00:29:25,234 This Tank Mask 678 00:29:25,234 --> 00:29:27,566 is where we're going to select Players. 679 00:29:28,621 --> 00:29:30,955 It's the only thing we want it to actually effect. 680 00:29:30,955 --> 00:29:32,955 so select the Players layer from the 681 00:29:32,955 --> 00:29:35,867 Tank Mask variable or drop down. 682 00:29:37,961 --> 00:29:41,216 Then Explosion Particles is the child object 683 00:29:41,216 --> 00:29:43,216 that's underneath the shell. 684 00:29:44,270 --> 00:29:47,237 So you can drag a shell explosion on to 685 00:29:47,237 --> 00:29:49,237 Explosion Particles, you'll notice in the 686 00:29:49,237 --> 00:29:51,794 parenthesis it says Particle Systems. 687 00:29:52,182 --> 00:29:54,294 And for Explosion Audio it's the same 688 00:29:54,294 --> 00:29:57,468 object because that also has an audio source. 689 00:29:57,468 --> 00:30:00,605 But this time when you drag it you'll notice it picks that component. 690 00:30:01,008 --> 00:30:03,008 So Unity is smart that way, it knows which component 691 00:30:03,008 --> 00:30:05,008 you're referring to if you drag an object that's got that 692 00:30:05,008 --> 00:30:08,061 component it'll say 'great, okay, that must be what you mean'. 693 00:30:08,061 --> 00:30:10,061 And assign that particular component 694 00:30:10,061 --> 00:30:12,061 from the game object that you drag in. 695 00:30:12,657 --> 00:30:14,657 Then the other stuff that we've setup there, 696 00:30:15,045 --> 00:30:17,045 you know, it's setup based on 697 00:30:17,045 --> 00:30:19,045 something that we wanted to make, if we get to the 698 00:30:19,045 --> 00:30:21,045 end of the day and you want to have a huge 699 00:30:21,045 --> 00:30:23,045 amount of damage, you want to have one shot kills 700 00:30:23,045 --> 00:30:25,045 and you just up the maximum damage 701 00:30:25,433 --> 00:30:27,433 or add the explosion force and the tank 702 00:30:27,433 --> 00:30:29,433 just gets flung all the way off the screen. 703 00:30:29,433 --> 00:30:31,433 You can do that stuff. 704 00:30:31,433 --> 00:30:33,433 Obviously up the explosion radius as well. 705 00:30:34,168 --> 00:30:35,522 You can play around with that. 706 00:30:35,522 --> 00:30:37,522 For now let's stick with these values 707 00:30:37,522 --> 00:30:39,522 and see how we like that. 708 00:30:39,522 --> 00:30:41,522 And then the last thing we're going to do is to save the 709 00:30:41,522 --> 00:30:43,522 shell as a prefab, as I said before we don't want 710 00:30:43,522 --> 00:30:46,305 it to be in the game when it begins. 711 00:30:46,305 --> 00:30:48,305 So I'm going to select my Prefabs folder. 712 00:30:48,846 --> 00:30:50,846 I'm going to drag my shell and drop it in to 713 00:30:50,846 --> 00:30:52,846 the empty space in that folder. 714 00:30:54,954 --> 00:30:56,954 So really we should check if this works, so I'm just going 715 00:30:56,954 --> 00:30:58,954 to briefly save my scene and demonstrate 716 00:30:58,954 --> 00:31:00,954 what you should be seeing. 717 00:31:00,954 --> 00:31:02,954 So if I press Play right now 718 00:31:04,410 --> 00:31:06,410 my shell should fall down 719 00:31:06,410 --> 00:31:08,410 and because it's pretty near the tank 720 00:31:08,410 --> 00:31:10,410 take off quite a lot of damage. 721 00:31:10,715 --> 00:31:13,324 Take off a lot of health, sorry, apply a lot of damage. 722 00:31:14,435 --> 00:31:16,281 We've applied the shell explosion, 723 00:31:16,281 --> 00:31:19,149 in order to populate the particle effect 724 00:31:19,149 --> 00:31:22,251 and the audio source on to those two different variables. 725 00:31:22,251 --> 00:31:24,251 And then we've made it in to a prefab 726 00:31:24,251 --> 00:31:26,251 so now, as mentioned before, 727 00:31:26,251 --> 00:31:28,251 we don't need that shell in the game. 728 00:31:28,736 --> 00:31:30,736 Delete it out of your scene. 729 00:31:30,736 --> 00:31:33,633 Delete on PC, command-backspace on Mac 730 00:31:33,633 --> 00:31:35,898 and then save your scene. 731 00:31:35,898 --> 00:31:37,898 One more time, select the shell. 732 00:31:38,550 --> 00:31:40,550 delete it and then save your scene. 733 00:31:40,550 --> 00:31:42,759 Okay, so that's the end of phase 5. 734 00:31:44,077 --> 00:31:46,077 Then the next phase we're going to deal with 735 00:31:46,077 --> 00:31:48,691 actually shooting these projectiles. 736 00:31:48,691 --> 00:31:50,981 A little bit of physics and a bit more UI.