WEBVTT

00:03.380 --> 00:08.990
Welcome back to part two of this tutorial on acceleration.

00:08.990 --> 00:18.080
What we're going to do now is prepare the shell for destroying itself as well as hitting things and

00:18.080 --> 00:19.340
being able to move.

00:19.340 --> 00:25.640
So we're going to start by selecting the shell prefab that we've been using all along and we will come

00:25.640 --> 00:28.310
over into the inspector.

00:28.310 --> 00:32.210
We have the Move Shell script attached to it.

00:32.210 --> 00:35.090
We're actually going to turn that off and not use it.

00:35.090 --> 00:38.240
We're also going to add to it a collider.

00:38.240 --> 00:47.300
So we want a capsule collider to sit around the shell and we also want to add a few scripts that we've

00:47.300 --> 00:49.310
already got in our starter package.

00:49.310 --> 00:54.200
So if you go into scripts, I've moved the ones I was working on before, which was Fire Shell and Move

00:54.200 --> 00:59.720
Shell in to here, you'll find that there's a shell script and a destroy shell script.

00:59.870 --> 01:07.130
Grab the shell script and the destroy shell script and put them on to your shell.

01:07.130 --> 01:11.480
So let me just make the shell in the world there.

01:11.480 --> 01:15.230
And if we just zoom in on it, we can see where our capsule collider went.

01:15.260 --> 01:17.240
So it's not quite fitting.

01:17.240 --> 01:20.360
You might want to just rejig it a little bit.

01:20.450 --> 01:25.460
So it probably needs to just work with the radius.

01:25.940 --> 01:29.660
I think it needs a direction of the Z axis, so it's the other way.

01:29.660 --> 01:31.880
And then we can work with the.

01:33.340 --> 01:37.270
Radius and height to get it to fit.

01:38.140 --> 01:47.450
So there's the height and bring that radius down and then bring the height down just a little bit.

01:47.470 --> 01:49.510
I mean, it doesn't have to be that accurate.

01:49.520 --> 01:55.730
Then you might want to move it in its Z direction back along the shell a little bit.

01:55.750 --> 01:56.110
Okay.

01:56.110 --> 02:01.930
So we've got that now with that shell there in the hierarchy, we can open up our scripts and drag and

02:01.930 --> 02:10.630
drop the shell script onto the shell and then also the destroy shell script onto the shell.

02:11.140 --> 02:15.460
Now select that shell and find the shell script.

02:15.470 --> 02:18.820
You'll notice that there's an explosion just here.

02:19.240 --> 02:25.090
We're going to select the explosion in the Prefabs folder, which is called Shell Explosion.

02:25.120 --> 02:30.580
Drag and drop that on to that explosion script.

02:30.790 --> 02:35.110
Now that we've finished with the shell, we're actually going to go to the overrides up in the inspector

02:35.110 --> 02:39.180
to make sure that we save everything back to the prefab.

02:39.190 --> 02:42.970
Now you can't see it because it's off the edge of my screen, but there's an apply all button at the

02:42.970 --> 02:48.740
end which will write that back to the prefab and we can actually delete the shell.

02:48.760 --> 02:51.190
Now let's have a look in Destroy Shell.

02:51.190 --> 02:52.050
Let's open that up.

02:52.060 --> 02:56.650
What it's simply doing is destroying your shell after 3 seconds.

02:56.920 --> 03:00.850
So if your shell doesn't hit anything, this will get rid of it.

03:00.850 --> 03:02.020
That's all it's doing.

03:02.140 --> 03:07.950
Now, if we go back into our shell code, this is the one we're actually going to start working within.

03:07.960 --> 03:10.000
So we've got our explosion coming through.

03:10.000 --> 03:16.210
We have an on collision with an object called Tank, which we need to check if our tanks are actually

03:16.210 --> 03:17.230
tagged with that.

03:17.230 --> 03:23.020
And then we have a start and a light update where we're working mostly in this late update.

03:23.020 --> 03:29.530
So before we start, as I said, we should actually put a tank as a tank.

03:29.530 --> 03:34.150
So we're going to come in, select our green tank, which is this guy here.

03:34.150 --> 03:40.930
Now, it's got to be the object that has the collider on it, all the objects that have the collider

03:40.930 --> 03:41.300
on it.

03:41.320 --> 03:48.460
Now, in this case, none of these objects have a collider on it, and therefore we need to add a collider.

03:48.460 --> 03:52.600
So just add in here a simple box collider.

03:52.600 --> 03:55.000
It doesn't really matter too much.

03:55.000 --> 03:56.830
It doesn't have to be too accurate.

03:56.860 --> 04:01.300
Then we'll just have a look at our tank and see if we can see where that went to.

04:01.480 --> 04:04.060
We might just have to rejig it a bit.

04:04.060 --> 04:05.290
Move it up.

04:06.560 --> 04:16.010
And just make it a bit bigger in the Z direction and the X and the Y.

04:16.880 --> 04:17.490
Okay.

04:17.510 --> 04:25.370
And we also want one around our enemy tank, which is already tagged as Tank and has got a box collider

04:25.370 --> 04:25.700
on it.

04:25.700 --> 04:26.450
So that's good.

04:26.480 --> 04:33.240
So this green tank has to be tagged with the tank tag like that.

04:33.260 --> 04:39.020
Now, if you don't have the tank tag, feel free to make it and then attach it to the tank and the enemy,

04:39.020 --> 04:42.080
the top parent of both of those objects.

04:42.470 --> 04:47.780
Now, I'm just remembering before we actually stripped the shell, prefab from everything, before we

04:47.780 --> 04:49.020
started playing with it.

04:49.040 --> 04:54.200
What I'd like you to do is actually select it and make sure that you add a rigid body because it's the

04:54.200 --> 05:00.200
rigid body that's going to register, whether it's hit something and also have used gravity on it,

05:00.200 --> 05:01.250
as you can see there.

05:01.280 --> 05:04.160
Now you can change that with it just sitting in the assets.

05:04.160 --> 05:06.550
You don't have to make a copy in the hierarchy.

05:06.560 --> 05:06.920
Okay.

05:06.920 --> 05:09.920
So with that done, we can actually press play.

05:09.920 --> 05:19.280
And if we hit the B key for the red tank, you'll see that it's now producing shells and they're exploding.

05:19.280 --> 05:20.210
Now, why are they exploding?

05:20.210 --> 05:22.790
Because they're actually hitting that tank.

05:22.880 --> 05:26.980
If you hit the space bar, this one will be shooting cells.

05:26.990 --> 05:33.780
What you don't want your shells to be doing is being hit by the object that they're coming from.

05:33.910 --> 05:34.160
Okay.

05:34.160 --> 05:37.760
So what we can do with that instead is actually just modify.

05:37.790 --> 05:40.550
Let's stop playing their turret position.

05:40.550 --> 05:49.970
So on the red tank, if we just come in closer here and hit the W key, we can actually move that out

05:49.970 --> 05:51.500
to about there.

05:51.500 --> 05:58.430
And then we'll also go to our green tank, which has our tank turret that we added previously.

05:58.460 --> 06:05.630
Now it's not based on the rotation of that tank, if you remember, but we could just move it up like

06:05.630 --> 06:11.860
this and then hit the key and rotate it around its x axis.

06:11.870 --> 06:17.750
You'll need to check that that is not inside of this or make this a little bit smaller.

06:17.750 --> 06:19.670
That's the collider on your tank.

06:19.670 --> 06:25.700
So I'm going to make the collider just a little bit smaller in the Z direction like that.

06:25.700 --> 06:27.530
And then we'll go over to the enemy tank.

06:27.530 --> 06:28.820
Let's just double check that.

06:28.820 --> 06:32.650
Let's change its box collider in the Z.

06:32.660 --> 06:33.140
Okay.

06:33.140 --> 06:41.360
So now if we press play, we can check those bullets out, make sure that they don't, and now they're

06:41.360 --> 06:42.320
not hitting the tank.

06:42.320 --> 06:44.960
They're also not going anywhere because we haven't put any code in there.

06:44.960 --> 06:49.220
So let's go back into our Shell script and add that.

06:49.220 --> 06:56.450
So what I want you to do for a challenge in here is to project that shell forward along its Z axis.

06:56.450 --> 07:01.430
We've already done that before and you've already got the code to do it, make sure that it's going

07:01.430 --> 07:09.050
at one meter per second, or that you can set the speed of it and put that inside of this late update.

07:09.050 --> 07:10.700
So pause the video now give it a go.

07:10.700 --> 07:13.010
And when we come back, I'll go through it with you.

07:14.950 --> 07:15.520
Okay.

07:15.520 --> 07:16.580
Welcome back.

07:16.600 --> 07:21.130
What are we going to do is, of course, first of all, define our speed.

07:21.130 --> 07:25.290
So float speed equals one.

07:25.510 --> 07:30.190
So that's if you want one meter per second and that's what you would have.

07:30.190 --> 07:32.350
Let's let's make it three.

07:32.350 --> 07:36.640
And we could also make it public so we can change it in the inspector like that.

07:36.640 --> 07:46.360
And then inside of our light update, you're going to have this dot transform, dot translate zero in

07:46.360 --> 07:58.300
the extraction, zero in the Y direction and speed multiplied by time, dot delta time in the Z direction

07:58.300 --> 08:00.340
so that it'll move forward in the Z.

08:00.340 --> 08:02.170
Okay, so we'll save this.

08:02.200 --> 08:07.030
We'll go back into unity and we'll have a look at the result of that.

08:07.210 --> 08:08.980
So we'll press play.

08:09.460 --> 08:17.350
Let me just zoom out a little bit in this same view so we can see if they go further afield and then

08:17.350 --> 08:20.650
we'll use the B key and have a look at where they go.

08:20.650 --> 08:25.090
Now, notice that they come out and there's a little bit of a curve in the trajectory.

08:25.090 --> 08:28.630
That's because the gravity is on that rigid body.

08:28.630 --> 08:35.140
So the rigid body is moving with the Unity's physics system to add gravity, but it's moving forward

08:35.140 --> 08:37.630
with our transform translate.

08:37.630 --> 08:44.440
So if we go into our shell just for now, there's the shell we want to turn off the gravity.

08:44.440 --> 08:51.250
So down here in news, gravity, turn that off there, because we're actually programming our own acceleration

08:51.250 --> 08:55.900
movement, not using the physics system in Unity's, although you could do that.

08:55.900 --> 08:57.250
But that's not the point.

08:57.250 --> 08:58.420
Okay, so we've got that.

08:58.420 --> 09:05.860
Now, what we're going to do back in our shell script is calculate the speed based on acceleration.

09:06.040 --> 09:11.440
So we're going to now add in some variables to do with our physics of acceleration.

09:11.440 --> 09:22.840
We want to mass, we'll set it to equal ten, then we want a float of force, set that to 1000, then

09:22.840 --> 09:28.840
we need a float for acceleration and we've already got our speed value up there.

09:28.870 --> 09:29.320
Okay.

09:29.320 --> 09:32.380
So let's come down and use that in our late update.

09:32.470 --> 09:34.930
So first we calculate acceleration.

09:34.930 --> 09:39.460
Can you remember what the formula for acceleration is?

09:39.460 --> 09:47.230
Well, it's acceleration equals force divided by mass.

09:47.470 --> 09:55.120
This is the F equals M a formula that we looked at in the previous lecture from acceleration.

09:55.120 --> 09:56.380
We can work out the speed.

09:56.380 --> 10:03.580
So the speed is determined by the acceleration.

10:03.580 --> 10:08.290
How much acceleration over time.

10:08.290 --> 10:13.330
So multiplied by a time dot delta time.

10:13.330 --> 10:18.550
So now our speed is not put in as a set value.

10:18.550 --> 10:21.130
It's going to be based completely on acceleration.

10:21.130 --> 10:25.510
And if you think of the way a car works, it is based on the acceleration.

10:25.510 --> 10:28.630
Without any acceleration, there is no speed.

10:28.630 --> 10:29.800
So save this.

10:29.800 --> 10:36.310
Now we'll go back into unity and we will press play and have a look at the effect when we press the

10:36.310 --> 10:40.240
B key of the Shell's programming case.

10:40.240 --> 10:47.260
So if I hit the beak, it's going to go very long way because we are putting a fair bit of force on

10:47.260 --> 10:47.440
there.

10:47.440 --> 10:54.700
Notice how much force is coming out of that bullet and it just speeds up because we're constantly adding

10:54.700 --> 10:58.330
that force over and over and over and over again to the bullet.

10:58.330 --> 11:00.430
We're not actually just adding it once.

11:00.430 --> 11:06.340
So it's like that bullet is getting pushed through the air, which unless it has a rocket ship behind

11:06.340 --> 11:11.890
it, blasting out some kind of propulsion, it's not in real life going to go very well.

11:11.890 --> 11:15.400
It would have a little boost and then want to just fall to the ground.

11:16.090 --> 11:16.600
Okay.

11:16.600 --> 11:21.010
So let's come back in the next lecture and I look at those particular mechanics.
