WEBVTT

0
00:11.100 --> 00:17.350
Welcome back to Part 2 of this lecture in creating waypoints and A* pathfinding.

1
00:17.550 --> 00:23.820
So far we've created a map with a tank which is our NPC and a series of waypoints that we've created

2
00:23.890 --> 00:27.090
edges between to construct a graph.

3
00:27.090 --> 00:34.680
We've also added the graph code in, which contains an A* algorithm and we'll be able to ask the graph

4
00:34.680 --> 00:41.970
to find us a path between one and another of our waypoints. In the FollowPath code that is attached

5
00:42.000 --> 00:43.560
to our tank,

6
00:43.560 --> 00:45.410
there's two public functions.

7
00:45.540 --> 00:48.570
One is to go to the helipad or GoToHeli

8
00:48.930 --> 00:54.030
and the other one is GoToRuin, which will take us up to the back of the map where there's a ruined

9
00:54.030 --> 00:55.290
tank there.

10
00:55.680 --> 01:03.330
Each of these runs the A* algorithm from our current node to a specified position based on

11
01:03.330 --> 01:07.450
where the waypoint is, in the waypoint array.

12
01:07.470 --> 01:15.120
Now our have job is to call these functions from outside of here and use buttons.

13
01:15.150 --> 01:23.320
So with all that done and ready to go, let's go over to the Hierarchy and we'll just right-click, go down

14
01:23.330 --> 01:32.370
to 'UI' and find 'Button'. That's going to create a Canvas, a Button (obviously) which will be childed

15
01:32.370 --> 01:33.580
to the canvas

16
01:33.900 --> 01:35.990
and this EventSystem.

17
01:36.240 --> 01:38.780
Now this button will appear in the scene.

18
01:38.790 --> 01:46.830
If you go to the 2D view and just zoom out a little bit, we'll find the button...there it is and it's showing up

19
01:46.890 --> 01:47.670
on the canvas.

20
01:47.680 --> 01:54.720
This white frame shows you the extremes of what you've got in the Game view, which is here. You can see the button

21
01:54.720 --> 01:58.380
there and the scene itself is down here.

22
01:58.380 --> 02:07.360
If you haven't used the UI system before, just note that the scene itself (the 2D scene) and the 3D scene are

23
02:07.370 --> 02:09.340
not related to each other

24
02:09.390 --> 02:14.290
in this view. So don't think that you need to move the button and stick it over the top of the landscape

25
02:14.400 --> 02:16.570
because that's just not how it works.

26
02:16.590 --> 02:20.820
This button is on this screen, within these dimensions.

27
02:20.820 --> 02:22.940
We're going to move it up into the top corner,

28
02:22.950 --> 02:31.860
so just select the button and click on this Rect Transform location box and we want to click the

29
02:32.040 --> 02:39.150
top corner and we want to hold down the Alt-key at the same time and click it again, which will reposition

30
02:39.150 --> 02:40.980
the button into that top corner.

31
02:40.980 --> 02:46.130
Now I don't want that button EXACTLY up in the top corner, so I'm going to select it and just move it

32
02:46.140 --> 02:53.330
down a little bit like that, so when you look at the game view it's not jammed right up there, it's right there.

33
02:53.430 --> 02:53.790
All right.

34
02:53.790 --> 03:00.000
So the other thing we want to do is change that text. Under the button, if you click down, there's a text

35
03:00.090 --> 03:00.620
element

36
03:00.660 --> 03:03.790
and this gives us the text showing on the button.

37
03:03.890 --> 03:12.570
This will be 'Go to Helipad' and that's what it's going to do when we click on it. Before we can have the

38
03:12.570 --> 03:14.820
button run the code that we want it to,

39
03:14.820 --> 03:22.530
that code needs to exist in the Hierarchy somewhere and currently, it doesn't. The FollowPath code is ready

40
03:22.530 --> 03:29.780
to be attached to our tank. Select your tank, drag-and-drop the FollowPath onto it.

41
03:29.790 --> 03:35.880
Now if you remember, there's a waypoint manager reference that it needs to have, so that it can

42
03:35.880 --> 03:39.230
grab hold of all the waypoints and the graph etc.

43
03:39.420 --> 03:44.630
So just get your WPManager and make sure you connect that up to your tank.

44
03:44.760 --> 03:52.680
With that done, we can now go back to the button and at the bottom of the buttons Inspector, there's an

45
03:52.680 --> 03:54.240
'On Click' area.

46
03:54.270 --> 03:57.470
Now it might look like this list is empty.

47
03:57.510 --> 04:03.390
Click on the '+' sign and then we want to select the object that has that code.

48
04:03.390 --> 04:04.190
That's that tank.

49
04:04.190 --> 04:10.090
So put the tank down in there and we select the function.

50
04:10.220 --> 04:15.670
So drop down this box and you'll see that it's got FollowPath attached to it.

51
04:15.680 --> 04:19.570
Now in the list (that's just off your screen - we can bring this in a bit)

52
04:23.110 --> 04:30.100
'FollowPath'  and then it'll show you whatever it can run that's associated with that FollowPath code

53
04:30.130 --> 04:35.290
and you'll find your GoToHeli and GoToRuin functions in there.

54
04:35.380 --> 04:37.810
So we can just select that GoToHeli

55
04:37.960 --> 04:45.010
and now when that button is clicked, it will run that function on the tank, which is so nice and convenient.

56
04:45.040 --> 04:50.080
The last thing we need to do before pressing Play, is make sure that we'll be able to see what's going on in

57
04:50.080 --> 04:56.890
our Game scene. You can see here that the game is currently not looking where we want it to.

58
04:56.890 --> 05:06.010
So I'm going to go to the Scene and go out of 2D view, find the tank and then scroll out and

59
05:06.010 --> 05:07.980
just manipulate this around.

60
05:08.050 --> 05:12.020
I just want to be able to see what's going on when we run it, basically.

61
05:12.060 --> 05:14.000
So there we go.

62
05:14.070 --> 05:23.410
Now we'll select the camera and go 'GameObject > Align With View' so that the game is now looking

63
05:23.410 --> 05:26.550
at the scene where we'll be able to see what's going on.

64
05:26.560 --> 05:28.690
Right so now press Play.

65
05:29.080 --> 05:30.190
Nothing will happen.

66
05:30.300 --> 05:32.540
Notice that waypoints all disappear

67
05:32.560 --> 05:37.990
and that's because the graph code itself will grab hold of all of those nodes and turn off all of the

68
05:37.990 --> 05:43.770
things that are showing up, so that you're not going to see them in the final running of the game.

69
05:44.050 --> 05:49.750
Press [Go to Helipad] and you can see that the tank goes to the first waypoint that it needs to because

70
05:49.750 --> 05:51.450
it is closest to here

71
05:51.670 --> 05:56.290
and then it will take the path directly from there to there

72
05:56.290 --> 06:01.620
and when it gets to the heli, it will stop because it's got nothing else to do from there.

73
06:01.840 --> 06:02.090
Ok.

74
06:02.110 --> 06:09.130
So let's now program to go to the Ruins.  The process is the same, grab hold of the button on the canvas and

75
06:09.130 --> 06:15.490
just press Ctrl-D, which will create you a duplicate button. Go to the Scene and go to the 2D version

76
06:15.550 --> 06:23.920
of the scene,find that button, now to select it just click on this icon here, so that you get the little

77
06:23.920 --> 06:30.400
blue dots around it. That allows you to resize it and move it. Make sure you're moving the right one. Move that down

78
06:30.610 --> 06:32.920
just underneath it and while we're

79
06:32.920 --> 06:40.360
there, we're going to change the text to Go to Ruin.

80
06:40.410 --> 06:44.750
Now it's going to have an 'On Click' on its button, so you go up to the button.

81
06:44.910 --> 06:50.060
It's got the 'GoToHeli()' on it because we duplicated it from the previous button.

82
06:50.070 --> 06:56.500
Now we can just find the other function, which is 'GoToRuin' and add that in there instead.

83
06:56.820 --> 07:04.610
All right, so now let's press Play and we should be able to go to the helipad, which will take our tank

84
07:04.630 --> 07:07.530
first of all to our current way point down to here,

85
07:07.540 --> 07:10.140
at which point it will set that to the current waypoint...

86
07:12.850 --> 07:16.510
and stop and then click [Go to Ruin].

87
07:16.510 --> 07:20.880
Now it's not going to take this path back up to there because it's much quicker for it to go this way

88
07:20.890 --> 07:26.290
because of the distance that the A* is calculating and you can see now that it's following the

89
07:26.290 --> 07:32.470
path along, there was a waypoint there, there's a waypoint over here that it has to get to, to avoid these

90
07:32.470 --> 07:33.290
rocks

91
07:33.670 --> 07:37.230
and then it will go to the waypoint up there.

92
07:38.750 --> 07:39.350
Ok, great.

93
07:39.350 --> 07:40.520
So that's working.

94
07:40.520 --> 07:46.310
Last thing to try is the point that we had over here where there's only a one-way connection.

95
07:46.310 --> 08:00.080
So we'll duplicate the button for Ruin, move it  down and then we'll change the text to 'Go To Tanks'

96
08:00.110 --> 08:07.510
and for its button, it's going to run a function

97
08:07.510 --> 08:08.630
we haven't written yet.

98
08:08.710 --> 08:16.800
So we need to go back into our FollowPath code and we're going to copy one of the 'GoTo' methods that we had

99
08:16.800 --> 08:17.580
before

100
08:18.550 --> 08:30.010
and paste it underneath and this time we will 'GoToTanks' and the tanks are at waypoint number

101
08:30.010 --> 08:37.780
9 (which is 8 in the array) and we make sure that we set the current waypoint back to 0 before

102
08:37.780 --> 08:39.720
we get going.

103
08:39.760 --> 08:49.780
So save that, switch back into Unity and then grab hold of that button and set its function for the

104
08:49.780 --> 08:54.980
one we just wrote, which was 'GoToTanks'. With all that saved,

105
08:55.030 --> 08:56.700
we're ready to play.

106
08:56.710 --> 09:00.530
So let's begin by going straight to the tank.

107
09:00.550 --> 09:04.730
First of all, it has to go to its current waypoint, which is this first one up here

108
09:05.020 --> 09:07.870
and then it will follow this path that it has.

109
09:07.870 --> 09:14.710
It can't zip across to here because it has to come back to this one that's down there, which is our intersection.

110
09:14.740 --> 09:20.980
Now it's going to come around and end up down near the storage tanks. At this point,

111
09:21.000 --> 09:28.360
let's say we want to go to the Ruin. The shortest distance would be that way but it can't go back along that

112
09:28.360 --> 09:29.120
path.

113
09:29.140 --> 09:31.650
So let's have a look what happens.

114
09:33.300 --> 09:37.050
It's going to go around because it's found a path that will go around

115
09:39.590 --> 09:46.220
and then it will come back and join up with that waypoint that's just here, to then take the one here

116
09:46.250 --> 09:48.730
and end up over there.

117
09:52.440 --> 10:01.620
So that's the A* algorithm and waypoints and you can see how powerful it is. It's really quick to

118
10:01.620 --> 10:09.810
calculate distances and you can use these for navigating around all sorts of different terrain.

119
10:09.840 --> 10:11.530
So you know, have fun with it!

120
10:11.550 --> 10:16.140
Play with it, put your own maps in and see what you can come up with.