WEBVTT

00:03.960 --> 00:08.700
In the previous lecture, we normalized the movement of our pigs.

00:08.700 --> 00:14.580
We're now going to stop them going past that goal location.

00:15.030 --> 00:22.620
That means we need to check where they are in relation to the goal location, each loop.

00:22.920 --> 00:26.880
So let's go back in to our script.

00:27.030 --> 00:35.360
Inside of our light update, we're calculating the velocity and then we're also updating the position

00:35.370 --> 00:39.870
now the actual goal or the direction of the goal.

00:39.900 --> 00:47.430
That won't change, but the length of this will as we move closer and closer to the actual goal.

00:47.610 --> 00:55.650
So we need to really be calculating this over and over again within our update, and especially if our

00:55.650 --> 00:57.570
goal happens to move.

00:57.570 --> 01:00.510
You might want your character to keep updating that as well.

01:00.510 --> 01:04.470
So let's copy this and put it down into here.

01:05.490 --> 01:09.660
Can actually take that out of stop because we won't need it up there.

01:09.780 --> 01:13.020
Okay, so let's have a look at how this behaves.

01:13.290 --> 01:13.980
Save it.

01:13.980 --> 01:18.360
Go back into Unity Press play and let's have a look.

01:18.360 --> 01:26.400
So the pigs go towards the goal and then you get the pigs kind of jittering on that goal location.

01:26.400 --> 01:29.590
So what is actually happening here?

01:29.610 --> 01:32.130
Well, let me go back to the code again.

01:32.130 --> 01:34.100
Let's just emphasize that a bit.

01:34.110 --> 01:42.840
Let's update this speed, let's make it five, and we will try and get them to overshoot the position

01:42.840 --> 01:43.620
of that goal.

01:43.620 --> 01:45.660
So let's press play.

01:45.960 --> 01:47.970
And now they're going to go.

01:48.000 --> 01:51.200
It's got a bit more of a jitteriness there.

01:51.210 --> 01:58.710
What's actually happening is that the pigs are overshooting it and they're actually going past it and

01:58.710 --> 02:02.160
then in the next loop calculates a new direction.

02:02.160 --> 02:06.570
So they actually go over here and then they get a new direction to come back this way and then this

02:06.570 --> 02:07.350
way, then this way.

02:07.350 --> 02:10.710
So that's what's giving you that jitteriness there.

02:10.980 --> 02:18.420
Instead, we kind of want to say, well, if you've reached the goal or close enough to the goal, then

02:18.600 --> 02:19.640
be done with it.

02:19.650 --> 02:25.620
Basically, stop calculating and updating yourself to actually get spot on.

02:25.620 --> 02:31.620
The goal location is really hard, so you need some kind of a threshold value that's sort of near enough

02:31.620 --> 02:32.850
is good enough.

02:33.030 --> 02:35.010
Let's go inside here and have a look.

02:35.010 --> 02:45.900
So we calculating our direction before we update the pigs position again, we're going to say if direction

02:45.900 --> 02:50.490
and we're going to work out how far away we are from the goal.

02:50.670 --> 02:57.720
So remember that direction is the vector from the pig to the goal.

02:57.720 --> 03:04.950
If we get the length of that vector, then we know how far away we are and that is phan with a dot magnitude.

03:05.370 --> 03:09.840
So that's the actual distance between the pig and the goal.

03:09.870 --> 03:15.780
Now, if that magnitude, let's say that it's greater than two.

03:16.170 --> 03:23.190
If it's greater than two, then we are quite happy to go ahead and recalculate.

03:24.320 --> 03:27.140
The velocity and keep moving towards it.

03:27.140 --> 03:31.910
And this will actually stop once you get within that circle of two.

03:31.940 --> 03:33.500
So save this.

03:33.590 --> 03:38.000
Let's go back into unity and we will try this one out.

03:38.240 --> 03:43.340
So let's press play and the pigs will move close enough.

03:43.340 --> 03:45.860
And then when they get within two, they stop.

03:46.160 --> 03:49.970
So you can see that now you know what a distance of two looks like.

03:50.000 --> 03:57.880
If you want them to get a little bit closer, then you could of course decrease that threshold value.

03:57.890 --> 04:06.410
So if you ever get an AI character that comes over to a goal location and Jeter's back and forward between

04:06.410 --> 04:12.440
it or possibly even goes around in circles that we might see a little bit later in this course, it

04:12.440 --> 04:18.680
means that you are trying to get the character to like get exactly on this spot.

04:18.680 --> 04:23.810
And with the calculations that are currently running, it's just never going to happen.

04:23.810 --> 04:28.520
So just be aware that that's what you want to do in this case.

04:29.000 --> 04:35.690
The nice thing about the way we've done this code is it's always going to be checking for direction

04:35.690 --> 04:38.050
and directions magnitude.

04:38.060 --> 04:45.410
That means if you move that goal location, then you can actually get the pigs to follow it around because

04:45.410 --> 04:48.620
it will keep updating this direction.

04:48.620 --> 04:54.570
So let's actually add something onto the goal that will make it move around.

04:54.590 --> 04:59.270
What I'm going to do is replace that goal, object with something else.

04:59.270 --> 05:01.520
Okay, so let's just go into our scene now.

05:01.520 --> 05:09.050
You want to come in to your Easy Primitives folder prefab and find something in here that the pig might

05:09.050 --> 05:10.820
like to follow around.

05:10.820 --> 05:15.740
So I'm thinking something like a villager.

05:15.740 --> 05:16.940
Okay, so here's my villager.

05:16.940 --> 05:22.250
Let's drag and drop the villager in and put him just there.

05:22.250 --> 05:27.200
So this villager has a forward facing direction.

05:27.200 --> 05:33.020
So that means if we actually steer it, we'll be able to see which direction it's going in, which is

05:33.020 --> 05:33.680
useful.

05:33.680 --> 05:37.220
So the other goal location, let's just get rid of that.

05:37.220 --> 05:39.470
So select it and delete it.

05:39.470 --> 05:46.640
And then for our pig we want to grab our villager and just put that as the goal object over here and

05:46.640 --> 05:52.700
the inspector, same with the other pig because they're going to follow the same character.

05:52.700 --> 06:00.170
And now let's go to the top level of our assets and we're going to add another script in here, create

06:00.680 --> 06:03.680
C-sharp and let's call it Drive.

06:04.100 --> 06:07.190
Now, this code, I'm not writing from scratch.

06:07.190 --> 06:13.790
If you've seen any of my other videos where I actually create a drive script on the characters or cars

06:13.790 --> 06:18.800
or whatever I've got, I get that from the unity documentation.

06:18.800 --> 06:26.330
So if you have a look at your documentation and in the search, type in input, get access and go down

06:26.330 --> 06:32.690
to the first example, you'll find that there's a simplistic car driving piece of script here, which

06:32.690 --> 06:34.190
is so useful.

06:34.190 --> 06:41.930
I mean, I think I've been using this since Unity from 2006, Unity version two all the way back then.

06:42.110 --> 06:45.050
Find this and we're going to copy all of it.

06:45.470 --> 06:49.520
This uses the old input system, too, by the way.

06:49.520 --> 06:57.230
So let's come back over in here, open up our drive script and we're just going to replace the default

06:57.230 --> 06:58.760
with that code there.

06:58.760 --> 07:06.950
And then just make sure we call this drive, then save it, go back into unity and drag and drop that

07:06.950 --> 07:10.880
drive script on to the villager in the hierarchy.

07:10.880 --> 07:16.490
And if you want to know how that's working, then feel free to dig around through that code.

07:16.490 --> 07:22.820
But basically there's a speed and a rotation speed and we can move the village out using the arrow keys.

07:22.820 --> 07:25.460
Okay, so let's press play.

07:25.490 --> 07:29.780
The side arrow keys are going to turn the character, as you can see there.

07:29.960 --> 07:32.810
And then we can just use the up and down arrow keys.

07:32.810 --> 07:40.730
And now you can see that we've got our pigs following the little farmer or villager around, and they're

07:40.730 --> 07:43.250
actually now in exactly the same position.

07:43.250 --> 07:45.590
So they're kind of on top of each other.

07:47.850 --> 07:48.840
Okay.

07:49.260 --> 07:54.970
You might also want the characters to turn towards where they're going now.

07:54.990 --> 07:57.260
This is actually very simply done.

07:57.270 --> 08:06.090
If we go into our move after we've calculated the direction that we're going here, we're going to say

08:06.090 --> 08:14.490
that this dot transform dot look at and we will look at our goal position.

08:14.490 --> 08:17.610
So copy that, put it in there.

08:17.610 --> 08:22.740
And that basically will turn the character to look at the goal.

08:22.800 --> 08:30.450
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

08:30.450 --> 08:39.120
pigs, you're going to find that the axis system for the pig here took its forward direction, which

08:39.120 --> 08:44.610
is what you would assume it's facing towards is this blue axis, which is the Z axis.

08:44.610 --> 08:52.710
So most games and modelling packages and things like that have the Z axis as the forward facing axis.

08:52.710 --> 09:01.200
So what look at does is actually turn this axis, this pig around so that the blue axis is facing where

09:01.200 --> 09:02.910
it needs to go.

09:02.910 --> 09:11.430
So if we now press play, you'll be able to actually see them as they look where they're going at this

09:11.430 --> 09:11.790
point.

09:11.790 --> 09:17.070
Of course, you could multiply those pigs and have a lot of them following the farmer around.

09:17.070 --> 09:21.450
But let's finish off this video with a little bit of a challenge.

09:21.690 --> 09:31.800
See if you can give the pigs different speeds so they aren't travelling at the same rate as each other.

09:31.890 --> 09:34.140
So pause the video now, give it a go.

09:34.140 --> 09:37.500
And when we come back, I'll go through a solution with you.

09:38.290 --> 09:39.180
Welcome back.

09:39.190 --> 09:42.700
How did you go with giving them a different speed?

09:42.700 --> 09:44.680
Let's go into the movie script.

09:44.680 --> 09:51.040
So we have speed up here set to five to vary that for different pigs.

09:51.040 --> 10:01.420
We can put a public float speed in there and then if you save that and go back into unity, select the

10:01.420 --> 10:01.990
pig.

10:01.990 --> 10:05.890
And then in the inspector you can actually give them different speeds.

10:05.890 --> 10:08.530
So this guy here could be three, for example.

10:08.530 --> 10:10.030
So he's going to be a lot slower.

10:10.090 --> 10:11.620
Let's press play.

10:11.800 --> 10:20.980
And now we move the farmer around and you'll see that that other pig is actually moving a lot faster

10:20.980 --> 10:28.560
because he catches up to the little farmer slash villager guy quite quickly.

10:28.570 --> 10:29.020
Okay.

10:29.020 --> 10:31.330
That's a first look at some vectors.

10:31.330 --> 10:37.840
When we come back in the next video, we're going to look a little further into calculating the distance

10:37.840 --> 10:42.040
between points, which we've already started to do with our magnitude value.

10:42.040 --> 10:45.310
But we'll take a closer look at Pythagoras.
