WEBVTT

00:06.790 --> 00:12.770
Welcome back to part three of this set of tutorials on acceleration.

00:12.790 --> 00:15.910
We're going to start with a little bit of a challenge now.

00:15.910 --> 00:22.780
I said to you in the last video that we are constantly adding acceleration to the speed and therefore

00:22.780 --> 00:30.490
the bullet actually speeds up with a bullet, though it usually starts with a particular acceleration

00:30.490 --> 00:34.450
when it's fired from the gun and then it doesn't have any more propulsion added.

00:34.720 --> 00:37.150
So how would you modify this code?

00:37.150 --> 00:44.140
So it only gets a push at the start of its life and then continues on with that particular speed.

00:44.230 --> 00:46.990
Pause the video now and try that out.

00:46.990 --> 00:49.810
And when you get back, I will show you how to do it.

00:50.290 --> 00:51.070
Welcome back.

00:51.070 --> 00:52.720
How did you go with that challenge?

00:52.720 --> 00:55.360
Well, basically, we have a start method here.

00:55.360 --> 01:00.580
And this Dartmouth is going to run when the bullet is instantiated because the shell script is on the

01:00.580 --> 01:01.270
bullet.

01:01.360 --> 01:09.760
That means we can take this calculation out of here and put it up into here to actually work out what

01:09.760 --> 01:10.660
it's going to be.

01:10.660 --> 01:14.830
Now, the acceleration in this case won't be at time.

01:15.040 --> 01:16.120
Delta time.

01:16.120 --> 01:21.310
You want the acceleration at the beginning and not even at the beginning, because at the beginning

01:21.310 --> 01:22.540
the speed is zero.

01:22.540 --> 01:29.200
So when this starts, we're going to basically have it accelerated after one second.

01:30.310 --> 01:38.410
Now, you might have also realized that previously we had multiplied time delta time by speed twice

01:38.410 --> 01:39.610
with down in here.

01:39.610 --> 01:41.980
But now that we've got rid of that, that's no longer an issue.

01:42.010 --> 01:47.020
If you wanted to leave this code down in here, then you wouldn't need the two lots of time, delta

01:47.020 --> 01:48.130
time that we had before.

01:48.130 --> 01:55.090
Basically now with acceleration set to multiply by one second brings us to the question of what are

01:55.090 --> 01:57.820
the units of force and mass?

01:58.180 --> 02:01.150
Well, in unity it uses metric units.

02:01.150 --> 02:05.380
If you're interested in all these different unit types, you can have a look at this link that's now

02:05.380 --> 02:10.180
on the screen, which will take you to all the standard units that are used in force calculations.

02:10.180 --> 02:12.880
Force is basically Newtons.

02:12.880 --> 02:19.810
So how many Newtons are behind the force, which is the mass and the acceleration units put together?

02:19.810 --> 02:28.630
So you've basically got kilos and multiplied by the acceleration, which is in seconds squared actually.

02:28.900 --> 02:37.030
So up here with our mass and our force that we're initially putting in a mass of ten kilos for a bullet,

02:37.420 --> 02:39.340
that is a cannonball.

02:39.340 --> 02:41.800
Well, maybe not that big, but that's a big bullet.

02:41.800 --> 02:45.850
So let's reduce these down to proper units now.

02:45.850 --> 02:48.370
Now, how much force do you want to put behind it?

02:48.400 --> 02:52.660
If you think about simulating a real bullet, even a kilo is a lot, isn't it?

02:52.660 --> 02:54.280
But a real bull.

02:54.280 --> 02:55.600
You can't see it now.

02:55.600 --> 02:57.220
We can simulate it so we can't see it.

02:57.220 --> 02:58.600
But then there's really no point.

02:58.600 --> 03:02.500
What I'm going to do instead is bring this force down to one.

03:02.500 --> 03:10.750
So basically when we push the bullet out, the speed will end up being one divided by one, which will

03:10.750 --> 03:13.240
give us a speed of one meter per second.

03:13.240 --> 03:17.950
However, don't forget we've got speed set up the top here at three.

03:18.100 --> 03:23.650
I'm going to get rid of it as being a public because we're not setting it in the inspector anyway.

03:23.650 --> 03:29.260
And let's put this down to zero initially because that's what the starting speed will be.

03:29.410 --> 03:29.950
Okay?

03:29.950 --> 03:37.120
So with all of those in force, let's save it and we'll go into unity when we run it.

03:37.120 --> 03:42.310
We'll now have these bullets flying off at one meter per second.

03:42.760 --> 03:44.860
Okay, so let's hit the B key.

03:44.860 --> 03:51.160
And now we can actually see these bullets flying off, living for 3 seconds, which essentially, if

03:51.160 --> 03:56.620
you think about it, they have kind of lived for three meters across there.

03:56.980 --> 04:03.610
So they'll go one meter every second because we're quite zoomed out on the sizes of these things and

04:03.610 --> 04:05.200
those bullets are huge.

04:05.380 --> 04:09.040
So now what are we going to do if you want to speed this up?

04:09.040 --> 04:12.070
Well, of course, you could add a little bit more force to it.

04:12.160 --> 04:19.990
So if we come back into our script, we can come up and make this a force of two, which should give

04:19.990 --> 04:23.440
you twice as much sort of oomph when you start off.

04:23.440 --> 04:24.490
So save that.

04:24.490 --> 04:29.290
Let's come back, press play and have a look at the effect.

04:29.380 --> 04:35.620
We need it to be going decently fast so that we can actually see it slow down.

04:35.650 --> 04:36.820
Okay, well, that's not too bad.

04:36.820 --> 04:40.060
You could make it a bit faster if you wanted.

04:40.270 --> 04:46.630
The next part of simulating this bullet is to actually slow it down so it's going to get slowed down

04:46.630 --> 04:51.040
when it's in the atmosphere due to drag or air friction.

04:51.340 --> 04:54.820
So we need to add in a value for this.

04:54.820 --> 04:57.880
We'll put it up here underneath our other force.

04:57.880 --> 05:04.450
So put float and drag and set that to be, let's say, one in this case.

05:04.450 --> 05:06.010
Now, you might also have notice.

05:06.100 --> 05:08.400
I've increased my mass of my bullet.

05:08.410 --> 05:14.500
That was so I could slow down my simulation so that you can see it, because with my screen recording

05:14.500 --> 05:17.830
software, it keeps changing speeds on me.

05:17.830 --> 05:19.390
And it was really, really slow before.

05:19.390 --> 05:22.810
But since then I actually restarted everything and now it's much faster.

05:22.810 --> 05:26.590
So if you want to see what you're doing, you have to sort of manipulate these values.

05:26.590 --> 05:29.590
But we just want to see the actual effect of drag.

05:29.590 --> 05:35.080
So if we come down into our late update, we're going to update speed in here.

05:35.080 --> 05:45.730
So speed and this is multiplied by equals and it's going to be one minus the time dot delta time.

05:47.540 --> 05:50.570
Multiplied by drag.

05:50.960 --> 05:54.760
And of course, then you can also increase that drag amount.

05:55.040 --> 06:00.140
So with that in there, let's save it and have a look at how that slows down the bullet.

06:00.260 --> 06:01.910
So press play.

06:02.940 --> 06:04.370
And let's shoot a bullet.

06:04.380 --> 06:08.400
Now you can see that it comes out and it slows down.

06:10.750 --> 06:14.260
So that's some pretty thick air that it's trying to travel through.

06:14.290 --> 06:20.050
The next thing we need to add is some gravity because the bullet isn't going to sort of get to that

06:20.110 --> 06:20.830
point and stop.

06:20.830 --> 06:26.920
It needs to sort of, as it's coming out, create an arc because it's gravity on earth that creates

06:26.920 --> 06:28.330
that arc for us.

06:28.900 --> 06:34.660
So back into our shell, we're going to come up to the top and add in a force for gravity.

06:34.750 --> 06:36.430
So we'll put float.

06:36.580 --> 06:43.990
Gravity and gravity is -9.8 meters per second squared on Earth.

06:43.990 --> 06:46.280
So we'll actually use that value in here.

06:46.300 --> 06:55.030
Make sure you put an F now we'll need a float for gravity acceleration, which is basically a deceleration,

06:55.510 --> 06:56.790
which we'll calculate.

06:56.800 --> 06:59.170
And we also need another speed.

06:59.170 --> 07:05.800
So at the top we will put float and this will be our y speed for moving in the Y direction and make

07:05.800 --> 07:07.650
that zero f.

07:07.660 --> 07:08.140
Okay.

07:08.140 --> 07:14.770
So now we need to calculate the actual deceleration or the gravity acceleration.

07:14.770 --> 07:29.230
So in here after this, I can put in G Excel equals gravity, which is f force divided by mass again.

07:29.470 --> 07:35.380
And then in our late update we can add our gravity factor to our speed.

07:35.530 --> 07:47.620
Why speed plus equals g excel multiplied by time delta time.

07:48.940 --> 07:53.680
And then that value can go in here as our y speed.

07:55.300 --> 07:57.010
All right, so we've got that in place.

07:57.010 --> 07:59.260
Let's now save it and try it out.

07:59.260 --> 08:01.150
So come back into unity.

08:01.420 --> 08:07.750
Now, if you want the bullets to be destroyed when they hit the ground, you could tag the ground which

08:07.750 --> 08:08.410
is there.

08:08.410 --> 08:09.790
It's got a collider on it.

08:09.790 --> 08:17.860
So we could actually tag it as tank or you could tag it as ground and put that in your code as one of

08:17.860 --> 08:19.450
the conditions for doing that.

08:19.480 --> 08:24.250
It doesn't matter which way you do it, but for us right now, it doesn't really matter because we're

08:24.250 --> 08:27.270
not going to continue using this project like that.

08:27.280 --> 08:27.670
Okay.

08:27.670 --> 08:28.930
So I'm going to lift up my turret.

08:28.930 --> 08:36.880
Now I'm running and I'm going to hit the B key and you'll see those bullets are now coming out and nicely

08:36.970 --> 08:39.310
going down with gravity.

08:39.310 --> 08:43.990
And if I change the angle now, they're coming out.

08:43.990 --> 08:49.540
And actually because of the gravity hitting that box collider around the tank at that particular angle

08:49.540 --> 08:52.810
that I had it as, okay, great.

08:52.810 --> 08:58.660
So at this point you could give them a little bit more force depending on how they're flying for you.

08:58.660 --> 09:05.560
So starting force, if I come up to the top, I might just increase this by two or you could increase

09:05.560 --> 09:09.040
the drag if you want to slow it down or decrease the drag if you want to speed it up.

09:09.310 --> 09:11.650
There's all these sorts of forces involved.

09:11.830 --> 09:16.720
And let's play again with those values just to see what it looks like on my tank.

09:20.840 --> 09:22.760
So I've got it to go a little bit further.

09:22.940 --> 09:28.160
At this point, you probably want to turn the bullet so it faces in the direction it's going.

09:28.160 --> 09:32.690
But believe me, you don't want to do it with the code that we've got because of the way that we're

09:32.690 --> 09:36.380
actually doing it, because we're pushing the bullet along.

09:36.380 --> 09:39.650
It's basically its up axis and its axis here.

09:39.650 --> 09:44.780
If we try and turn the bullet, then it will affect this translate line here.

09:44.780 --> 09:51.230
And because the bullet can be looking in any direction, we get all confused and a lot of complicated

09:51.230 --> 09:56.030
maths to do with local positions and world positions and things like that.

09:56.030 --> 09:58.070
So we're not actually going to do it in here.

09:58.070 --> 10:03.260
Instead, what we're going to do is come back in the next lecture and look at some other ways to do

10:03.260 --> 10:09.020
trajectories, because this was just to make you aware of all the mathematic elements that are involved.
