In the previous lecture, we normalized the movement of our pigs.
We're now going to stop them going past that goal location.
That means we need to check where they are in relation to the goal location, each loop.
So let's go back in to our script.
Inside of our light update, we're calculating the velocity and then we're also updating the position
now the actual goal or the direction of the goal.
That won't change, but the length of this will as we move closer and closer to the actual goal.
So we need to really be calculating this over and over again within our update, and especially if our
goal happens to move.
You might want your character to keep updating that as well.
So let's copy this and put it down into here.
Can actually take that out of stop because we won't need it up there.
Okay, so let's have a look at how this behaves.
Save it.
Go back into Unity Press play and let's have a look.
So the pigs go towards the goal and then you get the pigs kind of jittering on that goal location.
So what is actually happening here?
Well, let me go back to the code again.
Let's just emphasize that a bit.
Let's update this speed, let's make it five, and we will try and get them to overshoot the position
of that goal.
So let's press play.
And now they're going to go.
It's got a bit more of a jitteriness there.
What's actually happening is that the pigs are overshooting it and they're actually going past it and
then in the next loop calculates a new direction.
So they actually go over here and then they get a new direction to come back this way and then this
way, then this way.
So that's what's giving you that jitteriness there.
Instead, we kind of want to say, well, if you've reached the goal or close enough to the goal, then
be done with it.
Basically, stop calculating and updating yourself to actually get spot on.
The goal location is really hard, so you need some kind of a threshold value that's sort of near enough
is good enough.
Let's go inside here and have a look.
So we calculating our direction before we update the pigs position again, we're going to say if direction
and we're going to work out how far away we are from the goal.
So remember that direction is the vector from the pig to the goal.
If we get the length of that vector, then we know how far away we are and that is phan with a dot magnitude.
So that's the actual distance between the pig and the goal.
Now, if that magnitude, let's say that it's greater than two.
If it's greater than two, then we are quite happy to go ahead and recalculate.
The velocity and keep moving towards it.
And this will actually stop once you get within that circle of two.
So save this.
Let's go back into unity and we will try this one out.
So let's press play and the pigs will move close enough.
And then when they get within two, they stop.
So you can see that now you know what a distance of two looks like.
If you want them to get a little bit closer, then you could of course decrease that threshold value.
So if you ever get an AI character that comes over to a goal location and Jeter's back and forward between
it or possibly even goes around in circles that we might see a little bit later in this course, it
means that you are trying to get the character to like get exactly on this spot.
And with the calculations that are currently running, it's just never going to happen.
So just be aware that that's what you want to do in this case.
The nice thing about the way we've done this code is it's always going to be checking for direction
and directions magnitude.
That means if you move that goal location, then you can actually get the pigs to follow it around because
it will keep updating this direction.
So let's actually add something onto the goal that will make it move around.
What I'm going to do is replace that goal, object with something else.
Okay, so let's just go into our scene now.
You want to come in to your Easy Primitives folder prefab and find something in here that the pig might
like to follow around.
So I'm thinking something like a villager.
Okay, so here's my villager.
Let's drag and drop the villager in and put him just there.
So this villager has a forward facing direction.
So that means if we actually steer it, we'll be able to see which direction it's going in, which is
useful.
So the other goal location, let's just get rid of that.
So select it and delete it.
And then for our pig we want to grab our villager and just put that as the goal object over here and
the inspector, same with the other pig because they're going to follow the same character.
And now let's go to the top level of our assets and we're going to add another script in here, create
C-sharp and let's call it Drive.
Now, this code, I'm not writing from scratch.
If you've seen any of my other videos where I actually create a drive script on the characters or cars
or whatever I've got, I get that from the unity documentation.
So if you have a look at your documentation and in the search, type in input, get access and go down
to the first example, you'll find that there's a simplistic car driving piece of script here, which
is so useful.
I mean, I think I've been using this since Unity from 2006, Unity version two all the way back then.
Find this and we're going to copy all of it.
This uses the old input system, too, by the way.
So let's come back over in here, open up our drive script and we're just going to replace the default
with that code there.
And then just make sure we call this drive, then save it, go back into unity and drag and drop that
drive script on to the villager in the hierarchy.
And if you want to know how that's working, then feel free to dig around through that code.
But basically there's a speed and a rotation speed and we can move the village out using the arrow keys.
Okay, so let's press play.
The side arrow keys are going to turn the character, as you can see there.
And then we can just use the up and down arrow keys.
And now you can see that we've got our pigs following the little farmer or villager around, and they're
actually now in exactly the same position.
So they're kind of on top of each other.
Okay.
You might also want the characters to turn towards where they're going now.
This is actually very simply done.
If we go into our move after we've calculated the direction that we're going here, we're going to say
that this dot transform dot look at and we will look at our goal position.
So copy that, put it in there.
And that basically will turn the character to look at the goal.
So it save that by look at what I mean is that if we go back in here and just click on one of these
pigs, you're going to find that the axis system for the pig here took its forward direction, which
is what you would assume it's facing towards is this blue axis, which is the Z axis.
So most games and modelling packages and things like that have the Z axis as the forward facing axis.
So what look at does is actually turn this axis, this pig around so that the blue axis is facing where
it needs to go.
So if we now press play, you'll be able to actually see them as they look where they're going at this
point.
Of course, you could multiply those pigs and have a lot of them following the farmer around.
But let's finish off this video with a little bit of a challenge.
See if you can give the pigs different speeds so they aren't travelling at the same rate as each other.
So pause the video now, give it a go.
And when we come back, I'll go through a solution with you.
Welcome back.
How did you go with giving them a different speed?
Let's go into the movie script.
So we have speed up here set to five to vary that for different pigs.
We can put a public float speed in there and then if you save that and go back into unity, select the
pig.
And then in the inspector you can actually give them different speeds.
So this guy here could be three, for example.
So he's going to be a lot slower.
Let's press play.
And now we move the farmer around and you'll see that that other pig is actually moving a lot faster
because he catches up to the little farmer slash villager guy quite quickly.
Okay.
That's a first look at some vectors.
When we come back in the next video, we're going to look a little further into calculating the distance
between points, which we've already started to do with our magnitude value.
But we'll take a closer look at Pythagoras.