WEBVTT

00:03.480 --> 00:09.300
Particles that are emitted at a certain velocity and angle that have no further force applied to them

00:09.300 --> 00:12.930
other than gravity form the curve of a parabola.

00:13.110 --> 00:20.070
We call this type of motion projectile motion, and it's the exact thing our tank shells are simulating.

00:20.370 --> 00:25.860
There's a lot of mathematics and trigonometry involved in working with parabolas, and an excellent

00:25.860 --> 00:32.700
breakdown of it is all on Wikipedia, where you can find more information if you want to delve in deeper.

00:32.700 --> 00:35.760
But let's just go over a few of the basics.

00:36.530 --> 00:44.660
All motion of the projectile we consider in two directions horizontally, which is its x coordinates

00:44.660 --> 00:47.660
and vertically which are its y coordinates.

00:48.200 --> 00:54.890
After the projectile is fired, it experiences no further acceleration in the x direction.

00:55.190 --> 00:58.850
Acceleration remains constant though.

00:58.880 --> 01:01.880
Wait, I hear you say or I hope you said.

01:02.240 --> 01:06.050
That would mean that an object would never stop moving.

01:06.290 --> 01:09.470
And indeed, in a vacuum that is the case.

01:09.590 --> 01:16.190
Of course, in the real world there's drag and wind resistance and all other matter of things that you

01:16.190 --> 01:17.300
could consider.

01:17.300 --> 01:21.110
But for simplicity, we aren't going there now.

01:21.110 --> 01:26.960
The Y direction of acceleration is always going to be our good old friend gravity.

01:26.960 --> 01:33.800
You can see in the animation how gravity remains constant vertically, no matter what the angle of projection

01:33.800 --> 01:34.580
is.

01:35.090 --> 01:43.190
So acceleration matters in projectile motion are fairly straightforward and so too are the ones to do

01:43.190 --> 01:44.450
with velocity.

01:44.510 --> 01:50.480
If you can remember your high school trigonometry, there's a great Wikipedia page where you can brush

01:50.480 --> 01:52.280
up on those skills too.

01:52.490 --> 01:59.720
So the velocity or vector that the projection begins at is donated v zero or the starting velocity.

01:59.720 --> 02:04.430
And the angle this vector makes with the horizontal is alpha.

02:04.880 --> 02:11.510
Using this knowledge, we can construct a right angle triangle to break velocity into its y or vertical

02:11.510 --> 02:14.960
direction and x its horizontal direction.

02:14.990 --> 02:24.320
Using trigonometric ratios, we can further work out that v x equals v zero multiplied by cosine of

02:24.320 --> 02:34.970
alpha and v y equals v zero multiplied by the sign of alpha minus g t where g t is the gravitational

02:34.970 --> 02:37.220
effect at any point in time.

02:37.460 --> 02:42.410
Remember that the acceleration due to gravity has a compounding effect.

02:42.410 --> 02:50.180
So each game loop or time interval that we are adding another lot of gravity to is going to multiply

02:50.180 --> 02:50.510
it.

02:50.510 --> 02:58.130
So if we do this t number of loops, then we have to multiply gravity by T to get the correct amount.

02:58.340 --> 03:05.570
Now from this diagram, if we know v, x and v, y, we can calculate the initial speed of the projectile,

03:05.570 --> 03:10.220
which is the magnitude or length of our v zero vector.

03:10.430 --> 03:15.920
And to do this we use Pythagoras theorem thus for what we want to use.

03:15.920 --> 03:22.790
The principles of projectile motion for is to work out the angle the tank needs to aim its turret at

03:22.790 --> 03:30.170
in order to shoot a shell with a known speed and hit its target where the target's position is known.

03:30.440 --> 03:38.960
So to calculate this angle, the formula that can be used is ten of the angle equals the speed squared,

03:38.960 --> 03:47.990
plus or minus the square root of the speed to the power of four minus g multiplied by g x squared plus

03:47.990 --> 03:53.660
two y multiplied by the speed squared all divided by g x.

03:54.110 --> 03:59.870
If you haven't used an equation with the plus minus symbol in it before, what it means is that there

03:59.870 --> 04:02.300
can be two possible solutions.

04:02.390 --> 04:07.640
In this case, there could be two angles that could be used to hit the same final location.

04:07.640 --> 04:10.520
But that's the nature of parametric equations.

04:10.610 --> 04:17.090
And you can see this in the animation shown for all the final hit points of the projectile, there can

04:17.090 --> 04:19.130
be two starting angles.

04:19.610 --> 04:21.980
So enough with all this mathematics.

04:21.980 --> 04:23.750
We have the equation that we need.

04:23.750 --> 04:26.390
Let's get back into unity and implement it.

04:26.720 --> 04:32.570
Okay, so we're now going to concentrate on working with the green tank and the shells that are coming

04:32.570 --> 04:36.500
out of it because it's kind of our eye character as such.

04:36.500 --> 04:41.600
So it's the one that has to automatically calculate the trajectory for our shells.

04:41.840 --> 04:46.910
So we need to actually create a different type of shell than the one we've got.

04:47.180 --> 04:53.540
So get the shell prefab that you're working with that's currently on both tanks and do a controlled

04:53.540 --> 04:53.990
D of it.

04:53.990 --> 04:55.700
So you get a second version.

04:55.700 --> 05:04.250
Now you can rename this as a shell, so you know it belongs to the green tank and then just select it.

05:04.250 --> 05:11.390
And over in the inspector, what we want to do is take off the shell script that's on there so we can

05:11.390 --> 05:13.220
just remove that.

05:13.640 --> 05:17.060
And what we do should get rid of this mov shell script as well.

05:17.780 --> 05:20.960
So we will make a new script to move it forward.

05:20.960 --> 05:23.450
You can leave destroy shell on there.

05:23.450 --> 05:24.470
That's alright.

05:24.650 --> 05:29.810
But something you should do now that I haven't done in this video is make sure that you check.

05:29.810 --> 05:34.730
Use gravity on this prefab as well as it will be needed later on.

05:35.270 --> 05:35.540
Okay.

05:35.740 --> 05:41.590
So the next thing we need to do is to edit the code for that shell.

05:41.590 --> 05:50.770
So I'm going to create a new script and we'll have this as the shell script.

05:50.770 --> 05:52.720
So just open that up.

05:53.170 --> 05:59.620
Now you want to get some stuff in here from the original Shell script that we're already using, and

05:59.620 --> 06:03.250
mostly it's to do with the collision stuff.

06:03.250 --> 06:06.520
So grab the on collision, enter.

06:06.550 --> 06:10.300
Now we're also going to need to bring through the explosion.

06:10.300 --> 06:19.360
So if we just grab this and copy it and come over to the shell and then just add that to the top and

06:19.360 --> 06:23.650
get rid of all of these things that we programmed physics with.

06:24.640 --> 06:29.080
And in here that looks good.

06:29.080 --> 06:30.340
Nothing else to do in here for now.

06:30.340 --> 06:31.480
So just save that.

06:31.570 --> 06:37.570
Let's go back into unity and now we want to attach that shell script to it.

06:37.570 --> 06:45.490
So just drag that over and we want to add in the explosion, which is in our prefabs folder, which

06:45.490 --> 06:46.600
is just here.

06:46.600 --> 06:50.470
So let's just go back to our shell.

06:51.620 --> 06:56.650
Actually, let me just put a copy of that into the hierarchy which will make it easier, then go into

06:56.650 --> 07:04.240
the prefabs and drag over that explosion, shell explosion just into there like that.

07:04.240 --> 07:11.200
And after making those changes, make sure that you click on override and apply all for that prefab.

07:11.530 --> 07:15.130
Okay, so now back into our scripts.

07:15.130 --> 07:16.990
We want to get hold of the fire shell.

07:16.990 --> 07:21.610
So Fire Shell is the script that is on the green tank.

07:21.610 --> 07:28.540
Let's open that up after I delete this shell because we're finished playing ramp that prefab and we'll

07:28.540 --> 07:33.760
get to work modifying this so it will automatically do trajectories.

07:34.750 --> 07:35.200
Okay.

07:35.200 --> 07:40.750
So to begin with, we're going to remove the calculate trajectory stuff from this script.

07:40.810 --> 07:46.090
If you want to keep a copy of this, then make a copy back up of Fire Shell.

07:46.090 --> 07:52.600
Before you delete all of these things, let's come down and get rid of our calculate trajectory and

07:52.600 --> 07:56.890
we're also going to remove that from in here.

07:56.890 --> 08:05.020
So we'll remove calculate trajectory and we will also get rid of this AIM code and just have the create

08:05.020 --> 08:13.510
bullet like we did at the beginning when the spacebar is clicked on, which of course later on it can

08:13.510 --> 08:14.830
do it automatically.

08:15.310 --> 08:17.980
Okay, so let's just bring that back in now.

08:17.980 --> 08:22.810
Instead, we're going to use Unity's physics system for all of these things.

08:22.810 --> 08:28.480
But the first thing we need to do is actually turn the tank around so that it's facing the enemy.

08:28.480 --> 08:38.610
So we'll create a vector three and we'll call that direction, and that's going to equal our enemy dot

08:39.070 --> 08:51.640
transform dot position minus this dot transform dot position.

08:52.390 --> 08:58.090
And you can normalize that if you like because we only need the direction vector so it can be a length

08:58.090 --> 08:59.230
of one that's fine.

08:59.230 --> 09:00.910
So normalized.

09:01.480 --> 09:05.950
And then we'll do a Catalonian to turn this around so I can turn in.

09:06.760 --> 09:17.110
Look, rotation equals return in dot look rotation.

09:18.520 --> 09:24.100
Now we could be using a look at for the transform on the green tank, but then it just snaps around

09:24.100 --> 09:24.880
to the exact position.

09:24.880 --> 09:30.490
This will actually sort of turn it over time, so it'll sort of look like it's moving, which is basically

09:30.490 --> 09:32.860
using the slip that we're going to do.

09:32.860 --> 09:46.090
So we're going to look rotation at new vector three direction dot X zero in the Y and direction dot

09:46.090 --> 09:46.840
Z.

09:46.840 --> 09:52.060
Now this is just in case the tanks aren't on the same level.

09:52.060 --> 09:56.470
You don't want your tank to actually lift up off the ground as though it's sort of looking up in the

09:56.470 --> 09:59.290
air because a tank won't actually do that.

09:59.620 --> 10:02.530
Okay, so that is our look rotation.

10:02.530 --> 10:17.410
Then we will go this dot transform dot rotation equals turn in dot slurp, which is a spherical interpolation

10:17.410 --> 10:20.290
between one angle of rotation and another.

10:21.080 --> 10:26.320
So this will be this dot transform dot rotation.

10:26.320 --> 10:31.210
So it's current rotation and the look rotation.

10:33.420 --> 10:37.440
And we're going to update that.

10:37.710 --> 10:50.220
Time Delta time multiplied by a rocket speed, which is our rotation speed and then end brackets.

10:50.840 --> 10:51.120
Okay.

10:51.120 --> 10:55.770
Now, we don't have this defined, so we need to come up to the top of our code and put that in.

10:56.940 --> 11:08.670
So up here, we'll just create a float for speed and let's set that to two and you can come back and

11:08.670 --> 11:12.730
change that if you'd like your green tank to turn quicker.

11:12.760 --> 11:13.770
Okay, so we've got that.

11:13.770 --> 11:22.740
Let's just save it and we'll go back in to Unity and try this out so you can actually drive the red

11:22.740 --> 11:23.610
tank.

11:24.090 --> 11:31.020
And the green tank should turn and look at it constantly and you can see it there.

11:31.050 --> 11:38.190
Notice how it doesn't snap immediately around actually slurps, which is a more sort of natural movement.

11:38.640 --> 11:44.760
In the next video, we'll come back and calculate the angle that we need to have our turret set out

11:44.760 --> 11:51.480
to shoot our projectile so it hits the red tank according to the equation that we looked at earlier

11:51.480 --> 11:52.410
in this video.
