In this lecture I'd like you to follow along as we examine moving along a straight line using vectors.
So download the resource project that comes with this lecture in there you'll find a man character.
And on top of that is being added a movie script and it's the movie script that we're going to be working
in.
So I pick that up and have a look in here.
You can see in the script that there is a victim 3 which is a goal.
So the victim theory is a 3-D version of Victor which has an x y and Zed and were initially setting
that there to be five in the x direction 0 in the wide direction and four in the z direction because
this is a humanoid character on a flat surface.
When we move him around we're not adding any y because you don't want it to go off and float in the
air.
If it was an airplane or something that flies it would be completely different.
Notice also there's no physics whatsoever involved in this project.
We're moving this character purely using our own code and that so that we can keep control of it.
Now in the stot function we're adding Al Victis three onto the current position of our character.
So here we go to transform and we're running a translight.
And basically what that does is it adds the goal vector to the transform vector that Sayf go back to
unity and press play.
And what you'll see is your character jump that much distance along that vector because it's in the
stop function.
It's adding the whole lot at once and moving your character to that goal.
Now that's not going to work.
If you want to move the character bit by bit in the update and make it appear as though it's moving
itself so I stopped playing let's go back in to move and have a look at moving these translite down
into the update.
Get rid of the one from the start and save that switch back into unity and press play.
And what you'll notice is the character completely disappears but if you select the man in the hierarchy
have a look at the transform and the position in the x and the Zed are going up and up and up and that's
because that whole vector is being added each time an update occurs.
And so he is moving he's just jumping in very big leaps and getting further and further away so that
we can't seem so let's stop that.
What we want instead of jumping to the goal position is to move in little tiny steps along in exactly
the same direction as that vector.
So to get a smaller version of the vector you can divide the logic by a number that will give be something
little.
So if we just got back in to move and in here with the update let's go in the stop let's go.
Go equals go multiplied by zero point five F and that's going to have the size of the goal.
So now he should move along the same direction.
Bosh You should see him stop halfway along and then jump to the goal location.
But after that he will keep cycling and adding the goal on say that suspect unity press play.
You probably saw him jump a little bit there and that was him moving in there is still a big leap but
only half of the victory each time.
So let's go back into mode.
What happens if we multiply this by zero or point zero one so we get 10 percent of that vector instead.
Anything you multiply or divide on the vector is going to still be in exactly the same direction that
you want your character to go.
So let's play this one have a look.
So that's much smoother in the movement and he's moving towards the goal location.
Now the goal location itself wasn't a position in space.
It was a goal direction.
So there's a difference between a point and evict a point is a fixed sport somewhere in your world.
A vector is just a set of instructions of which direction to move in from any location in your world.
So your character is now some way if you click on him still moving along at 10 percent of goal each
time.
Now if we simply multiply our victory what our character to move along by a small number then where
ever that vector changes it's going to affect our characters speed.
He won't be moving at a constant speed and we can illustrate that by adding in another character.
So first of all let's make our victim go public so that we can change it then we will still multiply
it by point 0 1 and translite along that.
So just save that.
Now if we go back into Unity get the man and just control data jus placate and then move humor out and
try and put him sort of moving in this direction we want them to move parallel to each other.
So I just want to put it in there.
But for this guy his move goal let's multiply it up by two.
So it's like twice as far away.
It's like out here somewhere.
And so this might take an 0 and.
Right.
OK so now when we run this we'll see different speeds see how this guy's going much faster because 10
percent of that longer Victor is going to be a big jump than 10 percent of the shortfall.
So they're not at a uniform spade and that's going to make the movement of your character inconsistent
when they're moving along different sort of Viktors and different length vectors.
So the question is how can we get a.
That is a standard size that points in the same direction along the vector in that we're traveling along.
And the solution is to get its normalized version the normalized version of any vector is a vector that
is one in length and you can find the normalized version of a vector by dividing each of the x y and
Zed values of the vector by the magnitude.
Now you can also get unity to do it for you.
Let's go back into the move and instead of having our goal here that we're moving along multiply by
point 0 1 just to make it much smaller we want to make it into a standard saw.
So let's just get rid of that altogether.
And what we're trying to do is translate a long goal of normalized like that and that will be a vector
of unit length 1 going in the same direction.
Now when you play this will probably be too far.
So if we just come back and run us Yep you can see that they did move the same right.
But it was quite jittery and it was a big hop h update.
But we now know that we have a standardized unit one movement each update and now that we have this
we can multiply this by a spade value to change how fast our character is going.
So let's just create another public float and let's call it spade and we'll set that because we want
to make it a bit slower 0.1 if and then multiply your normalized goalball your speed like that.
And now because it's public you could change the speed for each of those characters but we'll just leave
it at point one for both of them because we want to see them moving at the same right Press play.
And they go and no go on for ever and ever.
Because even though we gave them starting victims that were completely different they were pointing
in the same direction.
And then they got normalized down to unit 1.
And so they ended up with exactly the same vector multiplied by exactly the same speed.
So it doesn't matter how long the victory is if you normalize it you end up with the same little vector
which is of unit 1.
Now those Spade's even though you think it's a consistent speed the other factor involved in it go back
into your move code is the speed at which this update occurs because every time your game runs an update
it's not necessarily at exactly the same time that the last update happened.
So you will get inconsistencies in the spade and although you probably won't even notice these you will
see in a lot of code that people multiplied by time to time.
And what this does is it smoothes out and allows for the difference in the update running.
The other thing I should say to before we finish up is to make this a light update.
Any movement you've got on your characters end especially when you start to add character cameras that
follow things around you'll want it all to happen.
After all the physics has been calculated in the St..
Currently we don't have physics so it's not an issue but you should get into the habit of putting your
movements into the light updates which I still am not and feel free to correct me throughout.
If I do have an update loop with movement in it because I'm bound to because that's a bad habit of mine.
So this should be all we need now to have a nice smooth and consistent movement of our character.
Say it go back into unity and press play.
And now.
Well that's Bate's quite small isn't it.
And you can say that the multiplying of the Tondo delta time has also slowed down a fair bit because
it's going to be caught a tiny value because of the time difference between update loops and therefore
that allows you to then play around with the speeds that you're setting on your characters so maybe
instead of point 1 You might want to change these to 1 and try it out yet.
That's a bit better but that really is the code you should use when you stop moving a character around.
All right so that's it for moving something in a straight line and how to get consistent speed in your
character's movements in the next lecture.
We will start to look at moving towards a goal and making our characters turn.