WEBVTT

00:06.470 --> 00:07.430
Welcome back.

00:07.460 --> 00:09.490
No, you're not watching the wrong video.

00:09.500 --> 00:15.410
I've just gone back into here to remind you of the code that we wrote for these tanks to go round these

00:15.410 --> 00:16.160
palm trees.

00:16.190 --> 00:24.860
Now, we originally had some follow waypoint code for the tanks themselves, and they would have an

00:24.860 --> 00:30.800
accuracy of how close they got to the tree, and then they would sort of update to go to the next tree

00:30.800 --> 00:32.570
in the particular circuit.

00:32.570 --> 00:37.910
And then we updated that and went to a progress tracker where the tracker actually went around in the

00:37.910 --> 00:39.230
tanks, followed the tracker.

00:39.320 --> 00:46.700
What I'd like you to do for this particular video is cast your mind back to when we had just the tanks

00:46.700 --> 00:52.070
going around between the trees without the progress tracker, because we're not going to use the progress

00:52.070 --> 00:53.840
tracker in this particular case.

00:53.900 --> 01:00.410
And it will also give you good experience of writing code to get something to go between waypoints.

01:00.410 --> 01:04.670
Because trust me, I have done it I think a million times.

01:04.760 --> 01:11.150
So what we're going to do is remind ourselves of that so you guys can go and find where yours is and

01:11.150 --> 01:11.670
open that up.

01:11.690 --> 01:13.890
I'll just put that out of the road for now.

01:13.910 --> 01:16.520
Now, we're going to go back into this project.

01:16.520 --> 01:22.280
And first of all, we're going to go into the graph code because I want to make a slight change in here.

01:22.460 --> 01:30.890
Path List is going to be your list of nodes that you should follow in the tank to get from one location

01:30.890 --> 01:32.090
to the other.

01:32.170 --> 01:32.610
Okay.

01:32.630 --> 01:38.930
Now, we're going to need this back in our follow up code because it's what's going to be following

01:38.930 --> 01:40.490
these nodes along.

01:40.490 --> 01:46.040
So I'm just going to make this public for now because that's just going to make life a lot easier.

01:46.040 --> 01:52.280
If you want to add methods for getting the path list and returning it, that's completely up to you.

01:52.280 --> 01:53.750
But this is just easier.

01:54.530 --> 01:57.260
So we're going to save that in here in the graph.

01:57.260 --> 02:00.290
Now switch back into follow up.

02:00.680 --> 02:03.560
What I'd like you to do as a challenge.

02:03.560 --> 02:04.040
Yes.

02:04.040 --> 02:07.310
Is make this a late update for a start.

02:07.310 --> 02:18.230
And then within here write the code so that if we go go to heli is run, then late update is going to

02:18.230 --> 02:23.930
go through the process of moving from one waypoint to another.

02:24.380 --> 02:33.680
And in fact, just to get this to run to start with, up in the start, we're going to put in an invoke

02:34.250 --> 02:42.680
and we'll invoke go to run in 2 seconds.

02:42.980 --> 02:51.050
We're doing that so that the rest of the code and rest of unity has timed actually populate all of the

02:51.050 --> 02:53.300
things in the inspector that it needs to do.

02:53.300 --> 02:59.480
Because if you run all of this in the start and then you're trying to go go to ruin, it could be that

02:59.480 --> 03:07.970
the waypoints have not been currently set up in the waypoint manager and that this will fail because

03:07.970 --> 03:11.540
it can't find the starting and the ending positions.

03:11.570 --> 03:14.450
Trust me, I know because I actually tried it and it didn't work.

03:14.450 --> 03:17.310
So that's why I put the invoke in here for 2 seconds.

03:17.330 --> 03:23.060
Now the other thing you might like to do too, so that you don't end up spinning around and ran around.

03:23.060 --> 03:29.380
One waypoint is come up to the accuracy and change that to a five.

03:29.390 --> 03:34.130
I found that actually works a little bit better for problem solving this.

03:34.310 --> 03:36.850
Okay, so we're going to save this.

03:36.860 --> 03:41.810
Now it's your turn to see if you can write that late update code.

03:41.810 --> 03:44.870
So pause the video now go give it a go.

03:44.870 --> 03:47.750
When we come back, I'll step through it with you.

03:49.120 --> 03:50.260
Welcome back.

03:50.290 --> 03:51.430
How did you go with.

03:51.430 --> 03:52.750
That was a big task.

03:52.780 --> 03:57.180
Now, there's numerous ways you could lay this code out to get it to work.

03:57.190 --> 03:59.780
I'm just going to show you my way.

03:59.800 --> 04:05.200
You might come up with something completely different, which still works quite well, and that's absolutely

04:05.200 --> 04:06.130
fine.

04:06.220 --> 04:08.110
Okay, so what are we going to do?

04:08.140 --> 04:15.220
First of all, is check whether we have a path that we can follow and that we're not already at the

04:15.220 --> 04:17.500
end of the path that we're following.

04:17.500 --> 04:32.590
So if G dot path list dot length count equals zero or current waypoint equals.

04:32.590 --> 04:37.630
So the index of the current waypoint equals g dot path list.

04:38.540 --> 04:39.920
Dot count.

04:40.970 --> 04:44.180
If that's the case, then we're just going to return because we've got nowhere to go.

04:44.180 --> 04:48.530
We're either have no path or we're at the end of the path we were following.

04:48.710 --> 04:55.580
Okay, so now we're going to test whether we've got close enough to the waypoint we're heading towards

04:55.580 --> 04:58.790
so that we can switch to the next waypoint.

04:58.790 --> 05:11.810
So if vector three dot distance of g dot path list at position current WP.

05:13.450 --> 05:16.450
Dot transform dot position.

05:18.280 --> 05:21.060
Now this is a path list of nodes.

05:21.070 --> 05:27.880
Now this will be a dot get ID on it because that's what's got the game object on it.

05:27.880 --> 05:34.290
We want to make sure that we turn the node into the game object in order to get the position.

05:34.300 --> 05:41.920
Now the distance between that and where the tank currently is, which will be this dot transform dot

05:42.040 --> 05:48.100
position and that is then going to be tested against our accuracy, which is the same thing that we

05:48.100 --> 05:52.470
did where we had the tanks racing around the palm trees.

05:52.480 --> 05:57.640
Okay, so if this is the case, we've got close enough to the waypoint we're currently going for so

05:57.640 --> 05:59.560
we can go to the next waypoint.

05:59.950 --> 06:06.970
Now, if we aren't at the end of our path, if we're still following waypoints around, we're going

06:06.970 --> 06:08.860
to go if current.

06:09.940 --> 06:17.260
Waypoint is less than g dot path list dot count.

06:17.920 --> 06:25.660
Then our goal will equal g dot path list at position.

06:25.810 --> 06:36.670
Current waypoint dot get id dot transform vector three.

06:38.040 --> 06:39.780
Look at goal.

06:39.840 --> 06:45.690
This is the direction we need to look to be looking towards so we can actually slurp our rotation around

06:45.690 --> 06:46.590
for our tank.

06:47.460 --> 06:54.750
New vector three go dot position dot x.

06:55.980 --> 07:08.760
And then this dot transformed position dot y and then goal dot position dot z.

07:08.790 --> 07:17.460
The reason that we're taking the tanks y position is so the tank doesn't actually tip forward or backwards,

07:17.700 --> 07:20.400
so it stays level on the Y.

07:20.430 --> 07:28.650
Even if you have put your waypoint positions slightly up or down from the tanks y position, this will

07:28.650 --> 07:31.050
just make sure you keep the tank level.

07:31.290 --> 07:31.830
Okay.

07:31.830 --> 07:45.390
Now vector three direction equals look at goal minus this dot transform dot position and then we can

07:45.390 --> 07:46.230
do our rotation.

07:46.230 --> 08:00.570
So this dot transform dot rotation equals turn in dot slurp this dot transform dot rotation and could

08:00.630 --> 08:03.270
turn in dot look.

08:04.300 --> 08:07.240
Rotation in direction.

08:08.860 --> 08:17.590
And then our speed, which will be time, delta time multiplied by our speed value.

08:18.490 --> 08:20.770
And then we simply just push our tank forward.

08:20.770 --> 08:23.080
So this dot transform.

08:25.430 --> 08:27.680
Dot translate.

08:30.040 --> 08:37.990
Zero zero spade multiplied by time dot delta time.

08:39.640 --> 08:46.990
One little thing I forgot to put in this code before you continue onwards is to put in this line here

08:46.990 --> 08:55.720
so that when you reach a waypoint, then this current node gets updated because it's this current node

08:55.720 --> 09:02.800
that is being used to basically set the path starting position in all of your go tos.

09:02.800 --> 09:06.220
And if you don't have this, then it just won't act correctly.

09:06.220 --> 09:13.630
So you want to set current node to the current node that you've actually just found, which is with

09:13.630 --> 09:21.280
this distance thing, then you can update current waypoint to be going to the next one and when it finds

09:21.280 --> 09:25.120
the next waypoint, it will update that for the current node as well.

09:25.150 --> 09:26.400
Okay, great.

09:26.410 --> 09:27.460
Save that.

09:27.460 --> 09:30.130
And now we're ready to test it out.

09:30.130 --> 09:33.130
So let's go back into unity.

09:33.250 --> 09:33.670
Okay.

09:33.670 --> 09:40.750
So now what we need to do is get our tank or is it over in the hierarchy and we have this WP manager

09:40.750 --> 09:41.650
exposed here.

09:41.650 --> 09:47.680
We need the WP manager in order to get hold of the graph and the path list and all of those things from

09:47.680 --> 09:48.130
there.

09:48.430 --> 09:49.060
Right?

09:49.060 --> 09:54.220
So let's press play and we'll be able to see our tank moving.

09:54.220 --> 09:58.510
Let's just zoom in in the scene view here so we can see what's happening.

09:58.510 --> 10:06.310
So we're going to start over at Waypoint one now after 2 seconds, we're going to start on our route

10:06.310 --> 10:08.290
and we're going to go this way.

10:08.290 --> 10:10.240
Well, I shouldn't predict where it's going to go.

10:10.240 --> 10:14.440
It's going to take the shortest path that it found.

10:14.470 --> 10:20.590
Of course, it could have gone around this way, I believe, as well, and come up to there.

10:21.310 --> 10:24.250
So now we are at here.

10:24.250 --> 10:27.760
So we can actually program at this point to go back to the helipad.

10:27.760 --> 10:34.570
So it should turn around and follow the points back and we'll come back and put some little buttons

10:34.570 --> 10:37.210
for commands in in the next lecture.

10:37.210 --> 10:43.900
But in the meantime, you might like to program in some other destinations.

10:43.900 --> 10:47.740
Okay, so we've got go to Heli, we've got Go to Ruin.

10:47.740 --> 10:54.490
We could have something like go to what are these oil pumps or factory or whatever it is, go to factory,

10:54.850 --> 10:56.860
go to over there.

10:57.220 --> 10:57.970
It's up to you.

10:57.970 --> 10:59.590
So just put those in.

10:59.590 --> 11:04.720
And that would go into our follow waypoints code with our other go tos in here.

11:04.720 --> 11:09.700
And when we come back in the next lecture, I'll show you how to put some really quick buttons up on

11:09.700 --> 11:15.880
the screen to call of these and get our tank moving around the environment.
