[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:01.76,0:00:04.07,Default,,0000,0000,0000,,In this phase we're going to create Dialogue: 0,0:00:04.07,0:00:06.86,Default,,0000,0000,0000,,this shell prefab, it looks like this Dialogue: 0,0:00:06.86,0:00:08.86,Default,,0000,0000,0000,,and has a number of different components, Dialogue: 0,0:00:08.86,0:00:11.93,Default,,0000,0000,0000,,it's got a collider, a rigidbody, it has a light component, Dialogue: 0,0:00:12.80,0:00:15.76,Default,,0000,0000,0000,,and the idea with this is that obviously we'll Dialogue: 0,0:00:15.76,0:00:18.88,Default,,0000,0000,0000,,fire it from the tank, but to demonstrate what it does, Dialogue: 0,0:00:18.88,0:00:20.88,Default,,0000,0000,0000,,it has an overlapped sphere Dialogue: 0,0:00:20.88,0:00:23.60,Default,,0000,0000,0000,,this is a kind of sphere of damage that it can Dialogue: 0,0:00:24.10,0:00:26.10,Default,,0000,0000,0000,,give to any of the tanks that i lands near. Dialogue: 0,0:00:26.47,0:00:28.47,Default,,0000,0000,0000,,So in this video what I'm doing is slowly moving it Dialogue: 0,0:00:28.47,0:00:30.47,Default,,0000,0000,0000,,closer to the tank and what you should notice is that Dialogue: 0,0:00:30.47,0:00:33.03,Default,,0000,0000,0000,,that health is getting reduced by more Dialogue: 0,0:00:33.03,0:00:35.03,Default,,0000,0000,0000,,the closer that that bullet happens to be. Dialogue: 0,0:00:36.02,0:00:38.02,Default,,0000,0000,0000,,Or at least it will do in a moment. Dialogue: 0,0:00:38.02,0:00:40.02,Default,,0000,0000,0000,,Off we go, moving it closer. Dialogue: 0,0:00:40.02,0:00:42.02,Default,,0000,0000,0000,,The closer it is the more damage that will be applied. Dialogue: 0,0:00:42.02,0:00:44.02,Default,,0000,0000,0000,,Obviously if you hit a tank dead-on then Dialogue: 0,0:00:44.02,0:00:46.02,Default,,0000,0000,0000,,that will be a huge amount of damage. Dialogue: 0,0:00:48.14,0:00:50.14,Default,,0000,0000,0000,,That's the shell that we're going to make and then in the next Dialogue: 0,0:00:50.14,0:00:52.14,Default,,0000,0000,0000,,phase after that we'll handle the actual shooting, Dialogue: 0,0:00:52.14,0:00:54.14,Default,,0000,0000,0000,,but first off we're going to handle the damage, Dialogue: 0,0:00:54.14,0:00:56.14,Default,,0000,0000,0000,,or the thing that's going to parse damage to Dialogue: 0,0:00:56.14,0:00:58.14,Default,,0000,0000,0000,,the TankHealth script that we did before. Dialogue: 0,0:00:58.75,0:01:00.75,Default,,0000,0000,0000,,What I would like everyone to do is look in Dialogue: 0,0:01:00.75,0:01:02.75,Default,,0000,0000,0000,,the Models folder, in this folder Dialogue: 0,0:01:02.75,0:01:05.43,Default,,0000,0000,0000,,you will find a model called Shell. Dialogue: 0,0:01:06.65,0:01:09.66,Default,,0000,0000,0000,,If you select it it will look like this in the inspector. Dialogue: 0,0:01:09.66,0:01:11.66,Default,,0000,0000,0000,,A little note, if you want to inspect something, Dialogue: 0,0:01:11.66,0:01:13.66,Default,,0000,0000,0000,,drag around it, you can just drag in that preview window Dialogue: 0,0:01:13.66,0:01:15.66,Default,,0000,0000,0000,,at the bottom of the inspector. Dialogue: 0,0:01:15.66,0:01:18.40,Default,,0000,0000,0000,,So what we'll do is drag that in to the hierarchy. Dialogue: 0,0:01:18.97,0:01:20.97,Default,,0000,0000,0000,,If we drag and drop that model from Dialogue: 0,0:01:20.97,0:01:22.62,Default,,0000,0000,0000,,the project to the hierarchy. Dialogue: 0,0:01:23.38,0:01:25.38,Default,,0000,0000,0000,,Move your cursor over the scene view, Dialogue: 0,0:01:25.38,0:01:27.38,Default,,0000,0000,0000,,press F to frame selected Dialogue: 0,0:01:27.38,0:01:29.38,Default,,0000,0000,0000,,and you'll notice it's at the origin, roughly, Dialogue: 0,0:01:29.38,0:01:31.38,Default,,0000,0000,0000,,so it's near to 0. Dialogue: 0,0:01:31.38,0:01:33.38,Default,,0000,0000,0000,,So all I'm going to do is just to drag mine Dialogue: 0,0:01:33.38,0:01:35.38,Default,,0000,0000,0000,,up slightly so it's somewhere I can Dialogue: 0,0:01:35.38,0:01:37.85,Default,,0000,0000,0000,,see it and work on it, something like that is fine. Dialogue: 0,0:01:39.31,0:01:41.31,Default,,0000,0000,0000,,Remember because we're creating a prefab that Dialogue: 0,0:01:41.31,0:01:43.31,Default,,0000,0000,0000,,we're going to fire with the tank this isn't something that's Dialogue: 0,0:01:43.31,0:01:45.83,Default,,0000,0000,0000,,going to be in the scene when the game starts Dialogue: 0,0:01:45.83,0:01:47.83,Default,,0000,0000,0000,,just like the tank, so we don't need to worry about Dialogue: 0,0:01:47.83,0:01:49.83,Default,,0000,0000,0000,,where it's positioned for now. Dialogue: 0,0:01:52.02,0:01:54.02,Default,,0000,0000,0000,,So because we are making Dialogue: 0,0:01:54.02,0:01:56.02,Default,,0000,0000,0000,,this something that's going to interact with other objects Dialogue: 0,0:01:56.02,0:01:59.45,Default,,0000,0000,0000,,it needs to have a collider. Dialogue: 0,0:01:59.45,0:02:01.45,Default,,0000,0000,0000,,We'll use primitive colliders because they're Dialogue: 0,0:02:01.45,0:02:04.47,Default,,0000,0000,0000,,nice and performant, and given the shape of this Dialogue: 0,0:02:04.47,0:02:06.47,Default,,0000,0000,0000,,we'll use a capsule collider. Dialogue: 0,0:02:06.47,0:02:08.47,Default,,0000,0000,0000,,So with that shell still selected Dialogue: 0,0:02:08.47,0:02:11.28,Default,,0000,0000,0000,,in the inspector we'll hit the Add Component button Dialogue: 0,0:02:11.28,0:02:13.28,Default,,0000,0000,0000,,and I'm just going to type Caps Dialogue: 0,0:02:13.28,0:02:15.28,Default,,0000,0000,0000,,for Capsule and then Dialogue: 0,0:02:15.28,0:02:17.28,Default,,0000,0000,0000,,click on it to confirm, or hit Return. Dialogue: 0,0:02:18.47,0:02:19.100,Default,,0000,0000,0000,,With that capsule collider, Dialogue: 0,0:02:20.61,0:02:22.61,Default,,0000,0000,0000,,because when you are firing things Dialogue: 0,0:02:22.61,0:02:25.73,Default,,0000,0000,0000,,like a projectile as we are. Dialogue: 0,0:02:26.59,0:02:28.59,Default,,0000,0000,0000,,We don't want things to be bouncing off one another Dialogue: 0,0:02:28.59,0:02:30.59,Default,,0000,0000,0000,,and we don't expect this tank shell to Dialogue: 0,0:02:30.59,0:02:32.59,Default,,0000,0000,0000,,ever hit something and push it back, Dialogue: 0,0:02:32.59,0:02:34.59,Default,,0000,0000,0000,,we're expecting it to hit something, Dialogue: 0,0:02:34.59,0:02:36.59,Default,,0000,0000,0000,,create a call back in the script and then create Dialogue: 0,0:02:36.59,0:02:38.59,Default,,0000,0000,0000,,an explosion force. Dialogue: 0,0:02:38.59,0:02:40.59,Default,,0000,0000,0000,,So we don't want any physical interactions. Dialogue: 0,0:02:41.06,0:02:44.13,Default,,0000,0000,0000,,And when we don't want those we use what's called triggers. Dialogue: 0,0:02:44.13,0:02:46.13,Default,,0000,0000,0000,,If you hear someone refer to a trigger Dialogue: 0,0:02:46.13,0:02:48.13,Default,,0000,0000,0000,,or a trigger zone, Dialogue: 0,0:02:48.13,0:02:50.13,Default,,0000,0000,0000,,or a trigger area they're usually referring to Dialogue: 0,0:02:50.13,0:02:52.13,Default,,0000,0000,0000,,a collider that's been checked Dialogue: 0,0:02:52.13,0:02:54.13,Default,,0000,0000,0000,,as a trigger. Dialogue: 0,0:02:54.13,0:02:56.86,Default,,0000,0000,0000,,So if you check the Is Trigger checkbox on that, Dialogue: 0,0:02:57.41,0:03:00.19,Default,,0000,0000,0000,,what that basically means is that it doesn't have Dialogue: 0,0:03:00.19,0:03:02.19,Default,,0000,0000,0000,,it doesn't have a solid surface to it Dialogue: 0,0:03:02.19,0:03:04.19,Default,,0000,0000,0000,,it can intersect, but if can send a Dialogue: 0,0:03:04.19,0:03:05.48,Default,,0000,0000,0000,,call back to the script saying Dialogue: 0,0:03:05.48,0:03:07.48,Default,,0000,0000,0000,,'I've now entered another trigger' or Dialogue: 0,0:03:07.48,0:03:09.48,Default,,0000,0000,0000,,'I've entered another collider'. Dialogue: 0,0:03:09.48,0:03:11.48,Default,,0000,0000,0000,,So often trigger colliders are used for things like Dialogue: 0,0:03:11.48,0:03:13.48,Default,,0000,0000,0000,,doors in games, like when you put a big Dialogue: 0,0:03:13.48,0:03:15.48,Default,,0000,0000,0000,,cubic collider in front of Dialogue: 0,0:03:15.48,0:03:17.48,Default,,0000,0000,0000,,a doorway and then when the character enters that way Dialogue: 0,0:03:17.48,0:03:19.48,Default,,0000,0000,0000,,you automatically open the door. Dialogue: 0,0:03:19.48,0:03:22.20,Default,,0000,0000,0000,,It's a pretty classic use of that kind of collider. Dialogue: 0,0:03:23.17,0:03:25.17,Default,,0000,0000,0000,,Once you've added that component, the next thing Dialogue: 0,0:03:25.17,0:03:27.17,Default,,0000,0000,0000,,we want to do is actually set it up. Dialogue: 0,0:03:29.07,0:03:31.07,Default,,0000,0000,0000,,So we need to change the centre, Dialogue: 0,0:03:31.07,0:03:34.94,Default,,0000,0000,0000,,so the centre value for that is (0, 0, 0.2). Dialogue: 0,0:03:36.57,0:03:39.13,Default,,0000,0000,0000,,And the radius is 0.15. Dialogue: 0,0:03:41.71,0:03:43.71,Default,,0000,0000,0000,,And the height is 0.55. Dialogue: 0,0:03:46.45,0:03:48.73,Default,,0000,0000,0000,,Crucially you'll notice that it's not Dialogue: 0,0:03:48.73,0:03:52.71,Default,,0000,0000,0000,,looking in the right direction, so we set the direction Dialogue: 0,0:03:52.71,0:03:55.74,Default,,0000,0000,0000,,to Z axis, and then it should Dialogue: 0,0:03:55.74,0:03:58.72,Default,,0000,0000,0000,,pretty much match our shell perfectly. Dialogue: 0,0:03:58.72,0:04:01.53,Default,,0000,0000,0000,,Or to an amount of precision that would never cause issues. Dialogue: 0,0:04:02.23,0:04:04.23,Default,,0000,0000,0000,,It should look like that. Dialogue: 0,0:04:05.14,0:04:07.65,Default,,0000,0000,0000,,And then finally because this is a physics object Dialogue: 0,0:04:07.65,0:04:09.65,Default,,0000,0000,0000,,we need it to be under control of Dialogue: 0,0:04:09.65,0:04:11.97,Default,,0000,0000,0000,,the physics engine, and so what are we going to add? Dialogue: 0,0:04:12.58,0:04:14.58,Default,,0000,0000,0000,,A rigidbody, there we go, nice and loud. Dialogue: 0,0:04:16.50,0:04:18.50,Default,,0000,0000,0000,,So add our rigidbody component. Dialogue: 0,0:04:20.28,0:04:21.23,Default,,0000,0000,0000,,And it's good to go. Dialogue: 0,0:04:21.23,0:04:23.23,Default,,0000,0000,0000,,So these shells are going to fire Dialogue: 0,0:04:23.23,0:04:25.23,Default,,0000,0000,0000,,a projectile, they're not going to just keep going straight forward, Dialogue: 0,0:04:25.23,0:04:27.23,Default,,0000,0000,0000,,they're going to fall with gravity over time. Dialogue: 0,0:04:27.92,0:04:29.92,Default,,0000,0000,0000,,So just a quick recap of those two points. Dialogue: 0,0:04:30.84,0:04:34.77,Default,,0000,0000,0000,,Okay, so we've found the model named Shell in the Models folder. Dialogue: 0,0:04:35.74,0:04:37.74,Default,,0000,0000,0000,,We dragged it in to the hierarchy. Dialogue: 0,0:04:38.41,0:04:41.24,Default,,0000,0000,0000,,We've added a capsule collider component to the shell Dialogue: 0,0:04:41.85,0:04:43.85,Default,,0000,0000,0000,,and we've made sure it's a trigger so that that Dialogue: 0,0:04:43.85,0:04:45.85,Default,,0000,0000,0000,,trigger won't bounce off of anything Dialogue: 0,0:04:45.85,0:04:48.48,Default,,0000,0000,0000,,it's just going to pass straight through stuff Dialogue: 0,0:04:48.48,0:04:50.48,Default,,0000,0000,0000,,and then immediately be destroyed by the script Dialogue: 0,0:04:50.48,0:04:52.19,Default,,0000,0000,0000,,that we're going to write. Dialogue: 0,0:04:52.19,0:04:54.86,Default,,0000,0000,0000,,We've checked Is Trigger to make sure that is the case. Dialogue: 0,0:04:57.58,0:05:00.10,Default,,0000,0000,0000,,Then we set the direction to Z axis Dialogue: 0,0:05:00.10,0:05:02.10,Default,,0000,0000,0000,,and we changed the centre, so the centre is Dialogue: 0,0:05:02.10,0:05:04.10,Default,,0000,0000,0000,,basically where that originates from so you can Dialogue: 0,0:05:04.10,0:05:06.84,Default,,0000,0000,0000,,kind of offset your colliders if you need to, art-wise. Dialogue: 0,0:05:07.43,0:05:11.70,Default,,0000,0000,0000,,And the radius we've set to 0.15, the radius of the capsule. Dialogue: 0,0:05:11.70,0:05:13.70,Default,,0000,0000,0000,,And the height to 0.55 Dialogue: 0,0:05:13.70,0:05:15.70,Default,,0000,0000,0000,,And we added a rigidbody to make sure that it's Dialogue: 0,0:05:15.70,0:05:18.39,Default,,0000,0000,0000,,under the control of the physics engine. Dialogue: 0,0:05:21.72,0:05:23.72,Default,,0000,0000,0000,,So we've got the shell Dialogue: 0,0:05:23.72,0:05:25.72,Default,,0000,0000,0000,,and currently the shell can fall down Dialogue: 0,0:05:25.72,0:05:27.72,Default,,0000,0000,0000,,and nothing is going to really happen. Dialogue: 0,0:05:27.72,0:05:29.72,Default,,0000,0000,0000,,It's just going to go straight through the ground Dialogue: 0,0:05:29.72,0:05:31.72,Default,,0000,0000,0000,,because it's not going to stop, it hasn't got a Dialogue: 0,0:05:31.72,0:05:33.19,Default,,0000,0000,0000,,physical presence, like I said. Dialogue: 0,0:05:33.19,0:05:35.61,Default,,0000,0000,0000,,So we don't want that, we want it to actually Dialogue: 0,0:05:35.61,0:05:38.23,Default,,0000,0000,0000,,explode and do stuff, so we've created Dialogue: 0,0:05:38.23,0:05:40.23,Default,,0000,0000,0000,,a shell explosion for you. Dialogue: 0,0:05:40.88,0:05:42.94,Default,,0000,0000,0000,,So if you look in the Prefabs folder Dialogue: 0,0:05:43.48,0:05:46.12,Default,,0000,0000,0000,,you will find a prefab called ShellExplosion. Dialogue: 0,0:05:47.70,0:05:49.12,Default,,0000,0000,0000,,So what we're going to do with that is make it Dialogue: 0,0:05:49.12,0:05:50.80,Default,,0000,0000,0000,,a child of the shell. Dialogue: 0,0:05:51.35,0:05:53.35,Default,,0000,0000,0000,,So if you drag from the Prefabs folder and drop Dialogue: 0,0:05:53.35,0:05:54.58,Default,,0000,0000,0000,,it on to the shell Dialogue: 0,0:05:55.71,0:05:56.95,Default,,0000,0000,0000,,It should look like that. Dialogue: 0,0:05:58.00,0:06:00.00,Default,,0000,0000,0000,,And when we simulate it Dialogue: 0,0:06:01.06,0:06:02.09,Default,,0000,0000,0000,,it looks like this. Dialogue: 0,0:06:02.09,0:06:05.16,Default,,0000,0000,0000,,A cloud of dust that kind of falls over time. Dialogue: 0,0:06:09.79,0:06:11.79,Default,,0000,0000,0000,,Okay and then the only other thing that we need Dialogue: 0,0:06:11.79,0:06:13.79,Default,,0000,0000,0000,,to do with that is to Dialogue: 0,0:06:13.79,0:06:15.79,Default,,0000,0000,0000,,add an audio source, Dialogue: 0,0:06:15.79,0:06:17.79,Default,,0000,0000,0000,,so similar to the tank exploding, Dialogue: 0,0:06:17.79,0:06:20.37,Default,,0000,0000,0000,,it needs to have visual effects and an audio effect. Dialogue: 0,0:06:20.89,0:06:22.89,Default,,0000,0000,0000,,So I'm going to click the Add Component button Dialogue: 0,0:06:22.89,0:06:24.89,Default,,0000,0000,0000,,and I'm going to choose Audio Source. Dialogue: 0,0:06:25.94,0:06:27.94,Default,,0000,0000,0000,,And then just like before we need an Dialogue: 0,0:06:27.94,0:06:29.94,Default,,0000,0000,0000,,audio clip, so I'm going to use the Dialogue: 0,0:06:29.94,0:06:31.94,Default,,0000,0000,0000,,circle select next to audio clip Dialogue: 0,0:06:32.47,0:06:35.15,Default,,0000,0000,0000,,and choose ShellExplosion to assign it. Dialogue: 0,0:06:37.18,0:06:39.18,Default,,0000,0000,0000,,Now we don't want this to play as soon as the shell Dialogue: 0,0:06:39.18,0:06:41.90,Default,,0000,0000,0000,,gets instantiated, as soon as the explosion appears, Dialogue: 0,0:06:42.61,0:06:44.61,Default,,0000,0000,0000,,so we're going to uncheck Play On Awake. Dialogue: 0,0:06:45.95,0:06:48.93,Default,,0000,0000,0000,,A quick recap, so we've got our ShellExplosion prefab, Dialogue: 0,0:06:48.93,0:06:52.02,Default,,0000,0000,0000,,we had to make it a child of the Shell game object Dialogue: 0,0:06:52.02,0:06:54.02,Default,,0000,0000,0000,,so it's going to travel with it and then do it's Dialogue: 0,0:06:54.02,0:06:56.02,Default,,0000,0000,0000,,thing as soon as it hits something. Dialogue: 0,0:06:56.02,0:06:58.02,Default,,0000,0000,0000,,We added an audio source to that Dialogue: 0,0:06:58.02,0:07:01.02,Default,,0000,0000,0000,,so that it's able to playback sound and we've used Dialogue: 0,0:07:01.02,0:07:04.50,Default,,0000,0000,0000,,circle select to set the audio clip to ShellExplosion. Dialogue: 0,0:07:04.50,0:07:06.50,Default,,0000,0000,0000,,We don't want it to play as soon as it enters Dialogue: 0,0:07:06.50,0:07:08.50,Default,,0000,0000,0000,,the world so we only want it to play Dialogue: 0,0:07:08.50,0:07:11.81,Default,,0000,0000,0000,,as soon as it impacts, so we uncheck Play On Awake. Dialogue: 0,0:07:15.58,0:07:17.58,Default,,0000,0000,0000,,Okay, the final thing we're going to do with the shell Dialogue: 0,0:07:17.58,0:07:20.73,Default,,0000,0000,0000,,so reselect the shell, the parent object, Dialogue: 0,0:07:20.73,0:07:22.30,Default,,0000,0000,0000,,is to add a light. Dialogue: 0,0:07:22.30,0:07:24.30,Default,,0000,0000,0000,,So as this travels across the Dialogue: 0,0:07:24.30,0:07:26.71,Default,,0000,0000,0000,,game board we want this to Dialogue: 0,0:07:26.71,0:07:28.42,Default,,0000,0000,0000,,light up the environment Dialogue: 0,0:07:28.42,0:07:30.42,Default,,0000,0000,0000,,and it helps pick out the shell, because obviously Dialogue: 0,0:07:30.42,0:07:32.42,Default,,0000,0000,0000,,you've got a gold shell, you've got orange sand, Dialogue: 0,0:07:32.42,0:07:36.35,Default,,0000,0000,0000,,it's kind of hard to see it and the light helps pick it out Dialogue: 0,0:07:36.35,0:07:39.11,Default,,0000,0000,0000,,so instead of creating a point light game object Dialogue: 0,0:07:39.11,0:07:42.14,Default,,0000,0000,0000,,we can just add that same light. Dialogue: 0,0:07:42.14,0:07:44.14,Default,,0000,0000,0000,,So if you've new to Unity, what you'll notice Dialogue: 0,0:07:44.14,0:07:46.14,Default,,0000,0000,0000,,if you go to Game Object - Create Dialogue: 0,0:07:46.14,0:07:48.14,Default,,0000,0000,0000,,is that you've got all these readymade Dialogue: 0,0:07:48.14,0:07:50.82,Default,,0000,0000,0000,,prefabbed game objects with several components on them. Dialogue: 0,0:07:50.82,0:07:52.82,Default,,0000,0000,0000,,So for example if you choose, Dialogue: 0,0:07:52.82,0:07:54.82,Default,,0000,0000,0000,,if there was a cube, then you've got Dialogue: 0,0:07:54.82,0:07:56.82,Default,,0000,0000,0000,,a bunch of different components on it. Dialogue: 0,0:07:56.82,0:07:59.78,Default,,0000,0000,0000,,But you could also create a light Dialogue: 0,0:08:00.27,0:08:02.27,Default,,0000,0000,0000,,which has just the light component on it. Dialogue: 0,0:08:02.60,0:08:04.60,Default,,0000,0000,0000,,We don't need to do that, we're just going to put the Dialogue: 0,0:08:04.60,0:08:06.60,Default,,0000,0000,0000,,component directly on to the shell, Dialogue: 0,0:08:06.60,0:08:08.60,Default,,0000,0000,0000,,so I got to Add Component - Light Dialogue: 0,0:08:09.40,0:08:11.40,Default,,0000,0000,0000,,So now we have to actually handle Dialogue: 0,0:08:11.40,0:08:13.40,Default,,0000,0000,0000,,how that behaves, how it's going to Dialogue: 0,0:08:13.40,0:08:15.87,Default,,0000,0000,0000,,impact and what it's going to do when it hits Dialogue: 0,0:08:15.87,0:08:17.87,Default,,0000,0000,0000,,a tank or when it hits the environment and the Dialogue: 0,0:08:17.87,0:08:20.74,Default,,0000,0000,0000,,kind of forces it's going to apply to other tanks in the game. Dialogue: 0,0:08:21.62,0:08:23.62,Default,,0000,0000,0000,,So what we're going to do is to look Dialogue: 0,0:08:23.62,0:08:25.62,Default,,0000,0000,0000,,in the Scripts folder and Dialogue: 0,0:08:25.62,0:08:27.62,Default,,0000,0000,0000,,find the Shell folder. Dialogue: 0,0:08:27.62,0:08:31.35,Default,,0000,0000,0000,,In that you will find a script called ShellExplosion. Dialogue: 0,0:08:32.75,0:08:35.74,Default,,0000,0000,0000,,So we're going to drag and drop ShellExplosion on to the Shell Dialogue: 0,0:08:35.74,0:08:37.74,Default,,0000,0000,0000,,game object in the hierarchy. Dialogue: 0,0:08:38.50,0:08:40.15,Default,,0000,0000,0000,,And then you should see Dialogue: 0,0:08:40.77,0:08:42.77,Default,,0000,0000,0000,,that along with everything else, the rigidbody, Dialogue: 0,0:08:42.77,0:08:45.30,Default,,0000,0000,0000,,the capsule collider, the light that we just added, Dialogue: 0,0:08:45.30,0:08:47.78,Default,,0000,0000,0000,,you've now got Shell Explosion (Script) Dialogue: 0,0:08:47.78,0:08:50.56,Default,,0000,0000,0000,,and we're going to double click on that to open it up for editing. Dialogue: 0,0:08:54.30,0:08:57.25,Default,,0000,0000,0000,,Okay, so in this one we don't have any private Dialogue: 0,0:08:57.25,0:09:00.40,Default,,0000,0000,0000,,variables but we do have a bunch of public ones. Dialogue: 0,0:09:00.40,0:09:02.40,Default,,0000,0000,0000,,So the first one is a layer mask. Dialogue: 0,0:09:02.40,0:09:04.40,Default,,0000,0000,0000,,Now you might remember at the beginning of the day Dialogue: 0,0:09:04.40,0:09:07.29,Default,,0000,0000,0000,,we put all the tanks on the Players layer. Dialogue: 0,0:09:07.29,0:09:09.29,Default,,0000,0000,0000,,So that means that when we cause an Dialogue: 0,0:09:09.29,0:09:11.29,Default,,0000,0000,0000,,explosion from the shell we can just Dialogue: 0,0:09:11.29,0:09:13.29,Default,,0000,0000,0000,,check for everything that's on the Players layer Dialogue: 0,0:09:13.29,0:09:17.44,Default,,0000,0000,0000,,and see which tanks we've hit rather than trying to affect everything. Dialogue: 0,0:09:17.44,0:09:19.44,Default,,0000,0000,0000,,So that will be available in the inspector Dialogue: 0,0:09:19.44,0:09:21.44,Default,,0000,0000,0000,,and we can choose which layer we want to affect. Dialogue: 0,0:09:22.34,0:09:25.81,Default,,0000,0000,0000,,The next is the explosion particle, Dialogue: 0,0:09:25.81,0:09:28.75,Default,,0000,0000,0000,,remember we just dragged on that as a child game object Dialogue: 0,0:09:28.75,0:09:30.75,Default,,0000,0000,0000,,we're going to get a reference to that so we can Dialogue: 0,0:09:30.75,0:09:32.75,Default,,0000,0000,0000,,play the particles when the shell explodes. Dialogue: 0,0:09:33.49,0:09:35.49,Default,,0000,0000,0000,,Likewise there's an audio source reference, Dialogue: 0,0:09:35.49,0:09:38.09,Default,,0000,0000,0000,,so that's for getting a reference to that audio source Dialogue: 0,0:09:38.09,0:09:39.54,Default,,0000,0000,0000,,so we can play a sound. Dialogue: 0,0:09:39.54,0:09:41.54,Default,,0000,0000,0000,,Then we've got a bunch of floats, Dialogue: 0,0:09:41.54,0:09:43.54,Default,,0000,0000,0000,,so the first one is the most Dialogue: 0,0:09:43.54,0:09:45.54,Default,,0000,0000,0000,,damage that it can possibly do, Dialogue: 0,0:09:45.54,0:09:47.21,Default,,0000,0000,0000,,the maximum damage, and we're setting that to 100. Dialogue: 0,0:09:47.21,0:09:49.88,Default,,0000,0000,0000,,so if you get a perfect direct hit Dialogue: 0,0:09:49.88,0:09:51.88,Default,,0000,0000,0000,,then you'll kill the tank outright. Dialogue: 0,0:09:52.20,0:09:54.20,Default,,0000,0000,0000,,But you can't get a perfect hit because Dialogue: 0,0:09:54.20,0:09:56.20,Default,,0000,0000,0000,,it's never going to be exactly on the position Dialogue: 0,0:09:56.20,0:09:58.20,Default,,0000,0000,0000,,of the tank because the collider is covering it. Dialogue: 0,0:09:59.47,0:10:01.47,Default,,0000,0000,0000,,So you're going to get close but no cigar. Dialogue: 0,0:10:01.47,0:10:03.47,Default,,0000,0000,0000,,The next one is the amount of force Dialogue: 0,0:10:03.47,0:10:06.14,Default,,0000,0000,0000,,that the explosion is going to do right at it's centre. Dialogue: 0,0:10:06.92,0:10:08.92,Default,,0000,0000,0000,,Likewise as the explosion radius Dialogue: 0,0:10:08.92,0:10:10.92,Default,,0000,0000,0000,,gets further out the tank will be Dialogue: 0,0:10:10.92,0:10:13.39,Default,,0000,0000,0000,,less affected by the explosion, you'll feel less force. Dialogue: 0,0:10:14.75,0:10:16.75,Default,,0000,0000,0000,,We've got the max lifetime, so we don't want Dialogue: 0,0:10:16.75,0:10:18.75,Default,,0000,0000,0000,,shells to exist forever Dialogue: 0,0:10:18.75,0:10:20.75,Default,,0000,0000,0000,,so if they happen to miss everything Dialogue: 0,0:10:20.75,0:10:22.75,Default,,0000,0000,0000,,we don't want them to stay in the scene forever so Dialogue: 0,0:10:22.75,0:10:24.11,Default,,0000,0000,0000,,we give them a lifetime, Dialogue: 0,0:10:24.11,0:10:26.11,Default,,0000,0000,0000,,and at the start of the scene we'll say Dialogue: 0,0:10:26.51,0:10:28.51,Default,,0000,0000,0000,,if it's not dead after 2 seconds just kill it anyway. Dialogue: 0,0:10:29.29,0:10:31.29,Default,,0000,0000,0000,,So it's pretty unlikely you'll ever see that, Dialogue: 0,0:10:31.29,0:10:33.29,Default,,0000,0000,0000,,it would either hit the ground or Dialogue: 0,0:10:33.29,0:10:35.29,Default,,0000,0000,0000,,some other object before that ever happened, Dialogue: 0,0:10:35.29,0:10:37.29,Default,,0000,0000,0000,,but it's just a failsafe that we'll write in. Dialogue: 0,0:10:37.74,0:10:39.74,Default,,0000,0000,0000,,So the last public variable is Dialogue: 0,0:10:39.74,0:10:41.74,Default,,0000,0000,0000,,the explosion radius. Dialogue: 0,0:10:41.74,0:10:43.74,Default,,0000,0000,0000,,And that is literally how far Dialogue: 0,0:10:43.74,0:10:46.53,Default,,0000,0000,0000,,from the shell it's going to affect when it explodes. Dialogue: 0,0:10:48.39,0:10:50.39,Default,,0000,0000,0000,,Okay, so let's look at these functions Dialogue: 0,0:10:50.39,0:10:52.39,Default,,0000,0000,0000,,and as we're just mentioning Dialogue: 0,0:10:52.39,0:10:54.39,Default,,0000,0000,0000,,in the Start function we're saying destroy Dialogue: 0,0:10:54.39,0:10:56.39,Default,,0000,0000,0000,,the game object Dialogue: 0,0:10:56.39,0:10:58.39,Default,,0000,0000,0000,,after the max lifetime. Dialogue: 0,0:10:58.39,0:11:01.24,Default,,0000,0000,0000,,So that's saying when you instantiate this Dialogue: 0,0:11:01.65,0:11:04.56,Default,,0000,0000,0000,,if it's still alive after 2 seconds, destroy it. Dialogue: 0,0:11:06.05,0:11:08.84,Default,,0000,0000,0000,,And now we're going to have to write some scripts. Dialogue: 0,0:11:10.90,0:11:12.47,Default,,0000,0000,0000,,So the first thing we want to do, Dialogue: 0,0:11:12.47,0:11:14.47,Default,,0000,0000,0000,,when OnTriggerEnter is called, so you remember we Dialogue: 0,0:11:14.47,0:11:17.80,Default,,0000,0000,0000,,put Is Trigger as True, that means that Dialogue: 0,0:11:17.80,0:11:19.80,Default,,0000,0000,0000,,OnTriggerEnter is called when something Dialogue: 0,0:11:19.80,0:11:21.03,Default,,0000,0000,0000,,bumps in to this. Dialogue: 0,0:11:21.03,0:11:23.03,Default,,0000,0000,0000,,So this function will Dialogue: 0,0:11:23.03,0:11:25.03,Default,,0000,0000,0000,,happen whenever it intersects with anything Dialogue: 0,0:11:25.03,0:11:27.03,Default,,0000,0000,0000,,and then we need to check what it's Dialogue: 0,0:11:27.03,0:11:29.03,Default,,0000,0000,0000,,intersected with and what to do as a result. Dialogue: 0,0:11:29.03,0:11:31.03,Default,,0000,0000,0000,,So we have this argument Dialogue: 0,0:11:31.03,0:11:33.41,Default,,0000,0000,0000,,which we've just called other which is of type Collider. Dialogue: 0,0:11:33.41,0:11:35.41,Default,,0000,0000,0000,,So it's going to say 'okay, we've intersected Dialogue: 0,0:11:35.41,0:11:38.13,Default,,0000,0000,0000,,with something else, it's a collider, what do we do about it?' Dialogue: 0,0:11:39.23,0:11:41.23,Default,,0000,0000,0000,,In this case we don't actually care what we're bumping in to Dialogue: 0,0:11:41.23,0:11:43.23,Default,,0000,0000,0000,,just that it's a collider, Dialogue: 0,0:11:43.23,0:11:45.23,Default,,0000,0000,0000,,so we should explode. Dialogue: 0,0:11:45.23,0:11:46.44,Default,,0000,0000,0000,,But what do we do first? Dialogue: 0,0:11:46.44,0:11:48.44,Default,,0000,0000,0000,,First of all we need to gather up all of the Dialogue: 0,0:11:48.44,0:11:50.44,Default,,0000,0000,0000,,colliders that we need to affect. Dialogue: 0,0:11:50.44,0:11:52.44,Default,,0000,0000,0000,,So we need an array of colliders Dialogue: 0,0:11:52.44,0:11:54.84,Default,,0000,0000,0000,,and you'll remember that's the square brackets after a collider. Dialogue: 0,0:11:55.33,0:11:57.33,Default,,0000,0000,0000,,And we'll call that array Colliders. Dialogue: 0,0:11:58.08,0:12:00.08,Default,,0000,0000,0000,,And we're going to set that to the return of Dialogue: 0,0:12:00.08,0:12:02.08,Default,,0000,0000,0000,,a function called OverlapSphere. Dialogue: 0,0:12:02.08,0:12:05.55,Default,,0000,0000,0000,,So that's Physics.OverlapSphere Dialogue: 0,0:12:08.17,0:12:10.94,Default,,0000,0000,0000,,And what that's going to do is similar to a ray cast, Dialogue: 0,0:12:10.94,0:12:14.01,Default,,0000,0000,0000,,it's going to create an imaginary sphere Dialogue: 0,0:12:14.81,0:12:17.28,Default,,0000,0000,0000,,and everything that overlaps with that sphere Dialogue: 0,0:12:17.28,0:12:20.15,Default,,0000,0000,0000,,or is inside the sphere is going to be collected. Dialogue: 0,0:12:20.87,0:12:22.87,Default,,0000,0000,0000,,And we want that sphere to be at Dialogue: 0,0:12:22.87,0:12:24.87,Default,,0000,0000,0000,,the shell's location, so transform.position Dialogue: 0,0:12:24.87,0:12:26.87,Default,,0000,0000,0000,,for it's position. Dialogue: 0,0:12:27.41,0:12:29.41,Default,,0000,0000,0000,,It's going to have a radius that we've already Dialogue: 0,0:12:29.41,0:12:31.12,Default,,0000,0000,0000,,got as an ExplosionRadius. Dialogue: 0,0:12:31.62,0:12:33.62,Default,,0000,0000,0000,,And we're going to use that TankMask Dialogue: 0,0:12:33.62,0:12:35.62,Default,,0000,0000,0000,,so that it's only picking up the tanks. Dialogue: 0,0:12:40.18,0:12:42.18,Default,,0000,0000,0000,,Okay, so just a quick note about layers. Dialogue: 0,0:12:42.18,0:12:44.18,Default,,0000,0000,0000,,Layers we use for various different things Dialogue: 0,0:12:44.18,0:12:46.18,Default,,0000,0000,0000,,in Unity but ostensibly they're Dialogue: 0,0:12:46.18,0:12:48.18,Default,,0000,0000,0000,,just for grouping things together in Dialogue: 0,0:12:48.18,0:12:50.18,Default,,0000,0000,0000,,some way that you want to use Dialogue: 0,0:12:50.18,0:12:52.77,Default,,0000,0000,0000,,to either include of exclude them from something. Dialogue: 0,0:12:52.77,0:12:54.77,Default,,0000,0000,0000,,In this case we're using it as a way of Dialogue: 0,0:12:54.77,0:12:57.71,Default,,0000,0000,0000,,only including or sort of masking out Dialogue: 0,0:12:57.71,0:12:59.71,Default,,0000,0000,0000,,anything but tanks, which is why we've called this Dialogue: 0,0:12:59.71,0:13:03.92,Default,,0000,0000,0000,,variable TankMask, because it's essentially a layer on Dialogue: 0,0:13:03.92,0:13:05.83,Default,,0000,0000,0000,,which only the tanks exist. Dialogue: 0,0:13:05.83,0:13:07.83,Default,,0000,0000,0000,,But don't think of it in any visual terms, Dialogue: 0,0:13:07.83,0:13:10.34,Default,,0000,0000,0000,,if anyone's in to Photoshop or anything like that Dialogue: 0,0:13:10.34,0:13:13.17,Default,,0000,0000,0000,,there's no kind of layering in that sense. Dialogue: 0,0:13:13.17,0:13:15.73,Default,,0000,0000,0000,,Okay, so now we've got all our colliders Dialogue: 0,0:13:15.73,0:13:17.73,Default,,0000,0000,0000,,we need to go through them all and Dialogue: 0,0:13:17.73,0:13:19.73,Default,,0000,0000,0000,,decide what we're going to do. Dialogue: 0,0:13:20.19,0:13:22.19,Default,,0000,0000,0000,,So for that we need to Dialogue: 0,0:13:22.19,0:13:24.19,Default,,0000,0000,0000,,loop through them all using a For Loop. Dialogue: 0,0:13:24.64,0:13:26.64,Default,,0000,0000,0000,,So this is the same format as we saw before Dialogue: 0,0:13:27.59,0:13:30.24,Default,,0000,0000,0000,,for(int i = 0 Dialogue: 0,0:13:30.96,0:13:33.24,Default,,0000,0000,0000,,i is less than colliders.length Dialogue: 0,0:13:33.24,0:13:35.78,Default,,0000,0000,0000,,so this is, colliders is the array that we're going Dialogue: 0,0:13:35.78,0:13:38.08,Default,,0000,0000,0000,,through so it's less than colliders.Length. Dialogue: 0,0:13:38.08,0:13:41.80,Default,,0000,0000,0000,,And then i++ means that we add to i for every loop. Dialogue: 0,0:13:43.27,0:13:47.07,Default,,0000,0000,0000,,Then open and close brackets? Dialogue: 0,0:13:47.07,0:13:49.07,Default,,0000,0000,0000,,Brackets? Yes brackets. Dialogue: 0,0:13:49.57,0:13:51.57,Default,,0000,0000,0000,,And then we can Dialogue: 0,0:13:51.57,0:13:53.89,Default,,0000,0000,0000,,use whatever we do in that for loop Dialogue: 0,0:13:53.89,0:13:55.89,Default,,0000,0000,0000,,to affect each of the colliders. Dialogue: 0,0:13:55.89,0:13:57.89,Default,,0000,0000,0000,,So the first thing we want to do is check Dialogue: 0,0:13:57.89,0:13:59.89,Default,,0000,0000,0000,,whether the thing that we've hit has Dialogue: 0,0:13:59.89,0:14:01.89,Default,,0000,0000,0000,,a rigidbody, so what we do is we Dialogue: 0,0:14:01.89,0:14:04.33,Default,,0000,0000,0000,,create a rigidbody variable Dialogue: 0,0:14:04.33,0:14:06.33,Default,,0000,0000,0000,,which we'll call targetRigidbody Dialogue: 0,0:14:06.86,0:14:11.32,Default,,0000,0000,0000,,and we'll set that equal to colliders[i], so that's Dialogue: 0,0:14:11.32,0:14:13.32,Default,,0000,0000,0000,,the collider at the position that we're Dialogue: 0,0:14:13.32,0:14:15.32,Default,,0000,0000,0000,,currently iterating through in the array, Dialogue: 0,0:14:16.58,0:14:20.03,Default,,0000,0000,0000,,.GetComponent, Dialogue: 0,0:14:20.03,0:14:22.03,Default,,0000,0000,0000,,so we'll use the angled brackets for rigidbody. Dialogue: 0,0:14:23.72,0:14:25.72,Default,,0000,0000,0000,,So what that's going to do is Dialogue: 0,0:14:25.72,0:14:27.72,Default,,0000,0000,0000,,get that collider that we've just gone through Dialogue: 0,0:14:27.72,0:14:30.36,Default,,0000,0000,0000,,the array and try and find a rigidbody on it. Dialogue: 0,0:14:31.29,0:14:33.29,Default,,0000,0000,0000,,Next we need to know if it's actually Dialogue: 0,0:14:33.29,0:14:36.10,Default,,0000,0000,0000,,found a rigidbody or if it's just found nothing. Dialogue: 0,0:14:37.30,0:14:40.11,Default,,0000,0000,0000,,So what we're going to do is say if Dialogue: 0,0:14:41.84,0:14:43.84,Default,,0000,0000,0000,,not target(!Rigidbody) Dialogue: 0,0:14:43.84,0:14:47.12,Default,,0000,0000,0000,,so what that's going to do is say Dialogue: 0,0:14:47.12,0:14:49.52,Default,,0000,0000,0000,,if there's nothing there then what do I do? Dialogue: 0,0:14:50.03,0:14:52.03,Default,,0000,0000,0000,,And if there's nothing there we want to continue Dialogue: 0,0:14:52.03,0:14:54.03,Default,,0000,0000,0000,,on to the next loop, on the the next collider. Dialogue: 0,0:14:54.03,0:14:56.03,Default,,0000,0000,0000,,You'll remember it's going back to the Dialogue: 0,0:14:56.03,0:14:58.03,Default,,0000,0000,0000,,start of the for loop and carrying on looking Dialogue: 0,0:14:58.03,0:15:00.68,Default,,0000,0000,0000,,for any collider that's got a rigidbody Dialogue: 0,0:15:00.68,0:15:03.42,Default,,0000,0000,0000,,it's not continuing through a list of commands. Dialogue: 0,0:15:03.42,0:15:05.42,Default,,0000,0000,0000,,So bear in mind that this is a bit of a Dialogue: 0,0:15:05.42,0:15:07.42,Default,,0000,0000,0000,,safety catch kind of thing because we're making it Dialogue: 0,0:15:07.42,0:15:09.42,Default,,0000,0000,0000,,everything that's on the Players layer anyway. Dialogue: 0,0:15:09.78,0:15:11.78,Default,,0000,0000,0000,,So everything should have a collider Dialogue: 0,0:15:12.32,0:15:14.32,Default,,0000,0000,0000,,and everything that has a collider there will also Dialogue: 0,0:15:14.32,0:15:17.31,Default,,0000,0000,0000,,have a rigidbody, so we should always find something Dialogue: 0,0:15:17.31,0:15:19.31,Default,,0000,0000,0000,,but it's good to check just in case because otherwise Dialogue: 0,0:15:19.31,0:15:21.31,Default,,0000,0000,0000,,you're going to get errors. Dialogue: 0,0:15:21.93,0:15:23.93,Default,,0000,0000,0000,,So now we know we've got a rigidbody what Dialogue: 0,0:15:23.93,0:15:26.35,Default,,0000,0000,0000,,we can do is we can add a force to that rigidbody Dialogue: 0,0:15:26.35,0:15:32.28,Default,,0000,0000,0000,,so targitRigidbody.AddExplosionForce ( Dialogue: 0,0:15:33.75,0:15:35.75,Default,,0000,0000,0000,,and the force we want to add is the Dialogue: 0,0:15:35.75,0:15:37.75,Default,,0000,0000,0000,,ExplosionForce that we've got as a public variable. Dialogue: 0,0:15:37.75,0:15:40.68,Default,,0000,0000,0000,,So that's m_ExplosionForce. Dialogue: 0,0:15:41.43,0:15:44.56,Default,,0000,0000,0000,,Then comma, so we're on to the next parameter. Dialogue: 0,0:15:44.56,0:15:46.56,Default,,0000,0000,0000,,And the position that that explosion Dialogue: 0,0:15:46.56,0:15:49.60,Default,,0000,0000,0000,,force is emanating from is transform.position. Dialogue: 0,0:15:50.38,0:15:52.38,Default,,0000,0000,0000,,So that's position of the shell. Dialogue: 0,0:15:53.39,0:15:55.39,Default,,0000,0000,0000,,And then the last parameter that we're going to use is Dialogue: 0,0:15:55.39,0:15:57.39,Default,,0000,0000,0000,,the radius, so how far away Dialogue: 0,0:15:57.39,0:16:00.41,Default,,0000,0000,0000,,that explosion force can reach. Dialogue: 0,0:16:05.84,0:16:09.23,Default,,0000,0000,0000,,Okay, so we've added the force now Dialogue: 0,0:16:09.23,0:16:11.23,Default,,0000,0000,0000,,so the tank is going to move but we still need Dialogue: 0,0:16:11.23,0:16:13.23,Default,,0000,0000,0000,,to actually cause some damage to it. Dialogue: 0,0:16:13.90,0:16:15.58,Default,,0000,0000,0000,,So we're going to do something very similar. Dialogue: 0,0:16:15.58,0:16:17.58,Default,,0000,0000,0000,,We're going to create a TankHealth variable, Dialogue: 0,0:16:18.52,0:16:20.52,Default,,0000,0000,0000,,which we'll call targetHealth. Dialogue: 0,0:16:22.67,0:16:24.67,Default,,0000,0000,0000,,So just to qualify that, notice that when we've Dialogue: 0,0:16:24.67,0:16:26.67,Default,,0000,0000,0000,,made these other variables they've been familiar Dialogue: 0,0:16:26.67,0:16:28.67,Default,,0000,0000,0000,,components to things that you've Dialogue: 0,0:16:28.67,0:16:30.67,Default,,0000,0000,0000,,added using the Add Component button. Dialogue: 0,0:16:31.18,0:16:33.18,Default,,0000,0000,0000,,This time we're making a variable Dialogue: 0,0:16:33.18,0:16:35.53,Default,,0000,0000,0000,,of type TankHealth. Dialogue: 0,0:16:35.53,0:16:38.61,Default,,0000,0000,0000,,So that just means an instance of the TankHealth script Dialogue: 0,0:16:38.61,0:16:41.61,Default,,0000,0000,0000,,on a tank, let's go back from the top. Dialogue: 0,0:16:41.61,0:16:43.18,Default,,0000,0000,0000,,So we've gathered all the colliders we could find Dialogue: 0,0:16:43.18,0:16:45.80,Default,,0000,0000,0000,,within the imaginary sphere, the overlap sphere. Dialogue: 0,0:16:46.37,0:16:48.14,Default,,0000,0000,0000,,And we're looping through Dialogue: 0,0:16:48.63,0:16:50.63,Default,,0000,0000,0000,,that list, that array, Dialogue: 0,0:16:50.63,0:16:53.49,Default,,0000,0000,0000,,to the amount that there is, colliders found. Dialogue: 0,0:16:53.49,0:16:55.49,Default,,0000,0000,0000,,And then with each one we're Dialogue: 0,0:16:55.49,0:16:57.49,Default,,0000,0000,0000,,checking that there's a rigidbody and if there is Dialogue: 0,0:16:57.49,0:16:59.27,Default,,0000,0000,0000,,we're adding a force, Dialogue: 0,0:16:59.27,0:17:01.27,Default,,0000,0000,0000,,and then we want to actually also Dialogue: 0,0:17:01.27,0:17:03.27,Default,,0000,0000,0000,,take damage on those tanks. Dialogue: 0,0:17:03.59,0:17:05.59,Default,,0000,0000,0000,,So we're addressing a particular Dialogue: 0,0:17:05.59,0:17:07.59,Default,,0000,0000,0000,,instance of that TankHealth script so we can Dialogue: 0,0:17:07.59,0:17:09.59,Default,,0000,0000,0000,,talk to it and reduce health with it. Dialogue: 0,0:17:10.79,0:17:13.91,Default,,0000,0000,0000,,Okay, so we've got TankHealth targetHealth Dialogue: 0,0:17:13.91,0:17:16.36,Default,,0000,0000,0000,,and we're going to set that equal to the targetRigidbody. Dialogue: 0,0:17:17.15,0:17:19.15,Default,,0000,0000,0000,,GetComponent TankHealth. Dialogue: 0,0:17:19.48,0:17:21.78,Default,,0000,0000,0000,,So we've got the rigidbody, we know that now Dialogue: 0,0:17:22.23,0:17:24.23,Default,,0000,0000,0000,,because we were able to add a force to it. Dialogue: 0,0:17:24.68,0:17:26.68,Default,,0000,0000,0000,,So on the same game object as that Dialogue: 0,0:17:27.26,0:17:28.83,Default,,0000,0000,0000,,get the component TankHealth. Dialogue: 0,0:17:28.83,0:17:31.79,Default,,0000,0000,0000,,And again we should know that Dialogue: 0,0:17:31.79,0:17:33.79,Default,,0000,0000,0000,,there's definitely going to be a TankHealth here Dialogue: 0,0:17:33.79,0:17:35.79,Default,,0000,0000,0000,,but we're going to check just to make sure. Dialogue: 0,0:17:35.79,0:17:37.79,Default,,0000,0000,0000,,Because if somebody wants to extend the game and they Dialogue: 0,0:17:37.79,0:17:41.11,Default,,0000,0000,0000,,make something that's on the Players layer that doesn't have TankHealth Dialogue: 0,0:17:41.11,0:17:43.11,Default,,0000,0000,0000,,then this will still work. Dialogue: 0,0:17:43.11,0:17:45.80,Default,,0000,0000,0000,,So again instead of if(!rigidbody) we've got Dialogue: 0,0:17:45.80,0:17:49.89,Default,,0000,0000,0000,,if(!targetHealth) and then continue. Dialogue: 0,0:17:49.89,0:17:51.89,Default,,0000,0000,0000,,So if there's no targetHealth continue Dialogue: 0,0:17:51.89,0:17:53.89,Default,,0000,0000,0000,,on to the next iteration. Dialogue: 0,0:17:54.29,0:17:56.29,Default,,0000,0000,0000,,So a quick note about this line here. Dialogue: 0,0:17:56.29,0:17:58.29,Default,,0000,0000,0000,,So you'll notice that we've talked about Dialogue: 0,0:17:58.29,0:18:00.29,Default,,0000,0000,0000,,the fact that we need a reference to this instance Dialogue: 0,0:18:00.29,0:18:02.29,Default,,0000,0000,0000,,of the TankHealth script. Dialogue: 0,0:18:02.29,0:18:04.29,Default,,0000,0000,0000,,So you might wondering 'well what's that got to do with Dialogue: 0,0:18:04.29,0:18:07.03,Default,,0000,0000,0000,,the rigidbody, what's health got to do with physics?' Dialogue: 0,0:18:07.03,0:18:09.03,Default,,0000,0000,0000,,Nothing at all, the point is that we've found Dialogue: 0,0:18:09.03,0:18:11.45,Default,,0000,0000,0000,,a particular game object Dialogue: 0,0:18:11.45,0:18:13.45,Default,,0000,0000,0000,,the fact that we're calling it targetRigidbody Dialogue: 0,0:18:13.45,0:18:15.45,Default,,0000,0000,0000,,is not hugely important, we'e just referring to Dialogue: 0,0:18:15.45,0:18:18.07,Default,,0000,0000,0000,,the same game object that we've stored in that. Dialogue: 0,0:18:18.07,0:18:20.07,Default,,0000,0000,0000,,Often when we talk about rigidbodies Dialogue: 0,0:18:20.07,0:18:22.97,Default,,0000,0000,0000,,we'll say 'this is a rigidbody object'. Dialogue: 0,0:18:22.97,0:18:26.16,Default,,0000,0000,0000,,All things in the hierarchy are what we call game objects. Dialogue: 0,0:18:26.16,0:18:28.16,Default,,0000,0000,0000,,But often what people will say is Dialogue: 0,0:18:28.16,0:18:29.15,Default,,0000,0000,0000,,'that's a rigidbody object'. Dialogue: 0,0:18:29.15,0:18:31.15,Default,,0000,0000,0000,,It just means it's a game object with that Dialogue: 0,0:18:31.15,0:18:32.83,Default,,0000,0000,0000,,particular component attached. Dialogue: 0,0:18:32.83,0:18:35.59,Default,,0000,0000,0000,,So when we're talking about the target rigidbody Dialogue: 0,0:18:35.59,0:18:37.59,Default,,0000,0000,0000,,we just mean it's the tank game object that Dialogue: 0,0:18:37.59,0:18:40.46,Default,,0000,0000,0000,,we know has a rigidbody attached to it. Dialogue: 0,0:18:40.46,0:18:42.46,Default,,0000,0000,0000,,So we've done that check and we can then Dialogue: 0,0:18:42.46,0:18:44.46,Default,,0000,0000,0000,,say we know it's the right game object so Dialogue: 0,0:18:44.46,0:18:46.46,Default,,0000,0000,0000,,we know it's got the TankHealth script on it Dialogue: 0,0:18:46.46,0:18:48.06,Default,,0000,0000,0000,,and we'll carry on from there. Dialogue: 0,0:18:48.65,0:18:51.45,Default,,0000,0000,0000,,Just to reiterate what Will was saying there, Dialogue: 0,0:18:51.45,0:18:53.45,Default,,0000,0000,0000,,you don't need a game object to find Dialogue: 0,0:18:53.45,0:18:55.45,Default,,0000,0000,0000,,a component on it, you can use Dialogue: 0,0:18:55.45,0:18:58.93,Default,,0000,0000,0000,,a component reference to find another component. Dialogue: 0,0:18:58.93,0:19:00.93,Default,,0000,0000,0000,,So that's what's actually happening there. Dialogue: 0,0:19:00.93,0:19:02.93,Default,,0000,0000,0000,,You've got the rigidbody component, we want to find another Dialogue: 0,0:19:02.93,0:19:04.93,Default,,0000,0000,0000,,component on the same game object, Dialogue: 0,0:19:04.93,0:19:06.93,Default,,0000,0000,0000,,that's what we're doing. Dialogue: 0,0:19:07.45,0:19:09.45,Default,,0000,0000,0000,,Okay so we know that we've got Dialogue: 0,0:19:09.45,0:19:11.45,Default,,0000,0000,0000,,targetHealth because we've not come out of the loop yet Dialogue: 0,0:19:11.45,0:19:13.45,Default,,0000,0000,0000,,so let's create the damage, Dialogue: 0,0:19:13.45,0:19:17.88,Default,,0000,0000,0000,,so float damage is equal to Dialogue: 0,0:19:17.88,0:19:19.88,Default,,0000,0000,0000,,this CalculateDamage function which you can see at Dialogue: 0,0:19:19.88,0:19:21.57,Default,,0000,0000,0000,,the bottom of the screen there, Dialogue: 0,0:19:21.57,0:19:22.84,Default,,0000,0000,0000,,which we will write in a minute, Dialogue: 0,0:19:22.84,0:19:24.84,Default,,0000,0000,0000,,but what we need to do is give it the Dialogue: 0,0:19:24.84,0:19:26.84,Default,,0000,0000,0000,,position of the target, Dialogue: 0,0:19:26.84,0:19:29.54,Default,,0000,0000,0000,,because we need to know how far away is that target Dialogue: 0,0:19:29.54,0:19:31.54,Default,,0000,0000,0000,,from the shell when it explodes Dialogue: 0,0:19:31.54,0:19:33.54,Default,,0000,0000,0000,,so we know how much damage it needs to take. Dialogue: 0,0:19:34.58,0:19:36.58,Default,,0000,0000,0000,,So you can actually get the Dialogue: 0,0:19:36.58,0:19:39.48,Default,,0000,0000,0000,,position of something from the rigidbody as well. Dialogue: 0,0:19:39.48,0:19:41.78,Default,,0000,0000,0000,,If you remember back from the TankMovement script Dialogue: 0,0:19:41.78,0:19:44.86,Default,,0000,0000,0000,,we were doing things relative to the rigidbody's position. Dialogue: 0,0:19:44.86,0:19:46.86,Default,,0000,0000,0000,,That's the position that we're going to use here. Dialogue: 0,0:19:48.18,0:19:50.18,Default,,0000,0000,0000,,So we've got the damage Dialogue: 0,0:19:50.18,0:19:52.18,Default,,0000,0000,0000,,now we need to apply the damage. Dialogue: 0,0:19:52.18,0:19:54.34,Default,,0000,0000,0000,,So we've got the TargetHealth script, Dialogue: 0,0:19:55.41,0:19:57.41,Default,,0000,0000,0000,,and we need to call the takeDamage function Dialogue: 0,0:19:57.41,0:19:59.41,Default,,0000,0000,0000,,that we made earlier. Dialogue: 0,0:20:01.41,0:20:04.28,Default,,0000,0000,0000,,And we're going to parse in the amount of damage. Dialogue: 0,0:20:08.12,0:20:10.53,Default,,0000,0000,0000,,So that's all that we need to do for that for loop, Dialogue: 0,0:20:10.53,0:20:12.53,Default,,0000,0000,0000,,that's all that we need to do for each of the Dialogue: 0,0:20:12.53,0:20:14.53,Default,,0000,0000,0000,,targets that we've got, but we still need to Dialogue: 0,0:20:14.53,0:20:16.53,Default,,0000,0000,0000,,make the shell explode Dialogue: 0,0:20:16.53,0:20:18.88,Default,,0000,0000,0000,,and play the correct audio and stuff. Dialogue: 0,0:20:19.58,0:20:21.58,Default,,0000,0000,0000,,Yeah, another thing to mention about this is Dialogue: 0,0:20:21.58,0:20:22.89,Default,,0000,0000,0000,,we're creating a float called Damage, Dialogue: 0,0:20:22.89,0:20:24.89,Default,,0000,0000,0000,,we've got this CalculateDamage, currently if we play this Dialogue: 0,0:20:24.89,0:20:26.89,Default,,0000,0000,0000,,nothing would happen because we don't Dialogue: 0,0:20:26.89,0:20:28.89,Default,,0000,0000,0000,,have anything going on in this CalculateDamage Dialogue: 0,0:20:28.89,0:20:30.36,Default,,0000,0000,0000,,function just yet. Dialogue: 0,0:20:30.36,0:20:32.100,Default,,0000,0000,0000,,But all we're doing is just setting it up so when we do Dialogue: 0,0:20:32.100,0:20:35.78,Default,,0000,0000,0000,,we're going to parse that straight to the TakeDamage function. Dialogue: 0,0:20:35.78,0:20:38.51,Default,,0000,0000,0000,,If you remember when we looked at that TankHealth earlier Dialogue: 0,0:20:39.23,0:20:41.23,Default,,0000,0000,0000,,our TakeDamage function there is public, Dialogue: 0,0:20:41.23,0:20:43.23,Default,,0000,0000,0000,,that's how we're able to just reference it by saying Dialogue: 0,0:20:43.23,0:20:45.23,Default,,0000,0000,0000,,.TakeDamage. Dialogue: 0,0:20:46.92,0:20:50.08,Default,,0000,0000,0000,,Public being the accessibility level of that thing. Dialogue: 0,0:20:51.30,0:20:53.30,Default,,0000,0000,0000,,Okay, we've gone through all the colliders Dialogue: 0,0:20:53.30,0:20:55.30,Default,,0000,0000,0000,,that we need to do, so now we need to Dialogue: 0,0:20:55.30,0:20:58.99,Default,,0000,0000,0000,,sort out the particles and the audio. Dialogue: 0,0:20:58.99,0:21:03.24,Default,,0000,0000,0000,,So when we detonate the shell, when it explodes Dialogue: 0,0:21:03.24,0:21:04.74,Default,,0000,0000,0000,,we want to remove it from the scene, Dialogue: 0,0:21:04.74,0:21:06.74,Default,,0000,0000,0000,,we don't want it to hang around. Dialogue: 0,0:21:06.74,0:21:08.74,Default,,0000,0000,0000,,However if we remove it then the particles Dialogue: 0,0:21:08.74,0:21:10.74,Default,,0000,0000,0000,,are also going to be removed because they're children Dialogue: 0,0:21:11.36,0:21:12.64,Default,,0000,0000,0000,,and the audio source is also going to be removed Dialogue: 0,0:21:12.64,0:21:13.99,Default,,0000,0000,0000,,because that's a child. Dialogue: 0,0:21:14.72,0:21:16.72,Default,,0000,0000,0000,,So we want to make sure that those Dialogue: 0,0:21:16.72,0:21:19.79,Default,,0000,0000,0000,,still play even after we've destroyed the shell. Dialogue: 0,0:21:20.55,0:21:22.55,Default,,0000,0000,0000,,So what we're going to do to do that Dialogue: 0,0:21:22.55,0:21:23.88,Default,,0000,0000,0000,,is to unparent them. Dialogue: 0,0:21:24.68,0:21:27.65,Default,,0000,0000,0000,,So it will come in as a complete game object Dialogue: 0,0:21:27.65,0:21:30.67,Default,,0000,0000,0000,,it'll explode and they will decouple themselves Dialogue: 0,0:21:30.67,0:21:32.67,Default,,0000,0000,0000,,so the shell explosion will no longer Dialogue: 0,0:21:32.67,0:21:34.67,Default,,0000,0000,0000,,be part of the shell. Dialogue: 0,0:21:35.18,0:21:37.18,Default,,0000,0000,0000,,And we're going to do that by setting the Dialogue: 0,0:21:37.18,0:21:39.18,Default,,0000,0000,0000,,transform's parent to null. Dialogue: 0,0:21:39.18,0:21:41.18,Default,,0000,0000,0000,,So null just means nothing. Dialogue: 0,0:21:41.55,0:21:43.55,Default,,0000,0000,0000,,That means it would just be loose in the list Dialogue: 0,0:21:43.55,0:21:44.55,Default,,0000,0000,0000,,of game objects in the hierarchy. Dialogue: 0,0:21:44.55,0:21:46.55,Default,,0000,0000,0000,,So we have detached it now, Dialogue: 0,0:21:46.55,0:21:49.18,Default,,0000,0000,0000,,so next we need to play the Dialogue: 0,0:21:49.18,0:21:55.86,Default,,0000,0000,0000,,particle effect, so m_ExplosionParticles.Play (); Dialogue: 0,0:21:57.76,0:21:59.76,Default,,0000,0000,0000,,And likewise we want to play the audio Dialogue: 0,0:21:59.76,0:22:01.76,Default,,0000,0000,0000,,source so very similarly Dialogue: 0,0:22:02.66,0:22:05.07,Default,,0000,0000,0000,,m_ExplosionAudio.Play (); Dialogue: 0,0:22:06.91,0:22:08.91,Default,,0000,0000,0000,,So then we've just got a couple of more things Dialogue: 0,0:22:08.91,0:22:10.70,Default,,0000,0000,0000,,we've got to do in this function. Dialogue: 0,0:22:10.70,0:22:12.70,Default,,0000,0000,0000,,We need to destroy the particles and we Dialogue: 0,0:22:12.70,0:22:14.70,Default,,0000,0000,0000,,need to destroy the shell. Dialogue: 0,0:22:14.70,0:22:16.70,Default,,0000,0000,0000,,So to destroy the particles we're going to use Dialogue: 0,0:22:16.70,0:22:18.70,Default,,0000,0000,0000,,the same trick as we did in the Start function, Dialogue: 0,0:22:18.70,0:22:20.70,Default,,0000,0000,0000,,we're going to give it a delay. Dialogue: 0,0:22:21.23,0:22:27.29,Default,,0000,0000,0000,,So we're going to say Destroy(m_ExplosionParticles.gameObject Dialogue: 0,0:22:27.29,0:22:29.29,Default,,0000,0000,0000,,We don't want to destroy the particles because Dialogue: 0,0:22:29.29,0:22:30.60,Default,,0000,0000,0000,,that will destroy the component. Dialogue: 0,0:22:30.60,0:22:33.56,Default,,0000,0000,0000,,We want to destroy the game object that those particles are on. Dialogue: 0,0:22:34.74,0:22:36.74,Default,,0000,0000,0000,,And we're going to do that after Dialogue: 0,0:22:36.74,0:22:38.74,Default,,0000,0000,0000,,the duration of the particle system. Dialogue: 0,0:22:39.43,0:22:41.43,Default,,0000,0000,0000,,So you remember before we said that it's going to be Dialogue: 0,0:22:41.43,0:22:43.43,Default,,0000,0000,0000,,2 seconds, which was a number that decided on. Dialogue: 0,0:22:44.11,0:22:46.11,Default,,0000,0000,0000,,Well particle systems, one of their properties Dialogue: 0,0:22:46.11,0:22:47.77,Default,,0000,0000,0000,,is how long they last for. Dialogue: 0,0:22:47.77,0:22:49.77,Default,,0000,0000,0000,,So we're just going to say destroy it Dialogue: 0,0:22:50.39,0:22:52.39,Default,,0000,0000,0000,,after how long it lasts for. Dialogue: 0,0:22:52.39,0:22:54.39,Default,,0000,0000,0000,,Yeah, so not to be confused with the lifetime of Dialogue: 0,0:22:54.39,0:22:57.62,Default,,0000,0000,0000,,each particle, but the duration is the entire system. Dialogue: 0,0:22:57.62,0:23:00.50,Default,,0000,0000,0000,,So this, as you can see, autocomplete is showing us here Dialogue: 0,0:23:00.78,0:23:02.56,Default,,0000,0000,0000,,Optional amount of time before destroying the object. Dialogue: 0,0:23:02.56,0:23:04.67,Default,,0000,0000,0000,,We can simply just refer to Dialogue: 0,0:23:04.67,0:23:06.67,Default,,0000,0000,0000,,ExplosionParticles.duration Dialogue: 0,0:23:07.60,0:23:09.60,Default,,0000,0000,0000,,and that will simply wait for that to finish and then destroy it. Dialogue: 0,0:23:09.60,0:23:11.25,Default,,0000,0000,0000,,based on that. Dialogue: 0,0:23:11.25,0:23:13.25,Default,,0000,0000,0000,,Which is great, because you can then go and Dialogue: 0,0:23:13.25,0:23:15.25,Default,,0000,0000,0000,,adjust the duration and this code will still take Dialogue: 0,0:23:15.25,0:23:16.82,Default,,0000,0000,0000,,care of it for you. Dialogue: 0,0:23:17.45,0:23:19.45,Default,,0000,0000,0000,,And as James was saying, the last thing we need to do Dialogue: 0,0:23:19.45,0:23:21.45,Default,,0000,0000,0000,,once we'd destroyed the explosion, or once Dialogue: 0,0:23:21.45,0:23:23.45,Default,,0000,0000,0000,,we've triggered a destroy to occur once Dialogue: 0,0:23:23.45,0:23:25.20,Default,,0000,0000,0000,,it's finished playing the explosion Dialogue: 0,0:23:25.20,0:23:27.20,Default,,0000,0000,0000,,is actually to remove the shell as well. Dialogue: 0,0:23:27.72,0:23:29.72,Default,,0000,0000,0000,,So to do that all we do is Dialogue: 0,0:23:29.72,0:23:32.24,Default,,0000,0000,0000,,Destroy (gameObject) and that will destroy the Dialogue: 0,0:23:32.24,0:23:34.24,Default,,0000,0000,0000,,game object that this is on. Dialogue: 0,0:23:34.24,0:23:36.24,Default,,0000,0000,0000,,As ever with scripting everything is just Dialogue: 0,0:23:36.24,0:23:38.24,Default,,0000,0000,0000,,happening straight away, it's not like we're Dialogue: 0,0:23:38.24,0:23:40.24,Default,,0000,0000,0000,,waiting for one thing to happen and then something Dialogue: 0,0:23:40.24,0:23:42.98,Default,,0000,0000,0000,,else to happens, so theoretically you might Dialogue: 0,0:23:42.98,0:23:45.61,Default,,0000,0000,0000,,consider that you would get rid of the shell and then Dialogue: 0,0:23:45.61,0:23:48.25,Default,,0000,0000,0000,,the explosion but it really doesn't matter in this instance. Dialogue: 0,0:23:49.46,0:23:52.28,Default,,0000,0000,0000,,Okay, so, the last thing that we need Dialogue: 0,0:23:52.28,0:23:54.28,Default,,0000,0000,0000,,to do in this script is complete that Dialogue: 0,0:23:54.28,0:23:56.28,Default,,0000,0000,0000,,CalculateDamage function. Dialogue: 0,0:23:57.58,0:23:59.18,Default,,0000,0000,0000,,So in this CalculateDamage function Dialogue: 0,0:23:59.18,0:24:01.97,Default,,0000,0000,0000,,currently we've just got return 0f and that's Dialogue: 0,0:24:01.97,0:24:03.97,Default,,0000,0000,0000,,purely just so it doesn't spew errors Dialogue: 0,0:24:03.97,0:24:05.97,Default,,0000,0000,0000,,while we're working on the rest of the stuff. Dialogue: 0,0:24:06.51,0:24:09.28,Default,,0000,0000,0000,,So we're going to get rid of that to start off with, we don't need it. Dialogue: 0,0:24:10.34,0:24:12.34,Default,,0000,0000,0000,,And the next thing we're going to do is Dialogue: 0,0:24:12.34,0:24:15.55,Default,,0000,0000,0000,,create a vector from the target to the shell. Dialogue: 0,0:24:15.94,0:24:17.84,Default,,0000,0000,0000,,So that is going to be Dialogue: 0,0:24:17.84,0:24:19.84,Default,,0000,0000,0000,,Vector3 explosionToTarget. Dialogue: 0,0:24:27.06,0:24:29.06,Default,,0000,0000,0000,,and that is equal to Dialogue: 0,0:24:29.06,0:24:32.69,Default,,0000,0000,0000,,the target's position, minus the shell's position, Dialogue: 0,0:24:32.69,0:24:34.69,Default,,0000,0000,0000,,which is transform.position. Dialogue: 0,0:24:37.81,0:24:39.41,Default,,0000,0000,0000,,So if you imagine a big circle on the ground, Dialogue: 0,0:24:39.41,0:24:42.56,Default,,0000,0000,0000,,that's the explosion, at the centre of it is the shell, Dialogue: 0,0:24:42.56,0:24:45.05,Default,,0000,0000,0000,,and somewhere in that circle is the target. Dialogue: 0,0:24:45.70,0:24:47.70,Default,,0000,0000,0000,,We're finding a vector between the two. Dialogue: 0,0:24:49.96,0:24:51.96,Default,,0000,0000,0000,,The next thing we need to do is find out Dialogue: 0,0:24:51.96,0:24:53.64,Default,,0000,0000,0000,,how long that vector is, Dialogue: 0,0:24:53.64,0:24:55.21,Default,,0000,0000,0000,,the magnitude of that vector. Dialogue: 0,0:24:55.21,0:24:59.76,Default,,0000,0000,0000,,So what we're going to do is say float explosionDistance Dialogue: 0,0:25:00.64,0:25:05.66,Default,,0000,0000,0000,,is equal to explosionToTarget.magnitude. Dialogue: 0,0:25:11.92,0:25:13.92,Default,,0000,0000,0000,,Okay, so now we've found that distance, Dialogue: 0,0:25:13.92,0:25:17.88,Default,,0000,0000,0000,,which would be somewhere between 0 and the radius. Dialogue: 0,0:25:18.73,0:25:22.01,Default,,0000,0000,0000,,We want to know what the relative distance is. Dialogue: 0,0:25:22.01,0:25:25.26,Default,,0000,0000,0000,,Because we want a number from 0 to 1 really, Dialogue: 0,0:25:25.94,0:25:29.70,Default,,0000,0000,0000,,rather than a number between 0 and some radius that we found. Dialogue: 0,0:25:29.70,0:25:31.70,Default,,0000,0000,0000,,So the relative distance Dialogue: 0,0:25:31.70,0:25:33.70,Default,,0000,0000,0000,,we know the maximum it's going to be is Dialogue: 0,0:25:33.70,0:25:36.69,Default,,0000,0000,0000,,the radius and the minimum is going to be 0. Dialogue: 0,0:25:37.42,0:25:39.68,Default,,0000,0000,0000,,So what we need to do is calculate a number that Dialogue: 0,0:25:39.68,0:25:42.38,Default,,0000,0000,0000,,starts off at 0 and ends up at 1. Dialogue: 0,0:25:42.38,0:25:45.98,Default,,0000,0000,0000,,We do that by dividing by the explosion radius. Dialogue: 0,0:25:45.98,0:25:50.37,Default,,0000,0000,0000,,So relative distance is Dialogue: 0,0:25:50.37,0:25:54.75,Default,,0000,0000,0000,,(m_ExplosionRadius - explosionDistance) Dialogue: 0,0:25:55.62,0:25:57.62,Default,,0000,0000,0000,,then close the parenthesis Dialogue: 0,0:25:57.62,0:26:00.36,Default,,0000,0000,0000,,and divide all of that by the ExplosionRadius. Dialogue: 0,0:26:02.46,0:26:04.46,Default,,0000,0000,0000,,So when you think about that Dialogue: 0,0:26:05.09,0:26:08.24,Default,,0000,0000,0000,,if the explosion distance is very long, Dialogue: 0,0:26:08.24,0:26:10.58,Default,,0000,0000,0000,,IE it's close to the explosion radius Dialogue: 0,0:26:12.10,0:26:15.43,Default,,0000,0000,0000,,then the explosion radius minus something that's Dialogue: 0,0:26:15.43,0:26:17.43,Default,,0000,0000,0000,,very close to the explosion radius is going to be Dialogue: 0,0:26:17.43,0:26:20.75,Default,,0000,0000,0000,,very very small, divided by the explosion radius is going Dialogue: 0,0:26:20.75,0:26:22.75,Default,,0000,0000,0000,,to be very very small so it'll be near zero. Dialogue: 0,0:26:24.48,0:26:27.35,Default,,0000,0000,0000,,However, if the explosion distance is very small Dialogue: 0,0:26:27.35,0:26:29.35,Default,,0000,0000,0000,,then the explosion radius minus Dialogue: 0,0:26:29.35,0:26:31.82,Default,,0000,0000,0000,,something very small, let's say zero Dialogue: 0,0:26:31.82,0:26:33.82,Default,,0000,0000,0000,,divided by the explosion radius is going to be 1. Dialogue: 0,0:26:35.00,0:26:37.00,Default,,0000,0000,0000,,So we're going to end up with a number which Dialogue: 0,0:26:37.00,0:26:39.00,Default,,0000,0000,0000,,starts off at 1 near the centre and Dialogue: 0,0:26:39.00,0:26:41.00,Default,,0000,0000,0000,,trails down to 0 at the edge. Dialogue: 0,0:26:42.46,0:26:44.12,Default,,0000,0000,0000,,And that's how we're going to calculate how Dialogue: 0,0:26:44.12,0:26:45.54,Default,,0000,0000,0000,,much damage is done. Dialogue: 0,0:26:45.54,0:26:47.54,Default,,0000,0000,0000,,So we create a float called damage. Dialogue: 0,0:26:47.54,0:26:50.10,Default,,0000,0000,0000,,And that's equal to the relative distance Dialogue: 0,0:26:50.62,0:26:53.05,Default,,0000,0000,0000,,multiplied by the MaxDamage Dialogue: 0,0:26:53.05,0:26:55.05,Default,,0000,0000,0000,,m_MaxDamage. Dialogue: 0,0:26:59.75,0:27:02.32,Default,,0000,0000,0000,,So when the relative distance is 1 Dialogue: 0,0:27:02.32,0:27:04.32,Default,,0000,0000,0000,,and is very close to the centre, Dialogue: 0,0:27:04.32,0:27:06.32,Default,,0000,0000,0000,,then you're going to deal the maximum damage. Dialogue: 0,0:27:06.32,0:27:08.32,Default,,0000,0000,0000,,When it's close to the edge, it's close to 0 you're going Dialogue: 0,0:27:08.32,0:27:09.82,Default,,0000,0000,0000,,to do very little damage. Dialogue: 0,0:27:10.58,0:27:14.12,Default,,0000,0000,0000,,Okay, so as illustrated in the slide Dialogue: 0,0:27:14.12,0:27:16.12,Default,,0000,0000,0000,,we have our overlap sphere, Dialogue: 0,0:27:16.12,0:27:18.12,Default,,0000,0000,0000,,we have our tank right at the very edge of it, Dialogue: 0,0:27:18.12,0:27:20.12,Default,,0000,0000,0000,,very minimal or maybe 0 damage, Dialogue: 0,0:27:20.12,0:27:23.51,Default,,0000,0000,0000,,and then something right in the middle of that impact Dialogue: 0,0:27:23.51,0:27:25.51,Default,,0000,0000,0000,,such damage, many ow. Dialogue: 0,0:27:25.80,0:27:28.00,Default,,0000,0000,0000,,We're not finished yet with this function. Dialogue: 0,0:27:28.73,0:27:30.73,Default,,0000,0000,0000,,What we need to do next is make sure Dialogue: 0,0:27:30.73,0:27:33.56,Default,,0000,0000,0000,,that the damage is not negative. Dialogue: 0,0:27:33.56,0:27:35.56,Default,,0000,0000,0000,,So we're going to do that by saying damage is Dialogue: 0,0:27:35.56,0:27:44.80,Default,,0000,0000,0000,,equal to Mathf.max of 0f and Damage. Dialogue: 0,0:27:45.94,0:27:47.94,Default,,0000,0000,0000,,So what that's doing is saying if it's negative Dialogue: 0,0:27:47.94,0:27:50.97,Default,,0000,0000,0000,,then set it to 0, otherwise keep it as it is. Dialogue: 0,0:27:51.73,0:27:53.73,Default,,0000,0000,0000,,And then finally we can return that damage. Dialogue: 0,0:27:55.62,0:27:57.28,Default,,0000,0000,0000,,Why is it that we're Dialogue: 0,0:27:57.28,0:27:59.28,Default,,0000,0000,0000,,making sure it's not negative? Dialogue: 0,0:27:59.28,0:28:01.28,Default,,0000,0000,0000,,Well there is literally an edge Dialogue: 0,0:28:01.28,0:28:03.28,Default,,0000,0000,0000,,case in this scenario. Dialogue: 0,0:28:08.17,0:28:11.35,Default,,0000,0000,0000,,In the case that the tank is Dialogue: 0,0:28:11.35,0:28:13.35,Default,,0000,0000,0000,,right on the very edge it's position Dialogue: 0,0:28:13.35,0:28:15.35,Default,,0000,0000,0000,,is actually outside of Dialogue: 0,0:28:15.35,0:28:18.64,Default,,0000,0000,0000,,the overlap sphere but it's collider is within it. Dialogue: 0,0:28:18.64,0:28:21.36,Default,,0000,0000,0000,,So it's going to get picked up by that collider's array Dialogue: 0,0:28:21.96,0:28:24.92,Default,,0000,0000,0000,,and then it's going to be calculated in with everything else Dialogue: 0,0:28:24.92,0:28:26.92,Default,,0000,0000,0000,,but we've only caught the edge of it, so, Dialogue: 0,0:28:27.74,0:28:29.28,Default,,0000,0000,0000,,we're actually going to calculate a relative distance Dialogue: 0,0:28:29.28,0:28:33.18,Default,,0000,0000,0000,,that's negative, because you're starting off at 1 in the centre Dialogue: 0,0:28:33.18,0:28:35.18,Default,,0000,0000,0000,,going down to 0 at the outside Dialogue: 0,0:28:35.18,0:28:37.18,Default,,0000,0000,0000,,and then it'll be negative beyond that. Dialogue: 0,0:28:37.55,0:28:39.07,Default,,0000,0000,0000,,So we don't want negative damage, Dialogue: 0,0:28:39.07,0:28:40.56,Default,,0000,0000,0000,,we don't want to shoot things and make them more healthy, Dialogue: 0,0:28:40.56,0:28:42.56,Default,,0000,0000,0000,,it's just not the way. Dialogue: 0,0:28:43.52,0:28:45.52,Default,,0000,0000,0000,,So that's why we're doing that. Dialogue: 0,0:28:45.52,0:28:47.52,Default,,0000,0000,0000,,Shall we put the script back on and Dialogue: 0,0:28:47.52,0:28:49.52,Default,,0000,0000,0000,,finish it off? Dialogue: 0,0:28:52.10,0:28:54.10,Default,,0000,0000,0000,,Okay, so if you've saved your script switch Dialogue: 0,0:28:54.10,0:28:57.27,Default,,0000,0000,0000,,back to the editor, hopefully there won't be any errors. Dialogue: 0,0:28:59.27,0:29:01.27,Default,,0000,0000,0000,,And then we need to of course populate Dialogue: 0,0:29:01.27,0:29:03.63,Default,,0000,0000,0000,,our script with all those public variables. Dialogue: 0,0:29:04.08,0:29:06.08,Default,,0000,0000,0000,,So interestingly something you won't have seen before, Dialogue: 0,0:29:08.10,0:29:10.10,Default,,0000,0000,0000,,is the first public variable, it's this Dialogue: 0,0:29:10.10,0:29:12.10,Default,,0000,0000,0000,,Tank Mask, so remember it's of type Dialogue: 0,0:29:12.10,0:29:14.46,Default,,0000,0000,0000,,layer mask and we called it Tank Mask. Dialogue: 0,0:29:14.46,0:29:16.46,Default,,0000,0000,0000,,And what that basically gives you is a repeat of Dialogue: 0,0:29:16.46,0:29:18.46,Default,,0000,0000,0000,,all of Unity's layer system. Dialogue: 0,0:29:19.23,0:29:21.23,Default,,0000,0000,0000,,So you'll notice it's the same list that you get in the Dialogue: 0,0:29:21.23,0:29:23.23,Default,,0000,0000,0000,,Layer drop down at the top. Dialogue: 0,0:29:23.23,0:29:25.23,Default,,0000,0000,0000,,This Tank Mask Dialogue: 0,0:29:25.23,0:29:27.57,Default,,0000,0000,0000,,is where we're going to select Players. Dialogue: 0,0:29:28.62,0:29:30.96,Default,,0000,0000,0000,,It's the only thing we want it to actually effect. Dialogue: 0,0:29:30.96,0:29:32.96,Default,,0000,0000,0000,,so select the Players layer from the Dialogue: 0,0:29:32.96,0:29:35.87,Default,,0000,0000,0000,,Tank Mask variable or drop down. Dialogue: 0,0:29:37.96,0:29:41.22,Default,,0000,0000,0000,,Then Explosion Particles is the child object Dialogue: 0,0:29:41.22,0:29:43.22,Default,,0000,0000,0000,,that's underneath the shell. Dialogue: 0,0:29:44.27,0:29:47.24,Default,,0000,0000,0000,,So you can drag a shell explosion on to Dialogue: 0,0:29:47.24,0:29:49.24,Default,,0000,0000,0000,,Explosion Particles, you'll notice in the Dialogue: 0,0:29:49.24,0:29:51.79,Default,,0000,0000,0000,,parenthesis it says Particle Systems. Dialogue: 0,0:29:52.18,0:29:54.29,Default,,0000,0000,0000,,And for Explosion Audio it's the same Dialogue: 0,0:29:54.29,0:29:57.47,Default,,0000,0000,0000,,object because that also has an audio source. Dialogue: 0,0:29:57.47,0:30:00.60,Default,,0000,0000,0000,,But this time when you drag it you'll notice it picks that component. Dialogue: 0,0:30:01.01,0:30:03.01,Default,,0000,0000,0000,,So Unity is smart that way, it knows which component Dialogue: 0,0:30:03.01,0:30:05.01,Default,,0000,0000,0000,,you're referring to if you drag an object that's got that Dialogue: 0,0:30:05.01,0:30:08.06,Default,,0000,0000,0000,,component it'll say 'great, okay, that must be what you mean'. Dialogue: 0,0:30:08.06,0:30:10.06,Default,,0000,0000,0000,,And assign that particular component Dialogue: 0,0:30:10.06,0:30:12.06,Default,,0000,0000,0000,,from the game object that you drag in. Dialogue: 0,0:30:12.66,0:30:14.66,Default,,0000,0000,0000,,Then the other stuff that we've setup there, Dialogue: 0,0:30:15.04,0:30:17.04,Default,,0000,0000,0000,,you know, it's setup based on Dialogue: 0,0:30:17.04,0:30:19.04,Default,,0000,0000,0000,,something that we wanted to make, if we get to the Dialogue: 0,0:30:19.04,0:30:21.04,Default,,0000,0000,0000,,end of the day and you want to have a huge Dialogue: 0,0:30:21.04,0:30:23.04,Default,,0000,0000,0000,,amount of damage, you want to have one shot kills Dialogue: 0,0:30:23.04,0:30:25.04,Default,,0000,0000,0000,,and you just up the maximum damage Dialogue: 0,0:30:25.43,0:30:27.43,Default,,0000,0000,0000,,or add the explosion force and the tank Dialogue: 0,0:30:27.43,0:30:29.43,Default,,0000,0000,0000,,just gets flung all the way off the screen. Dialogue: 0,0:30:29.43,0:30:31.43,Default,,0000,0000,0000,,You can do that stuff. Dialogue: 0,0:30:31.43,0:30:33.43,Default,,0000,0000,0000,,Obviously up the explosion radius as well. Dialogue: 0,0:30:34.17,0:30:35.52,Default,,0000,0000,0000,,You can play around with that. Dialogue: 0,0:30:35.52,0:30:37.52,Default,,0000,0000,0000,,For now let's stick with these values Dialogue: 0,0:30:37.52,0:30:39.52,Default,,0000,0000,0000,,and see how we like that. Dialogue: 0,0:30:39.52,0:30:41.52,Default,,0000,0000,0000,,And then the last thing we're going to do is to save the Dialogue: 0,0:30:41.52,0:30:43.52,Default,,0000,0000,0000,,shell as a prefab, as I said before we don't want Dialogue: 0,0:30:43.52,0:30:46.30,Default,,0000,0000,0000,,it to be in the game when it begins. Dialogue: 0,0:30:46.30,0:30:48.30,Default,,0000,0000,0000,,So I'm going to select my Prefabs folder. Dialogue: 0,0:30:48.85,0:30:50.85,Default,,0000,0000,0000,,I'm going to drag my shell and drop it in to Dialogue: 0,0:30:50.85,0:30:52.85,Default,,0000,0000,0000,,the empty space in that folder. Dialogue: 0,0:30:54.95,0:30:56.95,Default,,0000,0000,0000,,So really we should check if this works, so I'm just going Dialogue: 0,0:30:56.95,0:30:58.95,Default,,0000,0000,0000,,to briefly save my scene and demonstrate Dialogue: 0,0:30:58.95,0:31:00.95,Default,,0000,0000,0000,,what you should be seeing. Dialogue: 0,0:31:00.95,0:31:02.95,Default,,0000,0000,0000,,So if I press Play right now Dialogue: 0,0:31:04.41,0:31:06.41,Default,,0000,0000,0000,,my shell should fall down Dialogue: 0,0:31:06.41,0:31:08.41,Default,,0000,0000,0000,,and because it's pretty near the tank Dialogue: 0,0:31:08.41,0:31:10.41,Default,,0000,0000,0000,,take off quite a lot of damage. Dialogue: 0,0:31:10.72,0:31:13.32,Default,,0000,0000,0000,,Take off a lot of health, sorry, apply a lot of damage. Dialogue: 0,0:31:14.44,0:31:16.28,Default,,0000,0000,0000,,We've applied the shell explosion, Dialogue: 0,0:31:16.28,0:31:19.15,Default,,0000,0000,0000,,in order to populate the particle effect Dialogue: 0,0:31:19.15,0:31:22.25,Default,,0000,0000,0000,,and the audio source on to those two different variables. Dialogue: 0,0:31:22.25,0:31:24.25,Default,,0000,0000,0000,,And then we've made it in to a prefab Dialogue: 0,0:31:24.25,0:31:26.25,Default,,0000,0000,0000,,so now, as mentioned before, Dialogue: 0,0:31:26.25,0:31:28.25,Default,,0000,0000,0000,,we don't need that shell in the game. Dialogue: 0,0:31:28.74,0:31:30.74,Default,,0000,0000,0000,,Delete it out of your scene. Dialogue: 0,0:31:30.74,0:31:33.63,Default,,0000,0000,0000,,Delete on PC, command-backspace on Mac Dialogue: 0,0:31:33.63,0:31:35.90,Default,,0000,0000,0000,,and then save your scene. Dialogue: 0,0:31:35.90,0:31:37.90,Default,,0000,0000,0000,,One more time, select the shell. Dialogue: 0,0:31:38.55,0:31:40.55,Default,,0000,0000,0000,,delete it and then save your scene. Dialogue: 0,0:31:40.55,0:31:42.76,Default,,0000,0000,0000,,Okay, so that's the end of phase 5. Dialogue: 0,0:31:44.08,0:31:46.08,Default,,0000,0000,0000,,Then the next phase we're going to deal with Dialogue: 0,0:31:46.08,0:31:48.69,Default,,0000,0000,0000,,actually shooting these projectiles. Dialogue: 0,0:31:48.69,0:31:50.98,Default,,0000,0000,0000,,A little bit of physics and a bit more UI.