WEBVTT

00:06.790 --> 00:10.390
Welcome back to the second part of calculating trajectories.

00:10.480 --> 00:17.560
Let's go back into our fire shell script, where also, while we're in here at the top of this file,

00:17.560 --> 00:22.210
going to add in a float for the speed of our bullet.

00:22.210 --> 00:29.320
And we'll set that to 15 initially because we're going to be doing the speed with the physics system

00:29.320 --> 00:31.930
in unity rather than calculating it like we did before this.

00:31.930 --> 00:34.240
We have a different value up here to start with.

00:35.020 --> 00:40.810
Okay, time to calculate the angle that we need to put our turret at.

00:41.080 --> 00:47.110
So we're going to create a new method and we'll create a float and we're going to create a float with

00:47.110 --> 00:51.880
the question mark on it like that for return, which will allow us to return a null.

00:52.270 --> 00:54.940
Remember our formula that we're about to do?

00:54.940 --> 01:02.590
Well, it can return angles, a high angle, a low angle or find absolutely no angle whatsoever, which

01:02.590 --> 01:07.060
means it's not possible to get an angle that will hit the target.

01:07.300 --> 01:13.930
Now we're going to call this calculate angle and we'll pass through a bool for low to say whether we

01:13.930 --> 01:21.490
want to get a low or a high angle back from our calculation, because we could get to values as we'll

01:21.490 --> 01:27.820
see in a moment, just like our quadratics that we were looking at previously, where you can have two

01:27.820 --> 01:28.480
solutions.

01:28.480 --> 01:31.000
We've got the same kind of thing going on here.

01:31.000 --> 01:34.300
In fact, this stuff is all coming from a quadratic anyway.

01:34.300 --> 01:40.570
And if you have a look at this solution that we're currently working with, it looks very much like

01:40.570 --> 01:44.560
the make up of the root of a quadratic equation.

01:44.680 --> 01:47.050
Therefore we've got that plus minus in there.

01:47.050 --> 01:52.540
It's going to give us two possible values, one value or no values.

01:52.780 --> 01:59.290
Okay, so let's go vector three, target d, i r.

01:59.290 --> 02:02.590
So we need the direction to our target, which is our enemy.

02:02.590 --> 02:08.230
So enemy dot transform dot position.

02:10.060 --> 02:14.260
Minus this dot transform dot position.

02:17.620 --> 02:26.170
Float y equals target d r dot y.

02:26.380 --> 02:32.350
And then we'll grab that and set it to zero, because we're only interested in the direction of the

02:32.350 --> 02:37.480
target on the x z value because it's on a flat surface.

02:37.480 --> 02:43.390
And as we did with our look rotation, we didn't actually lift the tank up off the ground.

02:43.690 --> 02:49.060
Okay, so target dear dot y equals zero.

02:49.750 --> 02:52.870
If so, let's just zero that out.

02:52.870 --> 02:53.350
Okay.

02:53.350 --> 03:01.420
So float x equals target di r dot magnitude.

03:01.600 --> 03:04.540
And these are all variables going into our equation.

03:04.540 --> 03:06.580
Float gravity.

03:07.090 --> 03:08.860
Can you remember what gravity is?

03:08.860 --> 03:16.450
From our calculations we did before 9.8 meters per second squared and I'm actually going to have it

03:16.450 --> 03:21.010
in positive this time to save us, negating it later on which it is.

03:21.010 --> 03:30.490
So float s squared equals the speed of the bullet multiplied by the speed of the bullet.

03:30.490 --> 03:31.930
So speed squared.

03:32.620 --> 03:38.290
And then we can calculate the equation that is under the square root in there.

03:38.290 --> 03:47.350
So we want to float and then we want under the square root equals.

03:47.350 --> 03:49.450
And this is your challenge.

03:49.450 --> 03:52.210
Can you put together this formula?

03:52.210 --> 03:57.010
So we only want what's sitting under that square root in that equation to go under.

03:57.010 --> 03:59.710
This equals here, pause the video now give it a go.

03:59.710 --> 04:02.710
And when we come back, I'll go through it with you.

04:04.770 --> 04:16.740
Okay to begin, let's go s squared multiplied by S squared minus gravity multiplied by in brackets gravity

04:17.010 --> 04:29.490
multiplied by x multiplied by x plus two multiplied by y multiplied by s squared.

04:29.940 --> 04:30.630
Okay.

04:30.720 --> 04:32.400
How did you go with that?

04:32.430 --> 04:34.020
Fair bit in that equation.

04:34.020 --> 04:37.320
That's all that's sitting underneath the square root.

04:37.320 --> 04:41.390
So now that we've got that in a variable, we can do the other calculations.

04:41.400 --> 04:42.540
So we want in here.

04:42.540 --> 04:55.950
If under the square root is greater than or equal to zero F, then float root equals math f dot square

04:56.400 --> 05:04.950
root of under the square root because you can't get a square root of a negative number.

05:04.950 --> 05:10.470
Well, you can, but that's going into imaginary numbers, which is not where we're going right now.

05:10.950 --> 05:28.110
So float high angle equals square root s squared plus the root and then float low angle equals s square

05:28.470 --> 05:31.680
minus the root.

05:31.990 --> 05:34.880
Then if lo.

05:34.890 --> 05:44.160
So if we want the low angle, then we'll return math f dot a10 squared.

05:44.160 --> 05:53.250
So we actually do the inverse tan of the result that we got, which is our low angle and gravity multiplied

05:53.250 --> 05:56.400
by X, which will be in radians.

05:56.400 --> 06:11.760
So we'll multiply that by math f radians two degrees else we will return math f 18 squared, high angle

06:12.210 --> 06:21.990
gravity multiplied by x and again multiply by math f rad two deg.

06:22.180 --> 06:30.840
Otherwise, if we've got a negative under that square root sine, we're going to return.

06:32.250 --> 06:32.910
No.

06:35.130 --> 06:35.620
Okay.

06:35.640 --> 06:38.430
Let me just fix up that spelling mistake and we're done.

06:38.430 --> 06:41.790
So there is our calculate angle formula.

06:42.210 --> 06:47.370
Now that we've got our angle, we need to set our turret to that.

06:47.370 --> 06:51.300
Now what you'll need to do is bring the turret base in.

06:51.300 --> 06:56.430
Remember, the turret on the green tank is actually where the bullets are coming from.

06:56.430 --> 06:59.460
You'll need to add this, which I've just done right now.

06:59.460 --> 07:06.600
So add in a public transform for the turret base and we want to save it, flip back into unity and select

07:06.600 --> 07:15.360
the green tank and then get the parent object that's holding on to your little turret marker that you

07:15.360 --> 07:19.980
created and drag and drop that into the turret base.

07:19.980 --> 07:27.810
So it's basically the same bit that we programmed in the red tank that could move up and down with the

07:27.810 --> 07:29.430
T and G keys.

07:29.730 --> 07:30.060
Okay.

07:30.090 --> 07:33.480
So now we can go in and actually program that to move.

07:33.480 --> 07:43.020
So we're going to create a new method under here and to create bullet, we'll call it void, rotate,

07:44.550 --> 07:45.450
turret.

07:46.500 --> 07:49.890
And inside of here we'll call that calculate angle.

07:49.890 --> 07:59.910
So we want a float question mark angle equals calculate angle and we'll say yes, we want the low one.

08:00.240 --> 08:11.430
And then if angle does not equal null, then inside of here we'll go turret base dot local euler angles

08:11.850 --> 08:29.010
equals new vector three 360 f minus the 360 F minus float the angle 0f0f.

08:29.010 --> 08:35.730
So we're basically rotating around our x axis, only taking into consideration the rotation that is

08:35.730 --> 08:37.710
already on that.

08:38.160 --> 08:38.670
Okay.

08:38.670 --> 08:40.410
So that's pretty much all we need there.

08:40.440 --> 08:42.450
We just need to call rotate turret.

08:42.450 --> 08:53.280
So let's come down into our update after we're sort of looking and put rotate turret in there so we

08:53.280 --> 08:55.170
can save this now and actually try it out.

08:55.170 --> 09:00.000
So come back into unity and we will press play.

09:00.690 --> 09:03.480
Move your red tank around and.

09:03.600 --> 09:16.800
Watch the turret of the green tank change to actually face or adjust itself for shooting at the red

09:16.800 --> 09:17.370
tank.

09:17.910 --> 09:23.760
When we come back in the next video, we'll put the final touches on this, which is to get the bullets

09:23.760 --> 09:29.790
travelling far enough that the whole system kind of works together so that the bullets will hit the

09:29.790 --> 09:33.270
tank based on the angle that they come out of the turret.
