WEBVTT

00:03.010 --> 00:09.220
There's a wide misconception that the speed, how fast something is travelling is the same as its velocity.

00:09.220 --> 00:10.360
But it's not.

00:10.390 --> 00:17.110
Velocity is in fact a vector value that not only represents the speed of an object, but also its direction

00:17.110 --> 00:18.130
of travel.

00:18.160 --> 00:25.120
Velocity is a quantity that expresses the rate of change in the position of an object over time.

00:26.110 --> 00:30.790
Velocity is measured by distance, traveled divided by the time of travel.

00:31.060 --> 00:36.640
Mathematically, it's represented as V equals delta d over delta t.

00:36.670 --> 00:39.790
The delta or triangle symbol means change in.

00:40.000 --> 00:47.920
For example, if a ball starts at zero and after one second is at ten, its velocity would be ten meters

00:47.920 --> 00:52.330
per second, and in this case, in a horizontal direction.

00:52.900 --> 01:01.390
If the ball starts at one and after 2 seconds is at 15, then the distance it has travelled is 14 meters

01:01.390 --> 01:04.700
in 2 seconds or seven meters per second.

01:04.720 --> 01:07.660
Also travelling in a horizontal direction.

01:09.310 --> 01:14.470
If you've already been moving objects about in a game world, you've been working with velocity.

01:14.470 --> 01:20.830
In fact, to move something, we must always adjust one or more of its x, y or z values in unity when

01:20.830 --> 01:22.250
we use translate.

01:22.270 --> 01:28.660
This is in fact what we are doing all at once, adding a vector three to the position of the object.

01:28.660 --> 01:30.790
To get a really good feel of how this works.

01:30.790 --> 01:36.250
We're going to program some stuff into unity so that you can experience it.

01:36.250 --> 01:43.480
So attached to this lecture is a unity package, and if you bring that into a new project, you'll see

01:43.630 --> 01:45.730
if you open up the tanks.

01:46.150 --> 01:48.550
Seen that there's two little tanks in here.

01:48.550 --> 01:51.160
Now, we're going to use this scene a little bit later.

01:51.160 --> 01:56.320
But for now, to just have a look at velocity, we're going to create a new scene.

01:56.320 --> 02:03.070
So once you've got that, let's go file new scene and it'll be a 3D scene and into here, we're going

02:03.070 --> 02:05.320
to add in a bullet.

02:05.320 --> 02:10.030
So if you go into prefabs, you're going to find that there's a shell in there.

02:10.030 --> 02:12.460
Just drag that into the scene.

02:12.460 --> 02:17.410
Now you should be able to see it over here in the game view.

02:17.410 --> 02:24.730
If you can't see it in the game view, just select your main camera and go game object, align with

02:24.730 --> 02:29.620
view and then you will be able to see what this scene view is using.

02:29.620 --> 02:31.870
And then you can adjust the camera as you like.

02:31.870 --> 02:38.320
If you want to see what my camera settings are, you can pick them up from this transform here.

02:38.410 --> 02:45.640
Okay, so if we grab hold of that shell, we can actually probably just move it closer to the camera

02:45.640 --> 02:51.040
and just across a little bit and up so that we can actually see it in there.

02:51.070 --> 02:56.020
Now in the scene view, I'm also looking down the positive Z axis.

02:56.020 --> 03:02.650
So to do that you would click on this axis symbol at the top here and click on the Blue Z.

03:02.650 --> 03:04.600
So that's going into the screen.

03:04.900 --> 03:09.970
Okay, so this is your bullet and if we just double click on it, we can get in a little bit closer

03:09.970 --> 03:10.840
to it.

03:10.840 --> 03:16.510
We want to rotate the bullet so that it's actually facing across the screen like that.

03:16.510 --> 03:23.080
Now, if you're not familiar with the settings for it and sometimes orientation in 3D can get quite

03:23.080 --> 03:23.710
confusing.

03:23.710 --> 03:25.990
What I usually do is I just select the shell.

03:25.990 --> 03:29.080
Now the up axis in this case is the y axis.

03:29.080 --> 03:35.680
Now we want to rotate around the Y, so you grab hold of this y axis over in the inspector and you can

03:35.680 --> 03:42.610
just start to sort of turn it like that and you'll see that you're turning either in the correct way

03:42.610 --> 03:43.540
or the wrong way.

03:43.540 --> 03:49.480
Now, for me, I'm turning in apparently the wrong way here, but you'll see over in the camera view

03:49.480 --> 03:52.630
and I am facing across the screen that way.

03:52.630 --> 03:57.670
Now, if we turn 90 degrees, we're going to be at 90 degrees like that.

03:57.880 --> 04:00.850
Let me now just turn this around.

04:00.850 --> 04:03.370
So we're looking at it like that.

04:03.400 --> 04:03.970
Okay.

04:03.970 --> 04:11.410
So another thing that we need to be aware of when we start to move objects around is their orientation.

04:11.440 --> 04:18.640
This bullet, when you click on it and hit the W key, you can see the axis system, the blue axis,

04:18.640 --> 04:25.690
the Z axis is otherwise known as its forward axis, and that's what you want to push it along its forward

04:25.690 --> 04:26.440
axis.

04:26.440 --> 04:29.350
This is in the bullet's local space.

04:29.380 --> 04:35.140
Okay, so the Z axis is going forward across the screen here, but you'll see that we set our Z axis

04:35.140 --> 04:37.000
to go into the screen.

04:37.000 --> 04:40.120
But this is the local coordinates to the bullet.

04:40.120 --> 04:45.490
If you want to see its global coordinates, then you would come at the top where this little cube is

04:45.490 --> 04:47.140
and click on Global.

04:47.140 --> 04:56.380
And now you can see that globally the positive Z axis is going that way and this would be the Y axis

04:56.380 --> 04:57.160
here.

04:57.460 --> 05:05.230
But whenever we use a transform forward or transform translate along the Z axis, then we're actually

05:05.230 --> 05:08.620
moving an object along its own Z axis.

05:08.620 --> 05:15.430
So in this case, we really want to be on local and note the local coordinates system is okay.

05:15.430 --> 05:19.210
So now it's time for a bit of a challenge.

05:19.270 --> 05:24.850
But just before we do, what I'll get you to do is get that shell prefab that you've used now it's already

05:24.850 --> 05:29.260
got some scripts and things on it and we're actually going to remove those.

05:29.260 --> 05:36.670
So in the inspector, just get your destroy shell and remove that component and these rigid bodies,

05:36.670 --> 05:37.960
let's remove that.

05:38.110 --> 05:40.690
The capsule collider will remove that.

05:40.690 --> 05:45.340
And there's another shell script that will also remove from there.

05:45.340 --> 05:50.320
Now the current version of unity will say that it's removed even though you'll still see it.

05:50.320 --> 05:54.310
So you could, if you needed to go, control Z to get those back.

05:54.310 --> 05:57.310
Okay, so let's look at the challenge.

05:57.820 --> 06:04.210
I'd like you to write a script called Move Shell and attach that to the bullet so it moves it across

06:04.210 --> 06:08.110
the screen at one meter per second.

06:08.920 --> 06:09.280
Okay.

06:09.290 --> 06:11.470
So pause the video now, give it a go.

06:11.470 --> 06:14.770
And when we come back, I'll step through the solution with you.

06:15.370 --> 06:15.790
Okay.

06:15.790 --> 06:16.390
Welcome back.

06:16.390 --> 06:17.340
How did you go?

06:17.350 --> 06:19.960
Well, first of all, let's create our script.

06:19.960 --> 06:25.060
So we create a C-sharp script and we'll call it Move Shell.

06:25.120 --> 06:27.580
Now let's open that up.

06:28.120 --> 06:34.270
And in here, we're basically going to push the shell forward with a transform translate.

06:34.270 --> 06:42.370
So we go this dot transform, that translate and we move it along its z axis.

06:42.370 --> 06:48.130
So zero zero and in he will put time dot delta time.

06:48.130 --> 06:51.790
Now this is going to be multiplied by a speed.

06:51.790 --> 06:57.340
Now we wanted one meter per second, so fair enough, we would multiply it by one, which you actually

06:57.340 --> 07:03.970
don't need to do, but let's make a value for speed so you could speed it up if you wanted.

07:03.970 --> 07:14.320
Then up the top we will put in here a float for speed and set that to equal 1.0 f.

07:14.830 --> 07:16.930
Okay, so that's pretty much it.

07:16.930 --> 07:17.470
Save that.

07:17.470 --> 07:25.720
Let's go back into unity and we're going to grab hold of that shell and drag and drop this move shell

07:25.750 --> 07:26.650
onto it.

07:27.310 --> 07:31.090
And then we can press play and have a look at the behavior of the shell.

07:33.500 --> 07:33.860
Okay.

07:33.860 --> 07:42.410
So you've basically set the velocity of this shell to one meter per second for its movement along the

07:42.410 --> 07:43.670
Z axis.

07:43.700 --> 07:52.310
Now, what if you wanted to move at half the speed on the diagonal up this way, then what?

07:52.310 --> 07:54.560
You're moving across the screen there.

07:54.560 --> 07:56.500
So this is your next challenge.

07:56.510 --> 08:05.180
See if you can modify that translate in order to get a diagonal movement at half the speed up to whatever

08:05.180 --> 08:06.740
it is across the bottom.

08:06.770 --> 08:07.760
Pause the video now.

08:07.760 --> 08:08.340
Give that a go.

08:08.360 --> 08:10.580
When we come back, I'll go through it with you.

08:11.930 --> 08:12.640
Okay.

08:12.650 --> 08:13.310
Welcome back.

08:13.310 --> 08:14.300
How did you go with that?

08:14.300 --> 08:21.170
Well, to get it to go up the screen in that particular direction, we're going to modify the Y axis

08:21.170 --> 08:22.880
because that's the up one.

08:22.880 --> 08:25.070
And this is this value here.

08:25.070 --> 08:30.050
So basically, it would be the same as we've got for that.

08:30.350 --> 08:39.230
But we want to multiply that by 0.5 F, which is going to give it half of whatever this speed is so

08:39.230 --> 08:51.290
we can save that, go back into unity and press play on that to now move it and you can see it going

08:51.290 --> 08:53.570
up there on the diagonal.

08:53.570 --> 09:01.150
So this code demonstrates very clearly the difference between speed and velocity.

09:01.160 --> 09:05.510
Speed is just a value or a magnitude.

09:05.510 --> 09:10.670
In this case, we've got it set to one, but velocity is a vector.

09:10.670 --> 09:13.040
This is a velocity.

09:13.040 --> 09:22.880
Velocity has a magnitude and also a direction associated with it, a very important concept to get a

09:22.880 --> 09:23.660
hold of.

09:23.660 --> 09:31.190
When we come back in the next lecture, we're going to look a little further at this velocity and how

09:31.190 --> 09:38.690
we can predict where an object will be in the future, which is often used in AI systems where a character

09:38.690 --> 09:44.990
can predict where the player is going to be based on the player's current speed and direction of travel.
