This is Stevie.
She's here to demonstrate how important an understanding of vectors is for programming movement in games.
Whether you want to move an NPC or turn a character, getting your head around the way they work is
essential to ensuring your game behaves the way you intend.
A 3D character has two vector spaces one that is local and one that is the world.
Each space is defined by the Axis X, Y and Z.
Sometimes these spaces align, but more often than not, they don't.
For example, as shown here, Stevie's local Z axis is pointing in a different direction to the world.
Z.
In unity, the world space orientation is represented by small axis system in the upper right of the
scene window.
To see a 3D models local axes, you can select the character and press the W key to reveal the X, Y
and Z.
A model will not be aligned with the world if it has any values other than zero.
As its rotation movement in any space can be defined by a vector, they are represented by an arrow
that has a direction and a length called the magnitude.
The direction is specified by the number of steps in X, Y and Z.
The vector covers, for example, in 2D.
This vector covers three steps in the X and two steps in the Y, and therefore it is represented by
the direction.
Three to its magnitude is calculated using Pythagoras theorem.
This takes the square root of the addition of both coordinates squared or the square root of x squared
plus y squared.
In this case, that would be the square root of three squared plus two squared, which results in 3.6.
We can then say the vector three two has a magnitude of 3.6.
A vector in three dimensions has the exact same properties with the addition of the z axis value.
It is therefore represented by the steps in x, y and Z.
This vector would be for one to.
Its magnitude would be the square root of four squared plus one squared plus two squared or 4.6.
A vector has no set starting position.
It simply represents a direction.
The same vector can appear anywhere.
This makes them useful for specifying the movement of a game character.
Adding a vector to the position of game character will move them to a new location.
Each of the X, Y and Z values are added to the character's current position in unity.
We can do this with a translate statement.
It is elementary to perform this operation in your head where the new position is the starting position
plus the vector.
When a vector is added to a position, the result is a new position where the X values have been added,
the Y values have been added, and the Z values have been added.
If the same vector is added to two different characters at different locations, each will move by exactly
the same amount, though.
Note they will not end up in the same location.
They are merely adding the same steps to the x, y and z positions.
And since they start in a different position, the result of adding the same vector will be different.
However, because the vectors being used are exactly the same, the characters will move in parallel.
Often when designing an NPC, you'll want the character to move to a goal location.
Even seemingly random wandering still requires some point in the world for the character to aim for.
This requires calculating the vector between the character and its target.
The victor is found by taking the character's position away from the target position.
Once you have the vector, you can use it to move your character towards its target.
If you want a character to move little by little along a vector, then you can't add the entire vector
in one go.
You need to add a much shorter vector that points in the same direction and add it with each update
loop to give the appearance of movement to find a vector that is shorter than a given vector but points
in the same direction, we divide it by some value.
For example, if you want a vector, there's half the size of the original, then you would divide it
by two.
This new vector is coordinates will be the X, Y and Z of the original.
All divided by two.
And although the coordinates of the vector changes, it still faces in the same direction.
But how much should you divide a vector up for a character's movement?
If you divided the vector by four and added the smaller vector each update, the character would take
four updates to reach its goal.
Here's Stevie trying out some different dividers for her goal vector.
You'll notice the smaller divider, the less update Stevie will take to get to Granny.
In addition, this makes her move much faster.
As the dividers increase, the movement becomes more fluid and she covers the distance in a longer time.
If you divide a target vector by a fixed number each time, then the MPC speed will change depending
on the distance.
In this case, you can see that dividing both scenarios by ten will give smaller vectors, but the smaller
vector over the longer distance will be longer than the smaller vectors for the shorter distance.
Here's Stevie putting it into practice.
In both examples, the targeting vector is divided by a thousand.
This gives Stevie 1000 updates to get to her goal as she has a long distance to cover in the same number
of loops in the bottom.
On the bottom she will move much faster.
So rather than dividing all targeting vectors up by the same amount, we want to reduce the smaller
vectors to a standard size such that they are all the same length.
And this standard size is one.
We can find a length, one vector pointing in the same direction as a larger vector by dividing the
larger vector by its magnitude.
This process is called normalizing.
When Stevie uses the normalized vector to update her movement, it doesn't matter how far she has to
travel, she will move with a constant speed.
Unity makes working with points and vectors straightforward via its vector three.
Class A Vector three has an X, Y and z value that represents the vectors coordinates.
If you're familiar with unity, then you'll have already been using vector three properties as the transform
for game objects uses vector three for the position and scale data.
You can work out the vector from a character to a goal by minus one from the other.
This will result in a vector.
It's easy to find the length of this vector using the magnitude property to get the normalized version
you simply use.
Normalized vectors are an extremely powerful concept and there's much to learn.
But now that you have a few tips under your belt with respect to using them to move a character best
we get started on some practical exercises.