WEBVTT

00:01.590 --> 00:06.090
Vectors themselves when written as coordinates look very much the same as points.

00:06.090 --> 00:07.710
However, they are different.

00:07.740 --> 00:15.180
The .45 is a single location for along the x axis and five along the y measured from the origin.

00:15.210 --> 00:21.750
The vector for five, however, can start from anywhere in space, but measure four in the x and five

00:21.750 --> 00:22.740
in the Y.

00:23.460 --> 00:25.750
Whereas a point is a single location.

00:25.770 --> 00:28.140
A vector can be anywhere in space.

00:28.530 --> 00:35.490
A vector also represents a direction the way it is pointing, and a magnitude held in the value of its

00:35.490 --> 00:36.150
length.

00:36.480 --> 00:42.570
This is very useful as it not only tells you which way it is going, but how far it is going.

00:42.900 --> 00:50.970
On a graph, the point for five is here a vector that starts at the origin and also has a value of four

00:50.970 --> 00:51.640
or five.

00:51.660 --> 00:56.670
Is an arrow drawn from the origin to the point of the same value?

00:56.940 --> 01:00.330
But we could also easily draw the vector for five.

01:00.360 --> 01:03.330
Here, here and here.

01:04.660 --> 01:07.560
It's just a direction and magnitude in space.

01:07.570 --> 01:10.210
It doesn't specify where it starts.

01:11.310 --> 01:17.610
The magnitude of a vector is determined by Pythagoras theorem using its x and y values.

01:18.030 --> 01:21.150
Do you think you can work out the length of this vector?

01:21.150 --> 01:23.640
Pause the video and give it a go?

01:24.270 --> 01:32.790
Recall Pythagoras theorem where the length is equal to the square root of x squared plus y squared.

01:32.790 --> 01:37.590
In our previous examples, we've been finding the distance between two objects.

01:37.710 --> 01:43.950
A vector in this case is essentially the same as it would be positioned at the origin.

01:43.950 --> 01:51.030
So you could consider its first point to be zero zero and its ending point to be its value.

01:51.030 --> 01:58.650
So for five in this example now because we have a zero value in one of our coordinates, that means

01:58.650 --> 02:02.580
that we can get rid of that out of our original Pythagoras equation.

02:02.580 --> 02:08.520
And all we need to do is X squared plus Y squared and then find the square root.

02:08.820 --> 02:14.580
Now you'll also notice that in this equation that I've written here, I've got V, which is the name

02:14.580 --> 02:19.240
of our vector surrounded by double pipes on both sides.

02:19.260 --> 02:25.110
This is the mathematical notation for the length of a vector.

02:25.110 --> 02:33.660
So this equation says that the length of the vector V equals the square root of its x value squared

02:33.660 --> 02:35.760
plus its y value squared.

02:36.760 --> 02:42.730
So substituting the values of our vector into this equation gives us the square root of four squared

02:42.730 --> 02:51.280
plus five squared, or the square root of 16 plus 25, which is the square root of 41, and that results

02:51.280 --> 02:53.290
in a value of 6.4.

02:53.290 --> 02:59.170
So the magnitude of this vector four five is 6.4.

02:59.650 --> 03:06.370
Once we know where a vector starts, we can calculate the point where it ends by adding the starting

03:06.370 --> 03:09.490
point coordinates to the vector coordinates.

03:09.640 --> 03:16.600
So if we start at zero zero and travel along the vector for five, the destination will be zero zero

03:16.600 --> 03:19.930
+45, which gives us four or five.

03:20.050 --> 03:29.530
If we start at the .71 and travel along the same vector, we end up at seven one +45, which will give

03:29.530 --> 03:33.850
us our destination point of 11 six.

03:34.420 --> 03:40.840
Again, a point is the specific location in space, whereas a vector represents instructions for moving

03:40.840 --> 03:42.970
from one location to the next.

03:43.150 --> 03:50.800
In the previous example, the vectors used to add to both zero zero and seven one are exactly the same.

03:50.980 --> 03:58.390
You cannot determine if two vectors are the same by their location in space, only their direction and

03:58.390 --> 03:59.200
length.

03:59.650 --> 04:02.290
So where do we use vectors in games?

04:02.290 --> 04:04.510
Well, pretty much everywhere.

04:04.510 --> 04:10.510
We're now going to investigate vectors and for that we're going to use a starter package that's attached

04:10.510 --> 04:11.440
to this lecture.

04:11.440 --> 04:17.860
So create a new Unity Project 3D one and bring in the starter package.

04:17.860 --> 04:25.390
When you open up the scene called Moving, you're going to find inside a pink pig, courtesy of easy,

04:25.390 --> 04:30.010
primitive people by bit gaming, which is available in the asset store for free.

04:30.250 --> 04:36.970
And he's got a whole bunch of great characters and a purple plane and this little cube thing.

04:36.970 --> 04:41.920
So the cubes were going to use as a goal location a little later on.

04:41.920 --> 04:46.450
Now the pig, if you have a look at the pig and click it in the hierarchy, you'll see that it has a

04:46.450 --> 04:49.000
move script attached to it.

04:49.000 --> 04:56.440
If we open up that move script, you'll find that there is a goal where you can pass through to this

04:56.440 --> 04:59.530
that brick object or the goal location.

04:59.530 --> 05:11.200
And then in the start we're actually setting the transform translate to goal, dot transform position.

05:11.200 --> 05:15.850
Let's press play and have a look at the result and then we'll talk about what we're getting.

05:15.850 --> 05:23.710
So on play the pig actually jumps to the location of the goal.

05:24.460 --> 05:31.390
Now if we have a look at where that pig is situated, it's at X of zero and Z of zero and pretty much

05:31.390 --> 05:34.180
zero in the Y, even though I've got to lift it up a little bit.

05:34.180 --> 05:36.520
So it sits above that ground plane.

05:36.640 --> 05:41.860
Grab that pig and let's just move it over here.

05:41.890 --> 05:43.810
Okay, now press play.

05:43.810 --> 05:48.910
Will the pig go to the location of that cube?

05:48.940 --> 05:49.350
Okay.

05:49.420 --> 05:50.200
No.

05:50.440 --> 05:52.570
So what's happening here?

05:53.320 --> 05:57.670
The original location of the pig is its current position.

05:57.670 --> 06:04.480
And we're adding or translating it using a vector, which happens to be equal to the goal location.

06:04.480 --> 06:10.960
Now, the goal location, its vector at the moment is let's just round this off, so let's make this

06:11.110 --> 06:15.880
for and we will make that seven just makes it a little bit easier.

06:15.880 --> 06:26.680
So we've got a vector of 407 being added to the pigs location that is going to move it by four in the

06:26.680 --> 06:33.130
X direction and seven in the Z direction, which doesn't put it where that brick is.

06:33.580 --> 06:37.090
It just puts it at the location that you saw.

06:37.090 --> 06:46.990
So if we press play, you'll see it that the difference between here and here is a vector of 407.

06:47.260 --> 06:49.360
Now for zero seven.

06:49.360 --> 06:55.390
If we add that to the pig, when the pig is at zero zero in the X and the Z.

06:55.390 --> 07:03.370
So if we set this to zero and zero and press play and add the four and the seven, then it actually

07:03.370 --> 07:12.850
will position the pig where the goal location is because the vector from the origin to the actual location

07:12.850 --> 07:17.980
of the goal is the same as the position of the goal.

07:18.700 --> 07:24.280
To better see this, let's just modify our move script and forget about the goal.

07:24.280 --> 07:26.950
For now, let's change this down in here.

07:26.950 --> 07:31.480
We're going to translate it by and let's make it something completely different.

07:31.480 --> 07:36.070
So 602 Now I'm not translating.

07:36.220 --> 07:39.460
In the Y because the Y is going to give you an up and down movement.

07:39.460 --> 07:42.760
We just want to move it along that particular ground plane.

07:42.880 --> 07:43.390
Okay.

07:43.390 --> 07:44.590
So just save that.

07:44.590 --> 07:50.980
If we go back into unity, what I'd like you to do is to get the pig and duplicate it.

07:50.980 --> 07:56.950
So if we select it and go control D, we've got a second pig and we'll actually move that second pig

07:56.950 --> 07:59.410
just somewhere else over here.

07:59.770 --> 08:06.970
Okay, so now when we press play, we'll get both pigs and they're going to move by the exactly the

08:06.970 --> 08:08.260
same vector.

08:08.260 --> 08:10.390
But notice they're not in the same place.

08:10.420 --> 08:10.800
Okay?

08:10.810 --> 08:17.410
Because remember, a vector does not have any particular location associated with it, but it can be

08:17.410 --> 08:19.300
added to a starting point.

08:19.300 --> 08:26.200
So in this case, these pigs have moved parallel to each other using exactly the same vector, which

08:26.200 --> 08:29.860
has nothing to do at all with our gold point.

08:30.040 --> 08:35.290
And if these vectors look just slightly, a little bit off and not exactly the same in the movement,

08:35.290 --> 08:40.300
it's because we've got a perspective view here which is going to distort things.

08:40.300 --> 08:47.500
If you really wanted to see it more accurately, you could go to a top down view just here and then

08:47.500 --> 08:53.770
have a look at the vectors and movement over here and you'll notice that they will be perfectly parallel

08:53.770 --> 08:54.790
to each other.

08:55.120 --> 09:01.810
Okay, so that's adding a vector to a position which in this case is the starting position of the pig.

09:01.810 --> 09:08.290
When we come back in the next video, we're going to look at getting the pig to move along the vector

09:08.290 --> 09:14.500
smoothly rather than jumping from the current position to the end of the added vector.
