WEBVTT

00:06.070 --> 00:11.500
In this video, we're going to complete our exercise with these waypoints by actually giving commands

00:11.500 --> 00:15.880
to the tank so we can see it going between the different locations.

00:15.880 --> 00:24.010
So what I've done so far with our project that we've created is I've set my camera view to full HD.

00:24.010 --> 00:29.800
We're about to put some buttons on the screen, so having this sort of set to a specific resolution

00:29.800 --> 00:30.430
really helps.

00:30.430 --> 00:32.320
So you'll want to do that.

00:32.320 --> 00:37.480
And now we can create the methods that we want to be called by our buttons.

00:37.480 --> 00:44.590
So if we go back into our follow waypoints we've got so far, go to Heli and go to ruin.

00:44.590 --> 00:51.790
I've also added in a go to factory, so you might want to add in as many of these go tos as you like

00:51.790 --> 00:52.930
to test these out.

00:52.960 --> 01:01.510
Now the factory node in my example is at the waypoints for position in that array.

01:01.870 --> 01:05.020
So how do we hook these up to a button?

01:05.050 --> 01:08.770
Okay, we're going to go over into the hierarchy, right click in here.

01:08.920 --> 01:13.270
We want to go down to the UI and you're looking for the legacy.

01:13.540 --> 01:19.090
We want not going to go into these other things because the legacy is just way quicker and we're not

01:19.090 --> 01:22.690
looking at creating fancy UI's at this point.

01:22.690 --> 01:25.150
So it's just off the screen for me.

01:25.150 --> 01:26.080
Let me just.

01:26.870 --> 01:29.360
Bring this in so you can see it.

01:29.690 --> 01:30.980
Right click.

01:32.010 --> 01:38.640
Create UI and legacy, and then you want to grab the button just there.

01:38.940 --> 01:40.530
So there's one button.

01:41.100 --> 01:49.500
Now once you've got that button, we can go over into our scene view and if you click on the 2D and

01:49.500 --> 01:55.410
then double click on the button and then zoom out, you'll be able to see where the screen space is.

01:55.410 --> 02:00.450
If you're not familiar with the UI system, then you can see it in the 2D view in the scene.

02:00.450 --> 02:02.970
And this is the actual size that we set our canvas to.

02:02.970 --> 02:09.870
So that HD setting where the button is here and where your 3D scene is, you can see it's very, very

02:09.870 --> 02:10.770
tiny down there.

02:10.770 --> 02:16.440
They kind of have nothing to do with each other, so don't get distressed that this is absolutely enormous.

02:16.440 --> 02:21.870
But the button, if we just bring this out, you can see that it's now sitting in the middle there.

02:21.900 --> 02:28.710
The first thing to do in your canvas, if you click on it to make this button scale with the screen,

02:28.710 --> 02:36.720
is to come over into the inspector and just go down to canvas scaler and select scale with screen size.

02:36.990 --> 02:37.470
Okay.

02:37.470 --> 02:44.340
So now we can actually bring this back in here a little bit and we're going to select the button and

02:44.340 --> 02:49.620
then over in the inspector, you want to click on this thing here to bring up the alignment.

02:49.710 --> 02:54.300
You want to hold down your option or your alt key and then just click on that there.

02:54.340 --> 02:57.240
Now that's going to move it up into the top corner.

02:57.240 --> 03:05.610
From there you can actually selected hit the W button if you don't have your axes and I'm having a bit

03:05.610 --> 03:07.980
of trouble here and then you can actually just move it like that.

03:07.980 --> 03:08.190
Okay.

03:08.250 --> 03:11.220
So I just don't want it smack bang in that top corner.

03:12.000 --> 03:14.880
Okay, let's get that saying something.

03:14.880 --> 03:20.760
So over in the hierarchy, drop down the button, you'll find that there's a text area there.

03:20.760 --> 03:29.670
That's where you can write the text on this button and we're going to put in here go to ruin like that.

03:30.120 --> 03:30.660
Okay.

03:30.660 --> 03:33.540
So it's going to change your text here.

03:33.690 --> 03:38.070
We now need to link the button up with our method in our tank.

03:38.070 --> 03:43.830
So select the button in the hierarchy and then come down in the inspector.

03:43.830 --> 03:51.000
You're looking for the button element in there component and we're going down to the on click, so go

03:51.000 --> 03:51.210
on.

03:51.210 --> 03:54.960
Plus you want to find a function that you can run.

03:54.960 --> 03:59.790
Now the function that you can run is in our follow waypoint code, which is attached to our tank.

03:59.790 --> 04:07.530
So we grab our tank and we put that down there and then if we drop this down, you'll see that there's

04:07.530 --> 04:09.660
follow waypoint in there.

04:09.660 --> 04:11.670
Now I need to bring this in again.

04:11.670 --> 04:16.170
So you should be able to see that and follow waypoint.

04:16.170 --> 04:20.580
And then in here you'll find a list of a whole bunch of different things.

04:20.580 --> 04:26.400
But any public methods that you've created in your script you will see here.

04:26.400 --> 04:32.640
So you've got go to factory, go to Heli and go to ruin and go to ruin is the one that we want here.

04:32.670 --> 04:32.930
Okay.

04:33.000 --> 04:36.600
So that is now ready to go.

04:36.600 --> 04:41.760
So let me just bring this screen out so we can see it press play.

04:42.540 --> 04:51.900
And once that starts up, we can hit go to ruin and he will immediately go to the ruin.

04:51.900 --> 04:59.100
Now the other thing that I haven't done is get rid of that delayed start for the go to ruin in the scene.

04:59.100 --> 05:01.080
But anyway, you can see that's working there.

05:01.110 --> 05:07.650
Let's go back into our code here and remove that so it doesn't automatically run.

05:08.100 --> 05:14.820
Now, before we continue adding more buttons, we need to actually check that if our destination is

05:14.820 --> 05:20.730
where we want to head to, then we don't need to find a path currently as it is.

05:20.730 --> 05:28.530
If you select a starting and ending destination that are exactly the same, it's going to fail at finding

05:28.530 --> 05:37.200
a path and it will then give you the previous path because the calculating path which is down here,

05:37.200 --> 05:38.790
reconstruct path won't run again.

05:38.790 --> 05:40.230
And that's what's doing the clearing.

05:40.230 --> 05:45.240
So what you can see that I've done, if I come up into here, I've got some de bugs because I was trying

05:45.240 --> 05:48.720
to sort out this issue at the top of the star.

05:48.720 --> 05:50.640
You want to add this in here?

05:50.640 --> 05:56.550
If your starting and ending points are exactly the same, then you just want to clear the path list

05:56.550 --> 06:01.320
and return false, which is going to stop your tank from doing odd things.

06:01.830 --> 06:02.370
All right.

06:02.370 --> 06:05.880
So we're all ready now to add some more buttons for all of these.

06:05.880 --> 06:11.010
Now, I've just got to add go to hell and go to factory, but you might have some more that you want

06:11.010 --> 06:11.490
to add in.

06:11.490 --> 06:13.140
So this is your challenge.

06:13.140 --> 06:18.630
What I'd like you to do is go ahead and add those buttons yourself so that can get your tank to navigate

06:18.630 --> 06:21.150
around when you click on them.

06:21.150 --> 06:21.870
Pause the video.

06:21.870 --> 06:23.250
Now go do that a go.

06:23.250 --> 06:28.020
And when we come back, I'll step through hooking up my other methods.

06:29.200 --> 06:30.080
Welcome back.

06:30.100 --> 06:32.010
How did you go with that task?

06:32.020 --> 06:37.450
Well, let's create two more buttons for mine, because I've only got two other methods.

06:37.450 --> 06:40.900
I'm going to select this button and duplicate it.

06:40.900 --> 06:47.800
So control D and then just move that one down and then duplicate it again and move that one down.

06:47.800 --> 06:57.250
Now, this button here, if we come over and find it and the text will put this one to go to helipad

06:57.490 --> 07:03.100
and the next one at the bottom that can be go to factory.

07:03.820 --> 07:04.300
Okay.

07:04.300 --> 07:07.270
So let's go back to the helipad button.

07:07.270 --> 07:13.720
And if we look down through the list because we copied it, it's already linked up to the go to ruin

07:13.720 --> 07:15.940
from the go to ruin button.

07:15.940 --> 07:21.520
So all you got to do is actually just select that and go into your follow waypoint.

07:21.520 --> 07:23.170
Again, that's off the screen.

07:23.170 --> 07:26.380
Bring this in here and follow Waypoint.

07:26.410 --> 07:28.750
Now, this is the helipad.

07:28.990 --> 07:31.750
Go down to the go to Heli for that one.

07:31.750 --> 07:34.000
And while we're here, we'll do the other one as well.

07:34.000 --> 07:40.180
So that one will be go to factory like that.

07:40.360 --> 07:40.900
Okay.

07:40.900 --> 07:47.590
So we can now actually run this and have a look at the behavior of the tank.

07:47.590 --> 07:49.270
So let's press play.

07:50.220 --> 07:50.700
Okay.

07:50.700 --> 07:53.760
So we're going to go to the factory.

07:53.760 --> 07:57.240
And yes, if you noticed, I did fix up my spelling mistake I had in that button.

07:57.240 --> 08:01.920
I've also sped up the time this takes to actually run with a time.

08:02.090 --> 08:04.110
Time scale equals five.

08:04.110 --> 08:09.770
Put in any of the starting methods that are going to run, which you'll just make testing a lot easier.

08:09.780 --> 08:13.650
And now let's go to the ruin and ruins up in the back corner.

08:13.650 --> 08:17.070
Now, if we try and go to the same place again, it won't let us.

08:17.070 --> 08:25.530
But we can go back to the helipad and then we can go back to the ruin and we can go to whatever station

08:25.530 --> 08:28.710
that you set up in there for your waypoints.

08:29.430 --> 08:36.660
Now, the important thing with this is that the tank stay on the path denoted by the waypoints.

08:36.660 --> 08:41.970
For example, if you put your tank over here, it's not near any particular waypoints.

08:41.970 --> 08:49.320
And if you want it to get to the closest waypoint, then it might actually have to go through geometry.

08:49.320 --> 08:54.570
And that defeats the purpose of why we have the waypoints in the first place, which is to essentially

08:54.570 --> 08:59.460
allow it to navigate without seemingly colliding with anything.

08:59.610 --> 09:07.470
So that means you really want to have the tank start on one of these waypoint nodes.

09:07.530 --> 09:13.920
So if it's at position one, as I said in the first video for this section, then you want to make sure

09:13.920 --> 09:17.520
that the code has it set up being at position one.

09:17.520 --> 09:21.690
So if we just go back to its code in follow waypoint, let me open that up.

09:22.320 --> 09:24.840
Up in the start where we set the waypoint that we're on.

09:24.840 --> 09:29.610
So Waypoint zero in this case, that's because we're at Waypoint zero here.

09:29.730 --> 09:37.650
But if we wanted to put our tank on the other side of the map, so let's just grab our tank and drag

09:37.650 --> 09:38.910
it over.

09:39.180 --> 09:41.490
Oh, not the top of the tank.

09:42.990 --> 09:50.430
Let's just grab the tank and we'll drag it over to here now because we're over here, we're starting

09:50.430 --> 09:53.010
at whatever that waypoint is just there.

09:53.010 --> 09:54.890
So let me zoom in and have a look.

09:55.140 --> 10:00.720
It's Waypoint five now in our waypoint system.

10:00.780 --> 10:02.940
Then we need to find out which one that is.

10:02.940 --> 10:07.470
So in our manager five is in position five.

10:07.470 --> 10:11.280
So this was element four in that list.

10:11.280 --> 10:19.260
So it's woops four so it means we go back into our code and just set this to starting at position four,

10:19.920 --> 10:20.880
save that.

10:20.880 --> 10:31.410
Then if we go back into unity, we can now press play and we're already at position five when I think

10:31.410 --> 10:32.430
I've got that as my factory.

10:32.430 --> 10:36.000
So if we hit go to factory, it's not going to go anywhere because it's already at the factory.

10:36.000 --> 10:44.520
So we can go to the helipad from here like super fast, and then we can go to the ruin and we can go

10:44.520 --> 10:46.920
wherever we like as long as it's on the path.

10:46.920 --> 10:52.050
And that is the most important thing about having a waypoint system is you don't want your tank going

10:52.050 --> 10:53.370
through any of the geometry.

10:55.250 --> 10:55.940
All right.

10:55.940 --> 11:03.260
So that is setting up waypoints and travelling between them using the estar algorithm.

11:03.290 --> 11:09.110
Now what we're going to do a little later in the course is actually look at the nav mesh system that

11:09.110 --> 11:10.610
comes with unity.

11:10.610 --> 11:18.920
And that nav mesh system to get from one place to another actually implements a star behind the scenes.

11:18.920 --> 11:21.050
So you don't need to code it.

11:21.050 --> 11:28.910
And for doing this exact same exercise, we can use the nav mesh instead, which just gives you a little

11:28.910 --> 11:34.760
bit more power in moving agents and working out the size and having multiple agents and not really having

11:34.760 --> 11:41.360
to worry about them colliding or any of the other sort of physics that might be involved with you having

11:41.360 --> 11:43.730
to do it manually like we're doing here.

11:43.730 --> 11:49.310
So that's the end of this section and we'll come back and start looking at NAV measures and what they

11:49.310 --> 11:53.930
are and then how waypoints fit into that in the next section.
