In the previous lecture, we added a vector to our pigs and got them to jump for the whole length of
that vector.
Now we'll make them move towards the goal location.
So we first need to work out what the vector to the goal location is.
And the way you do that is to get the goal coordinates and minus the pig coordinates from that, which
will give you the vector from the pig to the goal.
So let's open up our move, which is on both of the pigs.
We have the goal here.
Now we're going to also calculate a vector three, which will be the direction that we're going to travel
in.
We'll get rid of this translate from here, and instead we'll actually say that direction.
Equals goal dot transform dot position minus the pigs transform dot position.
And that's the object that this code is on.
So transform dot position and that will give us our direction.
Okay.
So then what we do is actually jump by direction.
So if we put it down in here and we put that this dot transform dot position equals this dot transform
position, and of course you don't need to put this in there because you could put it up here too.
It's just something that's left over from all my years of actually programming this.
So this dot transform dot position plus the direction.
Now this line here is the same as going this dot transform translate.
Direction.
So these two things are equivalent, but we're going to use this position up here.
Okay.
So let's save that and just have a look at what we get so the pigs will jump to the location of the
goal.
So let's press play.
And there they are.
So they're both actually on top of each other at this point.
Let me just move them out so you can see that they're both in that spot.
Now, usually when you have an artificial intelligence character and you want them to move towards a
goal location, they don't just immediately jump there.
They make their way bit by bit along here.
So what I'm going to do with these two pigs, I'm just going to move them out.
So they're sort of in a different location from each other like that.
Now we're going to go back into our MOVE code and we're going to come down into update and make this
a late update because we want it to be the last thing that gets calculated.
Usually you'll put this kind of movement in a late update because it will happen after any physics,
things that are going on or animations, and that in this case we don't have those, but it's good practice
to put it in a late update.
So this line here that's moving our pig, I'm just going to cut that out of there and we'll put it down
into our late update so it runs over and over again.
Now, the issue is that the first time this runs is going to jump the pig straight to the goal location.
But we want it to only take small steps of this direction, not the whole direction vector.
And you can do that by picking out a small section of it so you can actually reduce it by multiplying
by something like this.
0.01 f the vector for direction will stay pointing in the same direction.
It just becomes shorter.
So instead of one great big leap, it'll take lots of little leaps as we go along.
Okay, so with that change made, let's save this will come back in to Unity and there'll be two observations
here that you'll need to consider.
So let's press play.
Okay.
So there's our pigs.
Now, that's the first one.
Obviously, we are adding direction or a small vector that points in the same direction as direction.
And it keeps adding and it keeps adding and it keeps adding.
So it actually keeps moving past it because we're not telling it when to stop now.
That's okay at the moment.
Another thing, though, if we have this pig here, for example, and then we press play, then both
of the pigs are going to reach the goal at the same time.
Now, what's wrong with that?
Well, this pig's obviously going to have to work a lot harder, as in go faster to get there, whereas
this one can go slower.
But we because we are multiplying by 0.01 the length of this vector into little bits and this vector
into little bits, then 0.01 of this vector is going to be a lot longer than 0.01 of this.
So it's still going to do 100 iterations before it gets to the goal for both of them.
So they're still doing the same amount of update loops rather than moving at a consistent speed, because
if they were at a consistent speed, this pig would actually make it to there before this pig.
Okay, so how do we fix these two issues?
We'll address the speed.
First, let's open up our MOVE code.
Now, rather than adding direction multiplied by 0.01, which reduces direction down to 100th of its
size, and therefore for both pigs, it's going to be a different sort of length vector.
We normalize it.
Now normalizing a vector is to reduce it to a length of one keeps the same direction, but it's going
to be consistently a length of one.
So we go direction dot normalized and that will return a normalized version or a length of one direction.
Okay, so let's just save this.
When we run this now you're going to find that they will be moving at the same speed.
So that means the closest pigs going to hit that goal first.
Whoa.
And very fast.
Okay.
Because of the length of one that we're adding.
So we can actually slow that down a little bit.
What I'm going to do to make it even more consistent here is actually let's go up and add another line
in here so that you've got a vector three velocity.
Now the velocity is going to be basically our direction vector and it's length.
So we'll take this normalized direction here out and we'll make velocity equal that.
And then we can actually multiply it by a speed factor like this.
And the speed factor we might define up the top to slow things down.
Okay, so let's put up here.
Float speed equals 0.0 5fk.
So that will make this length of one smaller but consistently smaller for both peaks.
And then we'll add the velocity here.
So save that.
Let's make sure I've got a semicolon on that and let's run it again.
So the pigs should be slower but still moving at the same rate.
Okay.
And they are.
And this pig hits the goal first.
And.
That.
Okay, great.
So that's the first thing.
The other thing is to make this more consistent an update, any update loop isn't a set amount of time
in this case.
Okay.
There's there is fixed update.
Sure.
But we want this stuff to be consistent and smooth.
So we actually also multiply this by time dot delta time, which is the amount of time that has happened
since the last frame was produced.
And that will actually smooth out your velocity and take into consideration that there might be faster
updates that occur.
So with that in place, you'll probably also find, because you've multiplied by this, that your speed
is going to come down considerably and you might need to change this speed value, right?
So let's just press play and see where we're at.
We need to speed this up.
You'll also find as we're doing this, that the pigs are actually sinking through the ground.
Now, the reason that they're doing that and they are moving now just very, very slow, is that the
vector from the pig to our goal is actually slightly downwards.
Therefore, when this pig actually hits here, it's actually going down and through the ground and out
there because of the 3D nature of this, if you want to keep the pig on the plane, then you could lift
up the goal position to have the same y position as the pig.
And we'll have a look at that in one minute.
Let's just speed up these pigs.
So if we put two in here instead and press play, then we will.
Witness that pig going underground.
Okay, but they're moving much smoother.
So now if we grab one of the pigs, let's have a look at the height.
The pig is on.
It's like 0.862.
If you get the goal location and put that at 0.862 and then run this again, you'll find that because
there's no difference in the pigs height is not going to go through the ground.
It's going to stay on the plane.
Okay.
With that working, we'll leave this video here when we come back.
In the next video, we'll address the issue of getting the pig to stop at the goal.