WEBVTT

00:06.080 --> 00:08.390
Welcome back to this section.

00:08.390 --> 00:15.100
Looking at physics of objects, currently we're dealing with velocity that we've put on this bullet.

00:15.110 --> 00:20.660
Now what I want you to do with it, or the shell, as we've called it here, is drag and drop a copy

00:20.660 --> 00:24.800
of it into your asset and create an original prefab.

00:24.800 --> 00:28.820
We can then go back and open up our tanks code.

00:29.030 --> 00:33.440
We don't need to save this scene unless you want to write.

00:33.440 --> 00:38.360
So let's start by writing some script that we're going to call Fire Shell.

00:38.360 --> 00:43.670
So Go Create and we want a C sharp called Fire Shell.

00:43.670 --> 00:51.710
And in here we basically need to instantiate a copy of the shell and also position it where we want

00:51.710 --> 00:52.310
it to come out of.

00:52.310 --> 00:58.760
Now, if you double click on your tank to bring it in closer, you want it to come from this position

00:58.760 --> 01:05.600
here, not the position of the tank, because the global position of the tank or its origin for the

01:05.600 --> 01:07.160
model is actually sitting down here.

01:07.160 --> 01:10.430
So you don't want the bullets to come out of there, you want them up here.

01:10.430 --> 01:14.300
So what we can do in this case is we just add in a little marker.

01:14.300 --> 01:20.360
So we're going to go to our tank, we're going to right click it and create an empty.

01:20.360 --> 01:26.210
And then with that empty we will call it something like turret.

01:27.170 --> 01:30.830
And then you need to actually move it to where you want it.

01:31.100 --> 01:35.960
So select it and then just move it like this.

01:35.960 --> 01:44.180
Now if it is in an odd spot, then make sure you first of all, you 000 in here so it'll be sitting

01:44.180 --> 01:51.830
at the same position of the tanks, 000 then you can actually select it and just move it up and that

01:51.830 --> 01:53.600
will allow you to sort of put it just there.

01:53.600 --> 01:56.570
Now, we don't want it tipped at the moment.

01:56.570 --> 01:59.540
We want to shoot bullet in a straight direction.

01:59.540 --> 02:03.890
So we'll just keep that going horizontally with that Z axis.

02:04.010 --> 02:04.550
Okay.

02:04.550 --> 02:11.960
Now before we get on to the code for firing a shell, if you remember in Move Shell that we actually

02:11.960 --> 02:15.320
are going up the screen a little bit as well as across.

02:15.320 --> 02:23.270
We just want to change that back to going across the screen like that and then we can open up our fire

02:23.270 --> 02:24.500
shell code.

02:24.650 --> 02:27.290
So let's grab that.

02:27.740 --> 02:31.340
So in here we want to add in the bullet.

02:31.340 --> 02:42.650
So we want a public game object bullet and public game object turret.

02:44.600 --> 02:45.020
Okay.

02:45.050 --> 02:49.640
Then in the start, for now, what we'll do is we'll go.

02:52.060 --> 03:01.420
Instantiate a bullet at turret dot transform.

03:04.340 --> 03:15.080
Thought position and turret dot transform dot rotation to ensure that it's going to be facing in the

03:15.080 --> 03:15.860
right direction.

03:15.860 --> 03:17.660
And let's spell that correctly then.

03:17.660 --> 03:18.830
So just save that.

03:18.830 --> 03:20.630
Now we're going to go back into unity.

03:20.630 --> 03:27.830
We're going to drag and drop fire shell onto the tank in the inspector.

03:28.160 --> 03:29.960
And then you've got this bullet.

03:29.990 --> 03:36.560
You want to grab your shell from the assets folder, put that there, and then you want your turret

03:36.560 --> 03:40.580
drag the turret that's on the tank across to there.

03:40.610 --> 03:41.060
Okay.

03:41.060 --> 03:44.960
Now when we play it, the tank will actually shoot a bullet.

03:44.960 --> 03:53.360
So if we go play like that, you can see that it's shooting a bullet very slowly, one meter per second

03:53.360 --> 03:56.570
across the screen, and we'll speed that up a little bit later.

03:56.600 --> 03:56.870
Okay.

03:56.870 --> 04:01.370
So let's just stop playing to begin with.

04:01.370 --> 04:03.950
We're going to turn the red tank.

04:03.950 --> 04:11.330
So just select the enemy tank and then just hit on the key and just rotate it.

04:11.330 --> 04:15.290
So it's going to sort of move off into the distance like that.

04:15.290 --> 04:20.030
We then want to actually edit the script that's on that tank.

04:20.030 --> 04:22.520
So let's find the drive script, open that up.

04:22.520 --> 04:26.840
Now, this is the code that allows you to drive it, but we actually want to sort of automate it to

04:26.840 --> 04:27.290
begin with.

04:27.290 --> 04:34.760
So basically the transformed translate that's down here, just make a copy of that and we will put it

04:34.760 --> 04:35.750
down in there.

04:35.750 --> 04:43.340
We will comment out the other one and in this translation for the Zed, we'll just do a speed multiplied

04:43.340 --> 04:51.080
by time dot delta time and that's going to then just project that tank forward.

04:51.080 --> 04:56.870
You might also want to comment out the rotate just so that you don't accidentally rotate it.

04:56.870 --> 05:06.290
Okay, now if we save that and we just hit play to test it out, our red tank will go off in that direction

05:06.290 --> 05:10.400
into the distance whilst we're shooting across here.

05:10.940 --> 05:11.570
Okay.

05:11.630 --> 05:14.000
A little bit fast I think for my liking.

05:14.000 --> 05:18.380
So I'll just change this, speed down to one initially.

05:19.370 --> 05:19.820
Okay.

05:19.820 --> 05:27.710
So now we can go back into our fire shell and add some code in there to start shooting the bullet.

05:27.710 --> 05:30.530
And we'll do that when we hit the spacebar.

05:30.530 --> 05:31.160
So let's go.

05:31.160 --> 05:48.730
If input get key down is key code dot space, then we will instantiate the bullet.

05:48.740 --> 06:00.170
Okay, so we'll change this name of start up here to create bullet and down here we will run create.

06:01.220 --> 06:09.140
What we want to do now is to determine the angle that the tank should be facing before it actually creates

06:09.140 --> 06:10.130
the bullet.

06:10.460 --> 06:18.290
So in here we'll put calculate trajectory and create up here.

06:18.320 --> 06:23.150
Void, calculate trajectory.

06:25.100 --> 06:31.250
To solve this problem, we must first determine a time to impact based on when the bullet and the target

06:31.250 --> 06:33.290
will be in the same place.

06:33.290 --> 06:36.230
The bullets path is defined by its speed.

06:36.260 --> 06:40.940
The target's path is defined by its position plus its velocity.

06:41.420 --> 06:45.260
When they will be in the same place is dependent on the time.

06:45.260 --> 06:48.820
Therefore, we multiply each of these by time.

06:48.830 --> 06:54.200
Now, after that time, the target and the bullet are in the same place.

06:54.590 --> 06:58.160
That means they will be the same distance from the shooter.

06:58.310 --> 07:06.080
We express this as s multiplied by T equals p plus v multiplied by t.

07:06.110 --> 07:08.100
So what do we do with this equation?

07:08.120 --> 07:15.830
Well, we have to solve for T and this means getting some quadratics involved and solving for a quadratic

07:15.860 --> 07:16.880
equation.

07:16.910 --> 07:22.130
Now, if you're not familiar with quadratics, we don't really have enough time in this video to go

07:22.130 --> 07:22.730
through that.

07:22.730 --> 07:28.760
And I would suggest you go and read this Wikipedia link here and maybe also brush up on that mathematics

07:28.760 --> 07:32.530
if you have a look on YouTube at some teachings on that.

07:32.540 --> 07:37.930
Anyway, very simply and straightforward, what we're going to do here is to solve this.

07:37.940 --> 07:41.620
First of all, we're going to square both sides of it.

07:41.630 --> 07:42.140
Okay.

07:42.140 --> 07:52.130
And then that gives us the following line, which is the product of P with itself plus two P product

07:52.130 --> 08:01.680
v, t plus v dot product with itself and t squared equals s squared multiplied by t squared.

08:01.700 --> 08:08.690
Now quite quickly, if you do a square of a vector, it's actually the same as a dot product.

08:08.690 --> 08:11.690
So that's where those DOT products have come from.

08:11.690 --> 08:18.170
Now on the next line, we're basically getting the S squared T squared factor over on the other side

08:18.170 --> 08:18.770
of the equation.

08:18.770 --> 08:25.620
So the whole thing can equal zero and mixing that in with the values that we've already got.

08:25.640 --> 08:31.610
Okay, so this is our quadratic equation and we now have to solve it, will do it in code.

08:31.610 --> 08:37.340
But before we do, I just want to point out what all the variable names in the code are going to equate

08:37.340 --> 08:40.040
to that come out of this equation.

08:41.240 --> 08:48.370
So the general form of a quadratic equation is A X squared plus B, x plus C equals zero.

08:48.380 --> 08:50.720
We've got our equation in that format.

08:50.720 --> 08:55.410
So if you have a look, you can see where a x squared is.

08:55.410 --> 08:57.590
So that's the last term there.

08:57.590 --> 09:03.680
In this case, T is our x value because it's the value that we're trying to find.

09:03.680 --> 09:06.740
B x is the second term in the equation.

09:06.740 --> 09:11.960
Then C is the one at the beginning, the product of P and P.

09:12.110 --> 09:20.120
Now to solve this and find out what T is with a quadratic equation, there's two routes to it, and

09:20.120 --> 09:28.760
in this case we can use the formula shown here where X equals minus B plus minus square root of B squared

09:28.760 --> 09:31.040
minus four ac over two A.

09:31.040 --> 09:36.860
So basically we're going to take each of those different elements of that formula that equate to A,

09:36.890 --> 09:44.930
B and C and plug them into the root there in order to solve for X or in our case, T.

09:44.930 --> 09:53.240
And that will give us how long or the time difference until the impact of the bullet and the tank.

09:54.470 --> 09:54.920
Okay.

09:54.920 --> 09:57.980
So I think that's enough food for thought for this video.

09:57.980 --> 10:03.740
What we'll do is we'll come back in the next video and actually put this into code so you can see it

10:03.740 --> 10:06.470
working with the tank and the bullet.
