WEBVTT

00:03.900 --> 00:10.890
In the previous lecture, we added a vector to our pigs and got them to jump for the whole length of

00:10.890 --> 00:11.910
that vector.

00:12.060 --> 00:16.470
Now we'll make them move towards the goal location.

00:16.470 --> 00:22.950
So we first need to work out what the vector to the goal location is.

00:23.070 --> 00:30.690
And the way you do that is to get the goal coordinates and minus the pig coordinates from that, which

00:30.690 --> 00:34.920
will give you the vector from the pig to the goal.

00:34.920 --> 00:39.510
So let's open up our move, which is on both of the pigs.

00:39.510 --> 00:41.580
We have the goal here.

00:41.610 --> 00:50.370
Now we're going to also calculate a vector three, which will be the direction that we're going to travel

00:50.370 --> 00:50.910
in.

00:50.940 --> 00:57.990
We'll get rid of this translate from here, and instead we'll actually say that direction.

01:00.050 --> 01:09.500
Equals goal dot transform dot position minus the pigs transform dot position.

01:09.500 --> 01:11.420
And that's the object that this code is on.

01:11.420 --> 01:16.580
So transform dot position and that will give us our direction.

01:16.580 --> 01:17.030
Okay.

01:17.030 --> 01:22.640
So then what we do is actually jump by direction.

01:22.640 --> 01:35.090
So if we put it down in here and we put that this dot transform dot position equals this dot transform

01:35.540 --> 01:40.040
position, and of course you don't need to put this in there because you could put it up here too.

01:40.040 --> 01:45.020
It's just something that's left over from all my years of actually programming this.

01:45.290 --> 01:50.000
So this dot transform dot position plus the direction.

01:50.000 --> 01:59.540
Now this line here is the same as going this dot transform translate.

02:00.770 --> 02:01.820
Direction.

02:03.020 --> 02:07.880
So these two things are equivalent, but we're going to use this position up here.

02:08.390 --> 02:08.870
Okay.

02:08.870 --> 02:15.770
So let's save that and just have a look at what we get so the pigs will jump to the location of the

02:15.770 --> 02:16.670
goal.

02:16.940 --> 02:18.950
So let's press play.

02:20.810 --> 02:22.120
And there they are.

02:22.400 --> 02:25.790
So they're both actually on top of each other at this point.

02:25.790 --> 02:29.450
Let me just move them out so you can see that they're both in that spot.

02:30.620 --> 02:36.470
Now, usually when you have an artificial intelligence character and you want them to move towards a

02:36.470 --> 02:39.380
goal location, they don't just immediately jump there.

02:39.380 --> 02:42.930
They make their way bit by bit along here.

02:42.950 --> 02:47.180
So what I'm going to do with these two pigs, I'm just going to move them out.

02:47.180 --> 02:51.580
So they're sort of in a different location from each other like that.

02:51.590 --> 02:59.030
Now we're going to go back into our MOVE code and we're going to come down into update and make this

02:59.030 --> 03:04.640
a late update because we want it to be the last thing that gets calculated.

03:04.670 --> 03:10.520
Usually you'll put this kind of movement in a late update because it will happen after any physics,

03:10.850 --> 03:15.980
things that are going on or animations, and that in this case we don't have those, but it's good practice

03:15.980 --> 03:17.560
to put it in a late update.

03:17.570 --> 03:25.550
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

03:25.550 --> 03:28.890
into our late update so it runs over and over again.

03:28.910 --> 03:35.630
Now, the issue is that the first time this runs is going to jump the pig straight to the goal location.

03:35.630 --> 03:42.860
But we want it to only take small steps of this direction, not the whole direction vector.

03:42.860 --> 03:49.100
And you can do that by picking out a small section of it so you can actually reduce it by multiplying

03:49.100 --> 03:51.020
by something like this.

03:51.020 --> 03:56.510
0.01 f the vector for direction will stay pointing in the same direction.

03:56.510 --> 03:57.950
It just becomes shorter.

03:57.950 --> 04:03.710
So instead of one great big leap, it'll take lots of little leaps as we go along.

04:03.740 --> 04:10.790
Okay, so with that change made, let's save this will come back in to Unity and there'll be two observations

04:10.790 --> 04:13.310
here that you'll need to consider.

04:13.460 --> 04:15.560
So let's press play.

04:16.900 --> 04:17.280
Okay.

04:17.320 --> 04:19.180
So there's our pigs.

04:19.720 --> 04:20.850
Now, that's the first one.

04:20.860 --> 04:29.440
Obviously, we are adding direction or a small vector that points in the same direction as direction.

04:29.440 --> 04:32.020
And it keeps adding and it keeps adding and it keeps adding.

04:32.020 --> 04:37.930
So it actually keeps moving past it because we're not telling it when to stop now.

04:37.930 --> 04:39.330
That's okay at the moment.

04:39.340 --> 04:50.980
Another thing, though, if we have this pig here, for example, and then we press play, then both

04:50.980 --> 04:54.940
of the pigs are going to reach the goal at the same time.

04:54.940 --> 04:56.080
Now, what's wrong with that?

04:56.080 --> 05:03.220
Well, this pig's obviously going to have to work a lot harder, as in go faster to get there, whereas

05:03.220 --> 05:04.540
this one can go slower.

05:04.540 --> 05:13.180
But we because we are multiplying by 0.01 the length of this vector into little bits and this vector

05:13.180 --> 05:20.230
into little bits, then 0.01 of this vector is going to be a lot longer than 0.01 of this.

05:20.230 --> 05:26.710
So it's still going to do 100 iterations before it gets to the goal for both of them.

05:26.710 --> 05:33.220
So they're still doing the same amount of update loops rather than moving at a consistent speed, because

05:33.220 --> 05:39.010
if they were at a consistent speed, this pig would actually make it to there before this pig.

05:39.010 --> 05:41.710
Okay, so how do we fix these two issues?

05:42.010 --> 05:44.050
We'll address the speed.

05:44.050 --> 05:47.110
First, let's open up our MOVE code.

05:47.140 --> 05:55.510
Now, rather than adding direction multiplied by 0.01, which reduces direction down to 100th of its

05:55.510 --> 06:00.760
size, and therefore for both pigs, it's going to be a different sort of length vector.

06:00.970 --> 06:02.470
We normalize it.

06:02.470 --> 06:11.410
Now normalizing a vector is to reduce it to a length of one keeps the same direction, but it's going

06:11.410 --> 06:13.270
to be consistently a length of one.

06:13.270 --> 06:22.870
So we go direction dot normalized and that will return a normalized version or a length of one direction.

06:22.870 --> 06:24.490
Okay, so let's just save this.

06:24.490 --> 06:31.090
When we run this now you're going to find that they will be moving at the same speed.

06:31.630 --> 06:37.000
So that means the closest pigs going to hit that goal first.

06:38.270 --> 06:38.690
Whoa.

06:38.690 --> 06:39.800
And very fast.

06:40.000 --> 06:40.720
Okay.

06:41.180 --> 06:43.670
Because of the length of one that we're adding.

06:43.670 --> 06:45.920
So we can actually slow that down a little bit.

06:45.950 --> 06:53.120
What I'm going to do to make it even more consistent here is actually let's go up and add another line

06:53.120 --> 06:57.610
in here so that you've got a vector three velocity.

06:57.620 --> 07:03.980
Now the velocity is going to be basically our direction vector and it's length.

07:03.980 --> 07:10.790
So we'll take this normalized direction here out and we'll make velocity equal that.

07:11.060 --> 07:17.300
And then we can actually multiply it by a speed factor like this.

07:17.300 --> 07:22.040
And the speed factor we might define up the top to slow things down.

07:22.040 --> 07:24.200
Okay, so let's put up here.

07:24.200 --> 07:32.080
Float speed equals 0.0 5fk.

07:32.150 --> 07:38.000
So that will make this length of one smaller but consistently smaller for both peaks.

07:38.000 --> 07:40.940
And then we'll add the velocity here.

07:41.540 --> 07:43.010
So save that.

07:43.130 --> 07:48.680
Let's make sure I've got a semicolon on that and let's run it again.

07:48.680 --> 07:54.770
So the pigs should be slower but still moving at the same rate.

07:57.320 --> 07:57.490
Okay.

07:57.680 --> 07:58.260
And they are.

07:58.280 --> 08:01.130
And this pig hits the goal first.

08:01.820 --> 08:02.900
And.

08:04.220 --> 08:04.610
That.

08:06.270 --> 08:07.470
Okay, great.

08:07.470 --> 08:09.370
So that's the first thing.

08:09.390 --> 08:18.630
The other thing is to make this more consistent an update, any update loop isn't a set amount of time

08:18.630 --> 08:19.290
in this case.

08:19.320 --> 08:19.480
Okay.

08:19.500 --> 08:20.820
There's there is fixed update.

08:20.820 --> 08:21.210
Sure.

08:21.210 --> 08:24.810
But we want this stuff to be consistent and smooth.

08:24.810 --> 08:34.140
So we actually also multiply this by time dot delta time, which is the amount of time that has happened

08:34.140 --> 08:37.140
since the last frame was produced.

08:37.140 --> 08:43.380
And that will actually smooth out your velocity and take into consideration that there might be faster

08:43.380 --> 08:45.390
updates that occur.

08:45.450 --> 08:50.820
So with that in place, you'll probably also find, because you've multiplied by this, that your speed

08:50.820 --> 08:56.100
is going to come down considerably and you might need to change this speed value, right?

08:56.100 --> 08:59.220
So let's just press play and see where we're at.

08:59.270 --> 09:01.350
We need to speed this up.

09:01.350 --> 09:07.260
You'll also find as we're doing this, that the pigs are actually sinking through the ground.

09:07.260 --> 09:13.050
Now, the reason that they're doing that and they are moving now just very, very slow, is that the

09:13.050 --> 09:18.240
vector from the pig to our goal is actually slightly downwards.

09:18.240 --> 09:24.300
Therefore, when this pig actually hits here, it's actually going down and through the ground and out

09:24.300 --> 09:30.330
there because of the 3D nature of this, if you want to keep the pig on the plane, then you could lift

09:30.330 --> 09:35.460
up the goal position to have the same y position as the pig.

09:35.460 --> 09:37.110
And we'll have a look at that in one minute.

09:37.110 --> 09:39.930
Let's just speed up these pigs.

09:39.930 --> 09:46.770
So if we put two in here instead and press play, then we will.

09:51.040 --> 09:53.140
Witness that pig going underground.

09:53.170 --> 09:55.310
Okay, but they're moving much smoother.

09:55.330 --> 09:59.530
So now if we grab one of the pigs, let's have a look at the height.

09:59.560 --> 10:00.520
The pig is on.

10:00.520 --> 10:03.460
It's like 0.862.

10:03.550 --> 10:14.350
If you get the goal location and put that at 0.862 and then run this again, you'll find that because

10:14.350 --> 10:19.210
there's no difference in the pigs height is not going to go through the ground.

10:19.210 --> 10:21.010
It's going to stay on the plane.

10:21.550 --> 10:21.970
Okay.

10:21.970 --> 10:25.240
With that working, we'll leave this video here when we come back.

10:25.240 --> 10:31.060
In the next video, we'll address the issue of getting the pig to stop at the goal.
