WEBVTT

00:04.130 --> 00:05.200
This is Stevie.

00:05.210 --> 00:10.850
She's here to demonstrate how important an understanding of vectors is for programming movement in games.

00:11.210 --> 00:16.820
Whether you want to move an NPC or turn a character, getting your head around the way they work is

00:16.820 --> 00:20.270
essential to ensuring your game behaves the way you intend.

00:20.300 --> 00:25.730
A 3D character has two vector spaces one that is local and one that is the world.

00:26.300 --> 00:30.890
Each space is defined by the Axis X, Y and Z.

00:31.310 --> 00:35.810
Sometimes these spaces align, but more often than not, they don't.

00:35.840 --> 00:41.930
For example, as shown here, Stevie's local Z axis is pointing in a different direction to the world.

00:41.930 --> 00:42.510
Z.

00:42.530 --> 00:48.050
In unity, the world space orientation is represented by small axis system in the upper right of the

00:48.050 --> 00:49.070
scene window.

00:49.160 --> 00:55.940
To see a 3D models local axes, you can select the character and press the W key to reveal the X, Y

00:55.940 --> 00:56.660
and Z.

00:57.460 --> 01:01.840
A model will not be aligned with the world if it has any values other than zero.

01:01.840 --> 01:08.560
As its rotation movement in any space can be defined by a vector, they are represented by an arrow

01:08.560 --> 01:11.860
that has a direction and a length called the magnitude.

01:12.580 --> 01:17.200
The direction is specified by the number of steps in X, Y and Z.

01:17.230 --> 01:20.750
The vector covers, for example, in 2D.

01:20.770 --> 01:27.430
This vector covers three steps in the X and two steps in the Y, and therefore it is represented by

01:27.430 --> 01:28.240
the direction.

01:28.240 --> 01:33.520
Three to its magnitude is calculated using Pythagoras theorem.

01:33.790 --> 01:40.390
This takes the square root of the addition of both coordinates squared or the square root of x squared

01:40.390 --> 01:41.830
plus y squared.

01:42.280 --> 01:49.510
In this case, that would be the square root of three squared plus two squared, which results in 3.6.

01:50.260 --> 01:56.050
We can then say the vector three two has a magnitude of 3.6.

01:57.540 --> 02:03.180
A vector in three dimensions has the exact same properties with the addition of the z axis value.

02:03.720 --> 02:07.770
It is therefore represented by the steps in x, y and Z.

02:07.920 --> 02:11.670
This vector would be for one to.

02:12.340 --> 02:19.720
Its magnitude would be the square root of four squared plus one squared plus two squared or 4.6.

02:20.200 --> 02:22.390
A vector has no set starting position.

02:22.390 --> 02:24.460
It simply represents a direction.

02:24.460 --> 02:26.560
The same vector can appear anywhere.

02:26.770 --> 02:30.430
This makes them useful for specifying the movement of a game character.

02:30.730 --> 02:35.080
Adding a vector to the position of game character will move them to a new location.

02:35.080 --> 02:40.720
Each of the X, Y and Z values are added to the character's current position in unity.

02:40.720 --> 02:43.150
We can do this with a translate statement.

02:44.010 --> 02:49.500
It is elementary to perform this operation in your head where the new position is the starting position

02:49.500 --> 02:50.640
plus the vector.

02:50.940 --> 02:57.300
When a vector is added to a position, the result is a new position where the X values have been added,

02:57.300 --> 03:01.020
the Y values have been added, and the Z values have been added.

03:01.080 --> 03:06.900
If the same vector is added to two different characters at different locations, each will move by exactly

03:06.900 --> 03:08.250
the same amount, though.

03:08.250 --> 03:10.830
Note they will not end up in the same location.

03:10.830 --> 03:15.240
They are merely adding the same steps to the x, y and z positions.

03:15.240 --> 03:20.820
And since they start in a different position, the result of adding the same vector will be different.

03:20.940 --> 03:26.340
However, because the vectors being used are exactly the same, the characters will move in parallel.

03:27.210 --> 03:31.950
Often when designing an NPC, you'll want the character to move to a goal location.

03:32.160 --> 03:37.770
Even seemingly random wandering still requires some point in the world for the character to aim for.

03:38.620 --> 03:42.700
This requires calculating the vector between the character and its target.

03:43.610 --> 03:48.280
The victor is found by taking the character's position away from the target position.

03:48.290 --> 03:52.520
Once you have the vector, you can use it to move your character towards its target.

03:52.640 --> 03:58.100
If you want a character to move little by little along a vector, then you can't add the entire vector

03:58.100 --> 03:59.270
in one go.

03:59.330 --> 04:04.940
You need to add a much shorter vector that points in the same direction and add it with each update

04:04.940 --> 04:10.550
loop to give the appearance of movement to find a vector that is shorter than a given vector but points

04:10.550 --> 04:13.430
in the same direction, we divide it by some value.

04:13.460 --> 04:18.110
For example, if you want a vector, there's half the size of the original, then you would divide it

04:18.110 --> 04:18.980
by two.

04:19.310 --> 04:23.030
This new vector is coordinates will be the X, Y and Z of the original.

04:23.060 --> 04:25.010
All divided by two.

04:25.010 --> 04:30.710
And although the coordinates of the vector changes, it still faces in the same direction.

04:30.800 --> 04:35.270
But how much should you divide a vector up for a character's movement?

04:35.300 --> 04:40.370
If you divided the vector by four and added the smaller vector each update, the character would take

04:40.370 --> 04:42.140
four updates to reach its goal.

04:42.170 --> 04:45.800
Here's Stevie trying out some different dividers for her goal vector.

04:45.980 --> 04:50.450
You'll notice the smaller divider, the less update Stevie will take to get to Granny.

04:50.660 --> 04:53.870
In addition, this makes her move much faster.

04:54.170 --> 04:59.940
As the dividers increase, the movement becomes more fluid and she covers the distance in a longer time.

04:59.960 --> 05:05.990
If you divide a target vector by a fixed number each time, then the MPC speed will change depending

05:05.990 --> 05:07.250
on the distance.

05:07.280 --> 05:13.760
In this case, you can see that dividing both scenarios by ten will give smaller vectors, but the smaller

05:13.760 --> 05:19.070
vector over the longer distance will be longer than the smaller vectors for the shorter distance.

05:19.100 --> 05:21.400
Here's Stevie putting it into practice.

05:21.410 --> 05:25.080
In both examples, the targeting vector is divided by a thousand.

05:25.100 --> 05:31.700
This gives Stevie 1000 updates to get to her goal as she has a long distance to cover in the same number

05:31.700 --> 05:33.230
of loops in the bottom.

05:33.620 --> 05:36.110
On the bottom she will move much faster.

05:36.350 --> 05:41.780
So rather than dividing all targeting vectors up by the same amount, we want to reduce the smaller

05:41.780 --> 05:45.500
vectors to a standard size such that they are all the same length.

05:45.500 --> 05:47.660
And this standard size is one.

05:48.020 --> 05:53.540
We can find a length, one vector pointing in the same direction as a larger vector by dividing the

05:53.540 --> 05:55.760
larger vector by its magnitude.

05:55.880 --> 05:58.460
This process is called normalizing.

05:58.460 --> 06:03.620
When Stevie uses the normalized vector to update her movement, it doesn't matter how far she has to

06:03.620 --> 06:06.180
travel, she will move with a constant speed.

06:06.200 --> 06:10.190
Unity makes working with points and vectors straightforward via its vector three.

06:10.190 --> 06:15.440
Class A Vector three has an X, Y and z value that represents the vectors coordinates.

06:15.890 --> 06:20.870
If you're familiar with unity, then you'll have already been using vector three properties as the transform

06:20.870 --> 06:25.010
for game objects uses vector three for the position and scale data.

06:25.340 --> 06:29.720
You can work out the vector from a character to a goal by minus one from the other.

06:29.840 --> 06:31.720
This will result in a vector.

06:31.730 --> 06:37.510
It's easy to find the length of this vector using the magnitude property to get the normalized version

06:37.520 --> 06:38.810
you simply use.

06:38.810 --> 06:43.190
Normalized vectors are an extremely powerful concept and there's much to learn.

06:43.190 --> 06:48.470
But now that you have a few tips under your belt with respect to using them to move a character best

06:48.470 --> 06:51.080
we get started on some practical exercises.
