Vectors themselves when written as coordinates look very much the same as points.
However, they are different.
The .45 is a single location for along the x axis and five along the y measured from the origin.
The vector for five, however, can start from anywhere in space, but measure four in the x and five
in the Y.
Whereas a point is a single location.
A vector can be anywhere in space.
A vector also represents a direction the way it is pointing, and a magnitude held in the value of its
length.
This is very useful as it not only tells you which way it is going, but how far it is going.
On a graph, the point for five is here a vector that starts at the origin and also has a value of four
or five.
Is an arrow drawn from the origin to the point of the same value?
But we could also easily draw the vector for five.
Here, here and here.
It's just a direction and magnitude in space.
It doesn't specify where it starts.
The magnitude of a vector is determined by Pythagoras theorem using its x and y values.
Do you think you can work out the length of this vector?
Pause the video and give it a go?
Recall Pythagoras theorem where the length is equal to the square root of x squared plus y squared.
In our previous examples, we've been finding the distance between two objects.
A vector in this case is essentially the same as it would be positioned at the origin.
So you could consider its first point to be zero zero and its ending point to be its value.
So for five in this example now because we have a zero value in one of our coordinates, that means
that we can get rid of that out of our original Pythagoras equation.
And all we need to do is X squared plus Y squared and then find the square root.
Now you'll also notice that in this equation that I've written here, I've got V, which is the name
of our vector surrounded by double pipes on both sides.
This is the mathematical notation for the length of a vector.
So this equation says that the length of the vector V equals the square root of its x value squared
plus its y value squared.
So substituting the values of our vector into this equation gives us the square root of four squared
plus five squared, or the square root of 16 plus 25, which is the square root of 41, and that results
in a value of 6.4.
So the magnitude of this vector four five is 6.4.
Once we know where a vector starts, we can calculate the point where it ends by adding the starting
point coordinates to the vector coordinates.
So if we start at zero zero and travel along the vector for five, the destination will be zero zero
+45, which gives us four or five.
If we start at the .71 and travel along the same vector, we end up at seven one +45, which will give
us our destination point of 11 six.
Again, a point is the specific location in space, whereas a vector represents instructions for moving
from one location to the next.
In the previous example, the vectors used to add to both zero zero and seven one are exactly the same.
You cannot determine if two vectors are the same by their location in space, only their direction and
length.
So where do we use vectors in games?
Well, pretty much everywhere.
We're now going to investigate vectors and for that we're going to use a starter package that's attached
to this lecture.
So create a new Unity Project 3D one and bring in the starter package.
When you open up the scene called Moving, you're going to find inside a pink pig, courtesy of easy,
primitive people by bit gaming, which is available in the asset store for free.
And he's got a whole bunch of great characters and a purple plane and this little cube thing.
So the cubes were going to use as a goal location a little later on.
Now the pig, if you have a look at the pig and click it in the hierarchy, you'll see that it has a
move script attached to it.
If we open up that move script, you'll find that there is a goal where you can pass through to this
that brick object or the goal location.
And then in the start we're actually setting the transform translate to goal, dot transform position.
Let's press play and have a look at the result and then we'll talk about what we're getting.
So on play the pig actually jumps to the location of the goal.
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
zero in the Y, even though I've got to lift it up a little bit.
So it sits above that ground plane.
Grab that pig and let's just move it over here.
Okay, now press play.
Will the pig go to the location of that cube?
Okay.
No.
So what's happening here?
The original location of the pig is its current position.
And we're adding or translating it using a vector, which happens to be equal to the goal location.
Now, the goal location, its vector at the moment is let's just round this off, so let's make this
for and we will make that seven just makes it a little bit easier.
So we've got a vector of 407 being added to the pigs location that is going to move it by four in the
X direction and seven in the Z direction, which doesn't put it where that brick is.
It just puts it at the location that you saw.
So if we press play, you'll see it that the difference between here and here is a vector of 407.
Now for zero seven.
If we add that to the pig, when the pig is at zero zero in the X and the Z.
So if we set this to zero and zero and press play and add the four and the seven, then it actually
will position the pig where the goal location is because the vector from the origin to the actual location
of the goal is the same as the position of the goal.
To better see this, let's just modify our move script and forget about the goal.
For now, let's change this down in here.
We're going to translate it by and let's make it something completely different.
So 602 Now I'm not translating.
In the Y because the Y is going to give you an up and down movement.
We just want to move it along that particular ground plane.
Okay.
So just save that.
If we go back into unity, what I'd like you to do is to get the pig and duplicate it.
So if we select it and go control D, we've got a second pig and we'll actually move that second pig
just somewhere else over here.
Okay, so now when we press play, we'll get both pigs and they're going to move by the exactly the
same vector.
But notice they're not in the same place.
Okay?
Because remember, a vector does not have any particular location associated with it, but it can be
added to a starting point.
So in this case, these pigs have moved parallel to each other using exactly the same vector, which
has nothing to do at all with our gold point.
And if these vectors look just slightly, a little bit off and not exactly the same in the movement,
it's because we've got a perspective view here which is going to distort things.
If you really wanted to see it more accurately, you could go to a top down view just here and then
have a look at the vectors and movement over here and you'll notice that they will be perfectly parallel
to each other.
Okay, so that's adding a vector to a position which in this case is the starting position of the pig.
When we come back in the next video, we're going to look at getting the pig to move along the vector
smoothly rather than jumping from the current position to the end of the added vector.