WEBVTT

00:06.420 --> 00:11.430
Welcome back to this two parter on calculating a trajectory.

00:11.430 --> 00:16.790
Now we've got our calculate trajectory method in the fire shell script.

00:16.800 --> 00:24.420
Now we just have to translate the formulas that we looked at in the previous video into here to calculate

00:24.420 --> 00:32.340
where that tank is going to be down the track so that the bullet can be calculated with its speed to

00:32.340 --> 00:34.500
be in the same spot at the same time.

00:34.500 --> 00:34.950
Okay.

00:34.950 --> 00:37.230
So let's get started on this code.

00:37.230 --> 00:45.000
First of all, I want to change this to return a vector three and then that vector three will actually

00:45.000 --> 00:46.230
set in the update.

00:46.230 --> 00:54.690
So this dot transform dot forward equals the vector that we return.

00:54.840 --> 00:55.260
Okay.

00:55.260 --> 01:01.500
So let's start by defining all of the inputs for these formulas.

01:01.500 --> 01:07.500
So we need a vector three P, which is the position of the enemy.

01:07.800 --> 01:14.880
So that's going to equal enemy dot transform position.

01:15.300 --> 01:18.270
Now it has to be relative to the shooter.

01:18.270 --> 01:23.940
So we'll take away the shooter's position because all of this stuff is being calculated relative to

01:23.940 --> 01:29.520
the shooter, which means you kind of get to get all the coordinates of the enemy into your shooters

01:29.520 --> 01:30.930
local space.

01:31.320 --> 01:33.960
Then we need our vector for v.

01:34.200 --> 01:41.340
So three v which is our forward vector and speed of our enemy.

01:41.340 --> 01:50.310
So enemy dot transform dot forward multiplied by the enemy's speed.

01:50.310 --> 02:02.310
So enemy dot get component and we're going to get the drive script and then grab the speed out of there

02:02.460 --> 02:06.570
and then we are going to get the speed of the bullet.

02:06.570 --> 02:20.520
So float s equals bullet dot get component move shell and dot speed there.

02:20.850 --> 02:27.420
All right, now I'm having a little bit of a problem remembering whether I got you to put enemy up at

02:27.420 --> 02:28.050
the top peers.

02:28.050 --> 02:31.020
We have to pass through the enemy so we can use the value.

02:31.020 --> 02:32.520
So just check if you've got it.

02:32.520 --> 02:34.380
If you haven't, just put it in there.

02:34.620 --> 02:35.100
Okay.

02:35.100 --> 02:41.520
So now moving on, let's go and create A, B, C and D from our quadratic formula.

02:41.520 --> 02:55.200
So float a will equal vector 3.. of V and V minus s.

02:56.760 --> 02:59.160
Multiplied by.

02:59.190 --> 02:59.460
Yes.

02:59.460 --> 03:02.490
And you can see where that is in the formula.

03:02.790 --> 03:10.140
Then we need float for B and we need a float for C.

03:10.560 --> 03:11.070
Okay.

03:11.070 --> 03:16.290
So this is a challenge for you is to come up with the formulas for B and C.

03:16.320 --> 03:19.280
Pause the video now give it a go and we'll come back.

03:19.290 --> 03:21.270
We'll keep typing through this.

03:21.420 --> 03:21.880
Okay.

03:21.900 --> 03:23.400
How did you go with those?

03:23.430 --> 03:35.010
Well, they're actually both dot products, so it'll be a vector 3.. and B is P and V and then C is

03:35.010 --> 03:38.670
going to be a vector 3.. product.

03:38.850 --> 03:41.790
For P and p.

03:43.010 --> 03:43.330
Okay.

03:43.470 --> 03:56.580
Then we can add in D, so I float D and that's going to equal B times B or B squared minus C times A.

03:57.060 --> 04:05.430
And in the formula it's actually got C, A, times C, this is inside of your square root like this.

04:05.430 --> 04:11.760
So we can actually take advantage of there being fours and twos in those and cancel those values out,

04:11.760 --> 04:18.540
which is what you can do if you're deriving these values from quadratic formulas.

04:18.660 --> 04:23.670
So we really need, first of all, to figure out what the value of D is.

04:23.670 --> 04:28.980
If D, which is what's under the square root is going to be a small number.

04:29.050 --> 04:36.240
Okay, then the bullet isn't actually going to be fast enough to catch up, in which case we can return

04:36.240 --> 04:37.530
like zero or something.

04:37.530 --> 04:38.190
So we'll go.

04:38.190 --> 04:49.500
If D is less than 0.1 F, then we can return vector 3.0.

04:50.320 --> 04:50.840
Okay.

04:51.000 --> 04:58.800
Now if we're continuing on with this, then we need to calculate what the actual square root element

04:58.800 --> 04:59.250
is.

04:59.250 --> 05:09.000
So float square root equals massive dot square root of DX.

05:10.260 --> 05:10.880
Okay.

05:10.890 --> 05:20.460
Now we found out previously from our root equation for our quadratics that there are two possible values

05:20.460 --> 05:25.360
because there's a plus value in a minus value, so they can be two.

05:25.380 --> 05:31.800
So we're actually interested in what those two values are, and then we want to pick the best one.

05:31.980 --> 05:41.040
So we want float time one is going to equal and the first one will be minus B, minus the square root

05:41.040 --> 05:50.640
divided by C and float t two is going to equal what time for another challenge?

05:50.640 --> 05:56.190
What will this second root from the quadratic become force video.

05:56.190 --> 05:56.910
Now give it a go.

05:56.910 --> 05:59.160
And when we come back, I'll keep typing through it.

05:59.160 --> 05:59.560
Right.

05:59.580 --> 06:00.210
How did you go?

06:00.210 --> 06:04.890
Well, the formula is negative B plus or minus the square root, blah, blah, blah.

06:04.890 --> 06:11.210
So this one will be negative B plus the square root divided by C.

06:11.220 --> 06:11.700
Okay.

06:11.700 --> 06:13.290
So now we have these two values.

06:13.290 --> 06:16.230
We can figure out which one we actually want to use.

06:16.230 --> 06:22.410
So let's create a float for the actual time that we'll use and we'll set that initially to zero.

06:22.410 --> 06:32.970
Then we're going to say if t one is less than zero and T two is less than zero, then there's no acceptable

06:32.970 --> 06:33.810
solution.

06:33.810 --> 06:37.860
So we'll just set t equals zero in this case.

06:38.040 --> 06:38.580
Okay?

06:38.610 --> 06:47.130
Else if t one is less than zero, then t will equal t two.

06:48.410 --> 06:49.250
Else.

06:49.280 --> 06:59.780
If PT two is less than zero, then t will equal t one else.

06:59.780 --> 07:03.650
We will use the maximum value that we're getting out of there.

07:04.010 --> 07:15.020
So we'll put t equals math, f max, new float and then t1t2.

07:15.290 --> 07:25.280
So the math f max takes a array of floats and gives you back the maximum value which you can then use

07:25.720 --> 07:31.160
a bit quicker and easier than coming up with an if then else statement that does that.

07:31.160 --> 07:32.750
But of course you can do that too.

07:32.750 --> 07:41.690
Now, after we get through this little bit of logic, we're going to go return T multiplied by P plus

07:41.720 --> 07:42.560
V.

07:42.770 --> 07:50.030
So basically we're going to use our time to multiply out our values in order to calculate where the

07:50.030 --> 07:54.500
bullet and the enemy are going to cross paths.

07:54.500 --> 08:02.180
And when they do, we'll actually use that as the vector that we're sending back to the tanks forward

08:02.180 --> 08:06.140
direction so that it can aim in that way.

08:06.290 --> 08:06.950
Okay.

08:06.950 --> 08:10.190
So I think we're ready to save this and try it out.

08:10.190 --> 08:11.210
So let's save it.

08:11.450 --> 08:13.700
Let's go back into unity now.

08:13.700 --> 08:17.750
I've changed my grand color to black so that you can see what's going on.

08:17.900 --> 08:23.900
If we go over to the tank, let's remind ourselves of our fire shell script and what needs to go in

08:23.900 --> 08:24.440
the inspector.

08:24.440 --> 08:27.770
You'll have a shell, the turret, and then you want your enemy.

08:27.770 --> 08:31.310
So drag and drop your enemy from the hierarchy into there.

08:31.340 --> 08:32.930
The enemy itself.

08:32.930 --> 08:34.700
We haven't changed anything on that.

08:34.700 --> 08:40.520
I have increased the speed to three just to make it go a bit faster when we're testing.

08:40.820 --> 08:49.280
Also, I've gone down into the shell prefab and changed its move shell script to three as well down

08:49.280 --> 08:49.760
there.

08:50.090 --> 08:57.110
Okay, so let's try this out press play and as soon as it gets going, I'm just going to hit the spacebar

08:58.250 --> 09:01.790
and we'll see whether we intercept the tank.

09:01.790 --> 09:02.240
Yep.

09:02.240 --> 09:11.000
Now, at some point, you can see here that things have kind of fallen over because we can't calculate

09:11.000 --> 09:12.950
an actual trajectory.

09:13.040 --> 09:19.340
If I press play again and actually shoot lots of bullets, you'll find that they will arc across the

09:19.340 --> 09:27.230
screen and as the tank moves, it will move through all of them until a point where our math types of

09:27.350 --> 09:28.220
breaks down.

09:28.220 --> 09:29.360
Well, it doesn't break down.

09:29.360 --> 09:33.290
This is when we're going to get very, very small and inaccurate values.

09:33.290 --> 09:37.040
So that one there is where it first broke down.

09:37.070 --> 09:43.310
If we come out of here, these bullets are going to be the last lot that actually intersect with this

09:43.310 --> 09:43.640
tank.

09:43.640 --> 09:46.850
The further and further it goes out there.

09:46.850 --> 09:49.430
Now, that can be a bit of a problem.

09:49.430 --> 09:51.320
So we probably don't want that happening.

09:51.320 --> 09:57.440
We've also at this point, because we're returning zero when we get values we're not interested in and

09:57.440 --> 10:00.290
we're trying to set the forward vector of the tank to that.

10:00.290 --> 10:04.430
We're getting a look rotation viewing vector is zero error.

10:04.430 --> 10:08.030
So let's just go back into our code and fix that up.

10:08.030 --> 10:08.390
Okay.

10:08.390 --> 10:12.620
So we're returning zero up here, which is where our issue is.

10:12.620 --> 10:18.530
Let's come down into the update and we'll actually store the returned value first.

10:18.530 --> 10:31.880
So we'll go vector three, aim at equals, calculate trajectory and then we can test it if aim at equals

10:32.150 --> 10:34.580
vector 3.0.

10:34.730 --> 10:42.980
Or I should say if it doesn't equal vector three zero, then we will actually use it to set our transformed

10:43.100 --> 10:43.370
forward.

10:43.370 --> 10:46.520
So aim at down there.

10:46.520 --> 10:51.740
Now in addition where we're setting T to zero, we're not really interested in that either.

10:51.740 --> 10:59.120
So instead of doing that, we'll just go up here and we'll return zero vector in there.

10:59.120 --> 11:02.600
Okay, let's save this and have one more shot at it.

11:03.440 --> 11:03.830
Okay?

11:03.830 --> 11:10.220
So start hitting the spacebar and then when I get around as far as I can go, it will actually stop

11:10.790 --> 11:12.620
sending out bullets.

11:12.620 --> 11:19.460
But you can see as the tank arcs around, it actually will go through and hit all those bullets.

11:19.460 --> 11:19.730
Okay.

11:19.730 --> 11:21.320
So that's a simple.

11:21.590 --> 11:22.610
Yes, simple.

11:22.610 --> 11:28.580
That's probably one of the simplest ways of calculating projectile hitting that doesn't actually involve

11:28.580 --> 11:32.810
acceleration and gravity, which becomes a far more complex.

11:32.810 --> 11:34.940
So that is the start of it.

11:34.940 --> 11:40.700
And you can just imagine that the math from here for more complex things gets even more complicated.

11:41.180 --> 11:47.030
And when we come back in the next, we'll start looking at acceleration before we look at more trajectories.
