WEBVTT

00:03.840 --> 00:07.850
Just as velocity was the rate of change in position over time.

00:07.860 --> 00:14.100
Acceleration is the rate of change of velocity over time, and it is represented like this.

00:14.580 --> 00:21.600
Acceleration is used quite a lot in physics systems as objects in real life don't move with constant

00:21.600 --> 00:26.430
velocity, at least not if you have to slow them down or speed them up.

00:26.430 --> 00:30.720
Any collisions that occur will involve changes in acceleration.

00:31.020 --> 00:38.130
Anything being simulated in the real world will also constantly be under the effect of the most famous

00:38.130 --> 00:40.830
of the known accelerators called gravity.

00:41.220 --> 00:44.580
So acceleration is quite difficult to escape.

00:45.510 --> 00:50.460
In 1687, Sir Isaac Newton published his famous Three Laws of Motion.

00:50.460 --> 00:57.570
The second and more prolifically remembered is F equals MA or force equals mass times acceleration.

00:57.750 --> 01:03.840
And it's this equation we are going to put into practice with our tanks and shells to get a real feeling

01:03.840 --> 01:05.190
for how they work.

01:06.000 --> 01:13.500
So to begin with, we're going to go back into our drive script and modify it back so you can drive

01:13.500 --> 01:21.750
the tank around, but also so that you can actually lift up the turret with some keys to rotate it like

01:21.750 --> 01:22.230
that.

01:22.230 --> 01:23.490
Okay, so let's do that.

01:23.490 --> 01:26.130
Open up your drive script.

01:26.130 --> 01:30.510
Now, this is how it was last time we used it where it was self driving.

01:30.510 --> 01:33.180
We're going to get rid of the self driving code.

01:33.180 --> 01:38.880
I'm just going to comment that out and I'll put these two translates and rotates back in.

01:38.880 --> 01:43.620
We also need a link to the turret of the tank so that we can move it around.

01:43.620 --> 01:49.950
So public game object and we'll just bring in the transform for that.

01:49.950 --> 01:54.930
So that'll be the trans gun like that.

01:54.930 --> 02:02.250
Then down inside of our update will use the T and G keys because they won't interfere with the get access

02:02.250 --> 02:03.270
code up here.

02:03.540 --> 02:20.520
So if input dot get key key code dot t, then what we're going to do is go trans gun dot rotate around.

02:23.800 --> 02:31.360
Oh, and I meant to actually make this a transform up here, not a game object that allows to get straight

02:31.360 --> 02:33.310
into the rotate around.

02:33.310 --> 02:36.010
So rotate it around its position.

02:36.010 --> 02:46.600
So trans gun dot position will rotate around the axis of trans gun dot right.

02:47.110 --> 02:52.270
And we're going to rotate it by minus two degrees.

02:53.590 --> 02:53.980
Okay.

02:53.980 --> 03:00.580
Now we'll come down and we'll put the same in for else if the G key.

03:00.610 --> 03:03.910
So just grab a copy of that and put it in here.

03:06.770 --> 03:12.740
Make this into the G key and then make it rotate in the other direction.

03:12.780 --> 03:17.330
Okay, so save that code, switch back into unity.

03:17.360 --> 03:24.200
What we're going to do now is come to our enemy game object with the drive script.

03:24.230 --> 03:26.390
Now you've got trans guns sitting in here.

03:26.570 --> 03:33.500
Drop down the tank renderers and find the turret and actually put that down into that position.

03:33.530 --> 03:33.950
Okay.

03:33.950 --> 03:41.480
So if we press play at this point, we'll be able to move that turret up and down with the T and the

03:41.480 --> 03:42.980
GCS like that.

03:44.450 --> 03:46.370
Now attached to the turret.

03:46.370 --> 03:50.660
We want to add in the point where our bullets are going to spawn from.

03:50.990 --> 03:54.920
So we'll right click on it and create an empty.

03:55.010 --> 03:59.180
Now that's going to let's be called turret.

03:59.210 --> 04:00.470
You can call it gun or something.

04:00.480 --> 04:00.980
It doesn't matter.

04:00.980 --> 04:02.060
It's just a marker.

04:02.150 --> 04:06.680
Hit the key so you can see where it is over in the same view.

04:07.100 --> 04:13.880
I want to move around a little bit, so we're going to lift it up and we're going to bring it to the

04:13.880 --> 04:20.270
end and maybe just out a little bit, because that's where we want our bullets to spawn.

04:20.480 --> 04:23.600
And then you want to actually rotate it.

04:23.600 --> 04:29.000
So we're going to hit the E key and then rotate it up a little bit.

04:29.000 --> 04:34.580
And if you're wondering how to align it, probably get this ring that's going around the middle here

04:34.580 --> 04:43.760
for Y rotation to kind of align with the actual tank, which will then help make sure you only rotate

04:43.760 --> 04:49.820
it on the red axis because you want to rotate it around the x.

04:50.270 --> 04:54.860
So this is going to be the spawn point for your bullets and you can adjust that later if you want.

04:54.890 --> 04:59.500
We'll go back into the drive code and actually bring that through.

04:59.510 --> 05:07.220
So up here we'll put in public transform and let's call it the gun.

05:08.240 --> 05:08.820
Okay.

05:09.440 --> 05:14.730
Then we can actually spawn a bullet from the gun position.

05:14.760 --> 05:15.040
Okay.

05:15.050 --> 05:20.060
So we're going to do this in the same way as our other tank does it.

05:20.060 --> 05:22.850
This is a starting challenge for you.

05:23.060 --> 05:31.730
What I'd like you to do is program in the B key so that the red tank fires a bullet.

05:31.730 --> 05:32.750
So pause the video.

05:32.750 --> 05:33.620
Now go into that.

05:33.620 --> 05:35.870
And when we come back, I'll step through it with you.

05:36.440 --> 05:37.090
Welcome back.

05:37.100 --> 05:38.510
How did you go putting that together?

05:38.510 --> 05:42.680
Well, we need to do this in the drive script, so we'll open that up.

05:42.770 --> 05:51.110
Now at the top, we have a transform for the gun, the transform for actually moving the turret bit,

05:51.110 --> 05:52.640
which was up here.

05:52.730 --> 05:56.540
And what we're going to add underneath that is to bring in the bullet.

05:56.540 --> 06:01.490
So we need a public game object bullet and we'll call that the bullet.

06:01.490 --> 06:07.700
OBJ Now down in our update, we've got other key codes coming in.

06:07.700 --> 06:08.990
We're going to put an else in here.

06:08.990 --> 06:19.220
So else if input dot get key down and we're using key down so that we only get like one bullet.

06:19.340 --> 06:25.340
Where is key get key actually will hold it down and you can keep rotating the turret.

06:25.340 --> 06:28.100
But in this case, if you do that, you're just going to get a whole stream of bullets which we don't

06:28.100 --> 06:28.610
want.

06:28.610 --> 06:40.340
So key code B, so if that, then we're going to instantiate a bullet object at the position of our

06:40.340 --> 06:40.820
gun.

06:40.820 --> 06:45.140
So gun position and gun rotation.

06:45.140 --> 06:48.380
So gun rotation.

06:48.380 --> 06:53.360
Now the bullet still has its own code sitting on it to project it forward.

06:53.360 --> 06:59.150
So just save this and we'll go back into unity from here.

06:59.150 --> 07:04.250
We're going to find our shell prefab, which is up here in my top assets folder.

07:04.250 --> 07:05.780
Select the enemy.

07:05.780 --> 07:10.250
We want the shell to be the bullet object there.

07:10.310 --> 07:14.420
And then you want the gun, which is our little turret we put on the end.

07:14.420 --> 07:20.120
This one here to go into the gun like that.

07:20.390 --> 07:22.370
Okay, so we can test this out now.

07:22.370 --> 07:25.370
So let's press play now.

07:25.370 --> 07:29.950
Don't forget, you can use your T and g keys to use.

07:29.990 --> 07:33.650
Move your turret and then we hit the B key and we'll get a bullet.

07:33.650 --> 07:41.360
Now notice how it fires wherever we're actually facing because we're using the transform that we've

07:41.360 --> 07:42.980
got sitting on the end.

07:43.850 --> 07:50.750
Now, these bullets are going to go forever and ever and ever off into space, so we won't keep firing.

07:50.750 --> 07:53.090
Otherwise we'll overload the system.

07:53.090 --> 07:59.060
But instead what we'll do when we come back in the next video is deal with destroying them, as well

07:59.060 --> 08:03.830
as look at putting some gravity and acceleration onto them.
