WEBVTT

00:07.030 --> 00:13.240
Okay, so now we need to go up into our tick function in the player controller player Tick.

00:13.270 --> 00:14.230
Here it is.

00:14.350 --> 00:18.530
So first, let's get our controlled pawn.

00:18.550 --> 00:24.040
So a pawn controlled pawn equals get pawn.

00:24.820 --> 00:28.510
And we can actually place this in an if statement.

00:28.630 --> 00:32.230
So we only do this if we have a valid pawn.

00:32.440 --> 00:36.460
And in fact, we could just check is valid.

00:36.490 --> 00:39.550
That way we take care of the case when it's pending kill.

00:39.550 --> 00:41.280
We could do that if we wanted to.

00:41.290 --> 00:43.660
So just something to keep in mind.

00:43.750 --> 00:52.030
But if we have a valid pawn, I would like the location on the spline that is closest to the pawn because

00:52.030 --> 00:55.330
our pawn may not always be exactly on the spline.

00:55.330 --> 00:59.980
It'll most likely not be exactly on the spline.

01:00.370 --> 01:03.550
Our character will be slightly off from the spline.

01:03.550 --> 01:10.910
I want the location on the spline so I'm going to make a const f vector called location on spline.

01:11.240 --> 01:18.200
Now to get that, I take my spline and the spline has lots of functions that get things like this.

01:18.200 --> 01:26.690
One is find location closest to world location and this requires a world location so we can use our

01:26.690 --> 01:33.710
controlled pawn, get actor location and for coordinate space, I'm going to use world space.

01:33.710 --> 01:40.550
So I'm going to use E spline coordinate space, double colon world.

01:41.060 --> 01:47.490
So this vector is the location on the spline that's closest to the controlled pawn.

01:47.510 --> 01:54.770
So now that we have that location, we can find the direction on the spline that corresponds to this

01:54.770 --> 01:55.730
location.

01:55.940 --> 02:06.590
So we can make another const f vector called direction and we can take our spline and call another handy

02:06.620 --> 02:13.880
function, find direction closest to world location and we can use location on spline.

02:13.880 --> 02:17.530
In fact, we could just use the pawns location right there.

02:17.540 --> 02:18.920
That would work too.

02:18.950 --> 02:23.390
And we can also use e spline coordinate space world right here.

02:24.470 --> 02:28.220
So I could just pass in the controlled pawn location there.

02:28.220 --> 02:30.530
But then you wouldn't know about this function.

02:30.530 --> 02:32.090
So it's good to know about them.

02:32.150 --> 02:38.380
And as soon as we have that direction, we now know which direction to move the controlled pawn.

02:38.390 --> 02:45.260
So we're going to take our controlled pawn and add movement input passing in the direction.

02:47.930 --> 02:48.930
And that's it.

02:48.950 --> 02:54.590
Now, there are a couple other things We want to check our distance to the destination.

02:54.590 --> 02:58.930
We want to see if it's within auto run acceptance radius.

02:58.940 --> 03:08.240
So we're going to make a const float called Distance to Destination, and we're going to get our location

03:08.270 --> 03:14.270
on spline, which we calculated up there, minus our cached destination.

03:14.270 --> 03:21.170
So that gives us the vector from the cached destination to the location on the spline.

03:21.170 --> 03:23.570
It gets the distance between the two.

03:23.570 --> 03:27.320
I'm going to wrap that in parentheses and get length.

03:30.670 --> 03:32.380
And then we can check this distance.

03:32.380 --> 03:39.660
We can say if distance to destination is less than or equal to our auto run acceptance radius.

03:39.670 --> 03:43.420
If it is, well, then we should no longer be auto running.

03:43.420 --> 03:48.760
So we're going to set be auto running equal to false, and then we'll no longer auto run.

03:48.760 --> 03:49.750
We'll stop.

03:49.930 --> 03:51.700
So this is great.

03:51.700 --> 03:55.150
This is going to take care of our auto running.

03:55.150 --> 04:01.900
And this could be just refactored into a function called auto running or something like that.

04:01.900 --> 04:05.830
So why don't we make a private function for that void?

04:06.280 --> 04:08.740
We'll keep it simple and call it auto run.

04:09.250 --> 04:12.220
So we'll go ahead and generate definition.

04:13.080 --> 04:17.370
I'm going to cut this and stick it right up there next to the tick function.

04:19.770 --> 04:21.420
Right under player Tick.

04:23.290 --> 04:31.510
And I'm literally going to just stick this code into my auto run function and call auto run here.

04:32.470 --> 04:35.460
That way we're not cluttering up the player tick function.

04:35.470 --> 04:37.270
We're handling it here.

04:37.450 --> 04:44.230
So the last thing to do is to see if this works and most importantly, see if it works in multiplayer.

04:44.350 --> 04:47.080
So why don't we compile and launch?

04:49.210 --> 04:55.880
Okay, so I'm going to press play and it looks like I'm just auto running, so that's not good.

04:55.900 --> 04:59.710
So I think my auto running boolean should be set to.

05:00.370 --> 05:03.670
Fall somewhere and it's not being set to false.

05:04.180 --> 05:06.430
So we can fix that.

05:06.430 --> 05:12.450
And of course, our auto run function is just being called in player tick whether we should or not.

05:12.460 --> 05:18.280
So we can just check here in auto run if be auto running.

05:18.280 --> 05:26.830
In fact, if not be auto running return that way we'll only auto run if we should auto run if be auto

05:26.830 --> 05:28.690
running is true.

05:28.870 --> 05:30.700
So let's try that again.

05:32.390 --> 05:33.020
All right.

05:33.020 --> 05:35.330
So we're going to try this one more time.

05:35.330 --> 05:38.810
I'm going to click and hold That still works.

05:38.810 --> 05:40.700
I'm going to click and release.

05:40.850 --> 05:41.630
That works.

05:41.630 --> 05:45.620
And we see the path points and I'm going to click on the other side of this pillar.

05:47.140 --> 05:52.720
And it looks like we never stop auto running, but it did run around it, so that's good.

05:56.120 --> 05:57.800
We stopped that time.

05:59.090 --> 06:05.810
So I might have gotten a path point way off of the map somewhere.

06:07.620 --> 06:11.340
But it looks like aside from that small complication.

06:14.150 --> 06:15.530
It's working.

06:15.860 --> 06:19.400
Let's try this in multiplayer with two players.

06:20.280 --> 06:22.340
And on our client.

06:22.350 --> 06:25.050
I see that holding it down will work.

06:25.050 --> 06:28.230
But if I click and release, nothing happens.

06:28.230 --> 06:31.320
And in fact, we don't even get those debug spheres.

06:31.440 --> 06:36.570
Now, the reason for this is something that will trip you up if you forget this step or if you don't

06:36.570 --> 06:37.560
know about it.

06:37.560 --> 06:44.190
And that is that we have to go to edit and project settings and find navigation.

06:45.300 --> 06:47.460
Navigation system specifically.

06:48.120 --> 06:51.480
And check allow client side navigation.

06:51.480 --> 06:56.970
Without this checked that function generating our path will not work.

06:56.970 --> 06:59.570
But now that we've checked this, it should.

06:59.580 --> 07:01.950
So try not to forget about this.

07:01.980 --> 07:08.220
In fact, this is a good thing to write down if you're ever planning on generating a pathfinding path

07:08.220 --> 07:14.610
on the client side, because that's what we need to do here for our client now, as soon as that happens.

07:15.000 --> 07:16.440
Now it works.

07:16.440 --> 07:21.900
So I can click and we can generate that path and move to it.

07:22.680 --> 07:23.370
Great.

07:24.180 --> 07:28.230
Now, why is it that we get our character running off into the distance?

07:28.230 --> 07:34.200
Sometimes it's not every time, but if we click on the other side of something, it seems like we're

07:34.200 --> 07:36.600
never quite getting that end point.

07:36.630 --> 07:39.720
We're just running off into the distance.

07:39.720 --> 07:44.280
Well, that's going to happen if we click on this particular actor, right?

07:44.280 --> 07:52.110
If I click on it, we're not getting a point on the NAV mesh so I can click and we're really not going

07:52.110 --> 07:55.320
to get a valid point on the mesh.

07:55.320 --> 08:00.780
So we need to handle what happens when we click on something that's not on the NAV mesh.

08:00.810 --> 08:07.800
Well, for one, if I click on this pillar, I kind of want to move to the point behind it as if the

08:07.800 --> 08:11.460
pillar wasn't hit by my line trace.

08:11.490 --> 08:11.970
Right.

08:11.970 --> 08:13.080
So if I click.

08:14.310 --> 08:16.650
See, I don't want that sporadic behavior.

08:16.650 --> 08:18.990
I don't want undefined behavior.

08:18.990 --> 08:26.550
If I click on this thing, I want it to be like my line trace from my cursor could not even see it.

08:26.910 --> 08:29.620
Now, we can handle that in a number of ways.

08:29.640 --> 08:37.890
The simplest way would be to take our static mesh here and just have it ignore any and all line traces

08:37.890 --> 08:40.020
that are using the visibility channel.

08:40.050 --> 08:45.240
So if I go down to collision here for Collision Presets, it's set to default.

08:45.270 --> 08:49.230
We can change it to custom and we see that it's blocking.

08:49.230 --> 08:50.400
All right.

08:50.400 --> 08:53.340
But if we check ignore for visibility.

08:53.370 --> 08:55.140
Now if I press play.

08:56.670 --> 09:00.110
And I click on it, we'll go right behind it.

09:00.120 --> 09:05.220
So what was happening was we were hitting that collision volume, the simple collision volume.

09:05.220 --> 09:12.780
If I hit Tilde and type show collision, we'll see that it's a big block here.

09:12.780 --> 09:15.570
So clicking on that was causing us problems.

09:15.570 --> 09:19.080
Now that it's ignoring the visibility channel, we're good.

09:19.200 --> 09:25.980
Now, there's still one slight complication, and that's when we click on an area that's not in our

09:25.980 --> 09:27.930
NAV mesh bounds volume.

09:28.110 --> 09:30.690
And that would be basically right here in the center.

09:30.690 --> 09:36.990
If I click on that, you'll see that, well, it's trying to run, but it'll never actually get close

09:36.990 --> 09:39.780
enough to cancel auto running.

09:39.780 --> 09:46.770
So we do have a path and the last point in the path is right here, but the destination is right there

09:46.770 --> 09:47.630
in the middle.

09:47.640 --> 09:54.450
So an easy way to fix that is to simply take that last point in our generated path and set that to be

09:54.450 --> 09:56.410
our cached destination.

09:56.410 --> 10:02.860
That way, as we're checking to see if we should stop auto running, we're checking the cached destination.

10:02.860 --> 10:07.230
If that's now that last point in the path, then we'll reach it.

10:07.240 --> 10:14.530
So what we can do is after we get our path points, after looping through those path points, we can

10:14.530 --> 10:16.450
reset the cached destination.

10:16.450 --> 10:22.270
So we can say cached destination equals and we can get that path points array.

10:22.990 --> 10:32.650
So we'll say path points and we'll index it at NAV path path points dot numb minus one as Num is the

10:32.650 --> 10:37.610
number of elements and minus one gives us the last element in that array.

10:37.630 --> 10:42.310
And that way as we're checking to see if we're close enough, we're now going to check to see if we're

10:42.310 --> 10:48.520
close enough to the last point in the path, which we know is a reachable point because it was generated

10:48.520 --> 10:51.520
by find path to location synchronously.

10:51.520 --> 10:58.540
So now that we've added this one little line, we should now be able to click anywhere here and we'll

10:58.540 --> 11:00.910
always run to a reachable destination.

11:00.910 --> 11:04.780
So she won't be running off into the distance with no end.

11:05.910 --> 11:12.900
So we can test this out by going up to the pillar and clicking at its base and we'll see that we reach

11:12.900 --> 11:20.010
the last point, or at least close enough to the last point, and we'll never actually be running off

11:20.010 --> 11:23.310
into the distance with some undefined behavior.

11:23.670 --> 11:29.910
Okay, so we now have Click to Move and it works in multiplayer and it's looking great.

11:30.030 --> 11:32.500
So now we're ready to move on.

11:32.520 --> 11:38.340
We have a couple of things to fix and refactor and then we can start working on our actual gameplay

11:38.340 --> 11:40.890
abilities, which is really exciting.

11:40.890 --> 11:42.390
So I'll see you soon.
