WEBVTT

0
00:11.100 --> 00:16.540
In this lecture, you'll follow along as we start to explore Behavior Trees and we'll start with setting

1
00:16.540 --> 00:21.080
up a very simple Behavior Tree and look at the Sequence node.

2
00:21.340 --> 00:26.740
Now if you get hold of the starter file that comes with this lecture and open it up, you'll find in

3
00:26.740 --> 00:35.470
the RobotGuard Scene, this particular scene. We've got a black guard (he will be the artificial intelligence,

4
00:35.470 --> 00:40.810
the NPC) and we've got a green one which you can drive with the arrow keys.

5
00:41.050 --> 00:48.480
So as it is, if you press Play, you'll find that you can drive the green robot around.

6
00:48.630 --> 00:50.730
It's already set up as a NavMesh,

7
00:50.730 --> 00:57.780
this particular environment and if you hit the spacebar you'll be able to fire. If you hit the collider

8
00:57.870 --> 01:00.180
that's on the bot itself,

9
01:00.690 --> 01:08.030
then you'll also notice that the health bar in the Game view (over here) goes down.

10
01:08.040 --> 01:15.240
It's also programmed to come back up incrementally so that the bot (if it can get away from you) will

11
01:15.240 --> 01:17.500
be able to heal itself.

12
01:17.520 --> 01:21.830
Let's have a really quick look at the way this whole project is set up.

13
01:21.990 --> 01:28.910
The black character is called Droid in the Hierarchy. If you select it, you'll see it has a Sphere Collider

14
01:28.970 --> 01:29.440
around it

15
01:29.460 --> 01:32.240
and this is being used to determine when the bullets hit it

16
01:32.280 --> 01:34.850
and that's the only reason that it's there.

17
01:34.890 --> 01:39.970
Other than that, the bot is being driven by a NavMesh

18
01:39.990 --> 01:46.180
and you see I've added a Nav Mesh Agent here. I've also adjusted the Speed as well as the Stopping Distance so I've got the

19
01:46.260 --> 01:51.450
Stopping Distance as 30 because later on we want the bot to follow us around and we don't want it to

20
01:51.450 --> 01:55.190
get too close when it does get near the player.

21
01:55.200 --> 02:02.960
There's also an A.I. script on the bot, which is quite simple at this point and it's already set up with

22
02:02.960 --> 02:10.230
a spawn point for the bullets, which is going to be sitting here as a little cube attached to it that

23
02:10.230 --> 02:19.170
I've got here as a turret spawn point and it has the shells which we used in the tank example much

24
02:19.230 --> 02:23.140
earlier on in this course as well that it can shoot.

25
02:23.160 --> 02:27.990
It currently won't shoot because we haven't programmed it with any behaviors, so it's just going to sit

26
02:28.050 --> 02:30.970
there and the health bar will go down when we hit its collider.

27
02:30.980 --> 02:38.140
So if we have a look at the code that's on there, we'll find all the usual things that we've used

28
02:38.230 --> 02:44.680
in the past. So we've got a public transform to the player so that the bot knows where the player

29
02:44.680 --> 02:46.490
is, we've got its spawn point,

30
02:46.510 --> 02:51.160
we've got its bullet prefab, we've got our Nav Mesh Agent, destinations,

31
02:51.280 --> 02:58.390
we've got a target that we'll be using a little later for moving the bot around, health, rotation speed,

32
02:58.810 --> 03:00.940
visible range, shot range

33
03:00.970 --> 03:05.240
and we've also got this slider, which is our health bar.

34
03:05.380 --> 03:10.870
So the slider is a UI object that is...back here...

35
03:10.880 --> 03:14.050
you can see it in the Game view on the screen

36
03:14.050 --> 03:21.130
and over in the Hierarchy, under the Canvas, you'll find a slider called DroidHealth that has been set

37
03:21.130 --> 03:21.670
up.

38
03:21.820 --> 03:27.200
The slider has a value that goes from 0 to 100

39
03:27.370 --> 03:34.450
and it's this value here (the value right at the bottom) that we set in the code which will make our health

40
03:34.450 --> 03:43.570
bar go down and up. So just back in that code, if we go down to here there's an UpdateHealth method that

41
03:43.570 --> 03:51.270
is being called every half-a-second, thanks to an InvokeRepeating sitting up in the Start.

42
03:51.280 --> 03:58.240
So this is just going to tick over and test if our health has gone down below 100, then it will just

43
03:58.350 --> 04:01.720
tick it up and repair the bot.

44
04:01.720 --> 04:08.860
Now the position of that health bar (if you haven't done anything like this before) is in the Update.

45
04:08.860 --> 04:12.520
It will keep that slider on the screen

46
04:12.520 --> 04:17.740
(so on the Graphical User Interface) just above where the bot is sitting.

47
04:17.740 --> 04:20.380
So as the bot moves around, it sticks to it basically

48
04:20.380 --> 04:27.370
but because the graphical user interface and the bot are in 2 different spaces (one is a 2D plane

49
04:27.760 --> 04:29.330
and one's a 3D space)

50
04:29.350 --> 04:36.910
we need to convert the coordinates of the position of the bot itself (which is this part here) into

51
04:37.210 --> 04:43.660
screen coordinates with a Camera.main.WorldToScreenPoint and then the rest of the code takes

52
04:43.660 --> 04:50.710
care of making sure that the health bar stays above that bot and you can see that the health bars value

53
04:50.720 --> 04:55.500
is also set in the Update just here.

54
04:55.510 --> 05:03.430
Now further down is the code for detecting a bullet hit which is our OnCollisionEnter code, which is

55
05:03.430 --> 05:10.610
just checking that the bot has been hit by something that is tagged with the name 'bullet'

56
05:11.050 --> 05:13.580
and then the health is reduced by 10.

57
05:13.690 --> 05:19.570
So there's really not a lot of code in there because the agent's not doing anything at the moment.

58
05:19.640 --> 05:22.140
Ok, so that's the A.I. code.

59
05:22.270 --> 05:29.770
Now the other things to take note of is that the green bot (or our character, the player character) is

60
05:30.370 --> 05:36.100
driven by a Drive code that's attached to it and we've used that before for just moving an object around with

61
05:36.160 --> 05:42.360
the arrow keys. Over in the Assets, you'll find a folder called PandaBehavior

62
05:42.640 --> 05:45.260
and this is the Panda Behavior Trees

63
05:45.410 --> 05:50.980
plug-in, which you can get from the Asset Store (here it is, here).

64
05:51.100 --> 05:56.770
There's a free version and there's a paid version with a few extra features but it's nice that we've

65
05:56.770 --> 06:02.110
got this free version to play around with and that you can see the value of it.

66
06:02.200 --> 06:08.900
Now while I'm just talking about this, there's a number of assets in the Asset Store for creating

67
06:08.920 --> 06:12.490
Behavior Trees and they're not free

68
06:12.580 --> 06:20.260
and they also include Graphical User Interfaces, so they would be a lot more powerful than what we're

69
06:20.260 --> 06:21.430
going to learn here

70
06:21.460 --> 06:26.830
because you can use drag-and-drop nodes and create a tree visually for the behavior and then

71
06:26.830 --> 06:30.940
link that up with the behavior of your particular bots.

72
06:31.040 --> 06:38.950
However, if we're in here, digging around and actually writing scripting code for the Behavior Tree, it'll give

73
06:38.950 --> 06:44.740
you a better sense of how things are connected and how the Behavior Trees work.

74
06:44.740 --> 06:47.470
So this is a really good introduction to Behavior Trees

75
06:47.470 --> 06:52.510
and then if you want to go ahead and purchase one of those other assets that has the graphical user

76
06:52.510 --> 06:58.360
interface, then feel free and you'll already understand what Behavior Trees are and then how to

77
06:58.840 --> 07:00.970
create more complex ones.

78
07:00.970 --> 07:05.650
Now this is already included and downloaded with the file you would have just opened, so you don't need

79
07:05.650 --> 07:07.210
to do it again.

80
07:07.240 --> 07:15.100
Let's start by creating a really simple script that's going to cause the Droid to wander. In programming

81
07:15.100 --> 07:22.180
the 'wander', if you think back about NavMeshes, the way you would do that is to pick some random destination

82
07:22.390 --> 07:28.120
on the NavMesh and then get the robot to move towards it.

83
07:28.120 --> 07:30.610
Now the NavMesh is doing all of that work for us.

84
07:30.610 --> 07:39.330
The only thing we need to really check is if the bot has finished getting to its destination point and

85
07:39.330 --> 07:44.740
we can include all of these functionalities into a really simple Behavior Tree.

86
07:44.760 --> 07:51.270
So first of all, we'll create the Behavior Tree. So under Project, drop down "Create'. Now you won't see

87
07:51.270 --> 07:57.510
it here because it's off my screen and it's right down at the very bottom when you've got the Panda BT

88
07:57.570 --> 08:00.740
plug-in in, it'll say 'Panda BT Script'.

89
08:00.750 --> 08:05.180
So create a new one of those and I will add it into the asset.

90
08:05.190 --> 08:12.510
Let's call it 'Wander.BT'. It doesn't really matter what you call it, as long as you know what it

91
08:12.510 --> 08:13.020
is.

92
08:13.020 --> 08:14.690
So there's your Wander.BT.

93
08:14.720 --> 08:22.980
Now in this (let's open this up) you're going to get a very simple structure of a very basic tree.

94
08:23.010 --> 08:29.880
First of all, the top level of the tree that you're going to run has to be called the 'Root' node and you

95
08:29.880 --> 08:31.170
can see that there

96
08:31.590 --> 08:33.600
and then underneath, there's a Succeed.

97
08:33.630 --> 08:36.480
So this is a root node, all by itself.

98
08:37.070 --> 08:44.550
It's got no children and it's running a success, so it's always going to be true. It's going to

99
08:44.550 --> 08:47.590
let the tree continue running.

100
08:47.610 --> 08:54.050
Now, this is called Wander but we'll still call it 'Root' because it is the first node in our tree.

101
08:54.070 --> 09:02.730
As we work down through the tree, each successive branch and node structure has to be tabbed in one

102
09:02.820 --> 09:04.110
for it to work.

103
09:04.110 --> 09:11.670
So if I did that, that would be an error because you can't have 2 root nodes.

104
09:11.760 --> 09:18.030
So it would look like that, where you've got your initial node and then one branch going straight down

105
09:18.030 --> 09:19.560
to the Succeed

106
09:19.620 --> 09:24.510
but we're not going to do this. We're going to get rid of the Succeed and replace it with a 'sequence'.

107
09:26.580 --> 09:35.010
So these words are words that the Panda BT script compiler knows what they are and it knows that after

108
09:35.010 --> 09:42.870
we put in a 'sequence' node, that we are going to then give it a series of leaf nodes or we could even

109
09:42.870 --> 09:46.610
give it a 'sequence' or a 'selector', if you want to go deeper and deeper into the tree.

110
09:46.620 --> 09:53.520
We're not at this point, so we're going to give it 2 children nodes that will be the actions we want

111
09:53.580 --> 09:54.810
our bot to take.

112
09:54.820 --> 09:59.180
So the first one we'll call PickRandomDestination

113
09:59.370 --> 10:10.060
and the second one we'll call MoveToDestination. The spelling of these has to be perfect

114
10:10.480 --> 10:17.050
and I mean that in the way that THIS spelling has to match the name of a method that you are now going

115
10:17.050 --> 10:20.570
to write back in your A.I. file.

116
10:20.920 --> 10:23.100
If it doesn't, you'll get an error obviously.

117
10:23.110 --> 10:29.200
So just keep note of how you've spelled both of these 2 things, that you've also tabbed them in.

118
10:29.350 --> 10:32.320
Right, so that is the end of this Behavior Tree.

119
10:32.320 --> 10:40.980
So just save that, go back to Unity and we're going to open up the AI script file.

120
10:40.980 --> 10:49.330
In here is where we'll add 2 methods that correspond to those 2 leaf nodes that we just

121
10:49.360 --> 10:56.370
added in the Behavior Tree. So down here, we're going to add this.

122
10:56.440 --> 11:04.780
The first one is called PickRandomDestination, which is spelled exactly the same in the Beahvior Tree here.

123
11:04.870 --> 11:09.140
Now back in the AI.cs file, we're creating

124
11:09.250 --> 11:15.870
the PickRandomDestination method and notice that beforehand, it's preceded with this little [Task]

125
11:15.880 --> 11:19.170
and this is telling the Behavior Tree

126
11:19.210 --> 11:28.080
(the Panda BT system) to pick this up as a task, which will link it back to the leaf node in your BT file.

127
11:28.090 --> 11:31.950
It knows that because at the top of the code (if we just go back up here)

128
11:31.960 --> 11:35.770
we've got our Panda library included.

129
11:35.980 --> 11:36.280
Right.

130
11:36.280 --> 11:38.830
So, let's just go back down here.

131
11:38.830 --> 11:49.120
All we're doing in PickRandomDestination is creating some point on our map that is within a range

132
11:49.150 --> 11:51.130
of -100 to 100.

133
11:51.220 --> 11:56.030
If you have a look at the plane that's in the scene, it's actually sitting at 0, 0, 0

134
11:56.230 --> 12:02.830
and so we're going to stay at 0 in the y-direction but just find a point that's within 100 radius

135
12:03.160 --> 12:12.350
of the bot itself and then we'll use this point as its destination for the Nav Mesh Agent. At this point, we're

136
12:12.700 --> 12:16.330
returning a task 'Succeed'.

137
12:16.330 --> 12:20.350
So you've got to send a succeed message back to the Behavior Tree,

138
12:20.350 --> 12:24.040
otherwise it won't keep executing its sequence.

139
12:24.040 --> 12:28.840
Now you can see this down in the next task, which is MoveToDestination

140
12:28.960 --> 12:37.110
and in this code, I've also included some debug info which is quite useful when you're running a

141
12:37.140 --> 12:41.030
Behavior Tree, to see where things are working and when they're not working.

142
12:41.050 --> 12:46.780
So this is saying when the task is being inspected (and that's in the debugger that I'll show you in

143
12:46.780 --> 12:53.010
a moment for this) then it's going to print (next to this task, in the debugger)

144
12:53.010 --> 13:02.280
a string, formatted like 't=' and then this here and print out the time and this string format,

145
13:02.320 --> 13:04.130
you can put anything into here.

146
13:04.260 --> 13:08.910
So later on when we're turning the bot through certain angles, you could put the angle in here so

147
13:08.910 --> 13:15.860
that you can easily debug as you're going along, where things might not be working for you.

148
13:15.900 --> 13:18.180
So that's a nice little feature of this.

149
13:18.180 --> 13:23.430
Now the other thing we've got under here, which is actually doing all the work in this particular task

150
13:23.910 --> 13:29.810
is just testing if the agent has made it to its destination.

151
13:29.820 --> 13:37.500
So we're doing an 'agent.remainingDistance <= agent.stoppingDistance'

152
13:38.240 --> 13:50.790
and that says I'm at my destination. Now we're also testing that agent.pathPending is false.

153
13:50.790 --> 13:58.230
If agent.pathPending is true, that means that the Nav Mesh Agent hasn't finished calculating a path

154
13:58.530 --> 14:09.330
and if you try and run these tasks out of sync with the NavMesh updating itself, then this task

155
14:09.390 --> 14:13.290
could actually fail, rather than succeed.

156
14:13.300 --> 14:20.880
So when you set this destination up here, you can't be assured that it has calculated the path and therefore

157
14:20.890 --> 14:25.770
will start moving to it, before this task down here is run.

158
14:25.810 --> 14:29.850
So that's why I've got this in here and it will come back and try it again.

159
14:29.860 --> 14:35.200
So it's always good to have that. If you leave it out, you will find you'll probably get a bit of error in

160
14:35.290 --> 14:38.970
your NavMesh calculations when you're trying to push this through.

161
14:38.990 --> 14:46.210
Anyway, if we've reached the destination then we're returning a 'Succeed' to the Behavior Tree which says

162
14:46.210 --> 14:51.970
that we've finished and that would make both of these things finished, which would make that whole tree

163
14:52.480 --> 14:53.550
finished.

164
14:53.550 --> 14:55.870
Right, so now you've got that, save it

165
14:56.260 --> 14:59.530
and we'll go back into Unity.

166
14:59.680 --> 15:06.130
We'll go over to the Droid. What you're going to do is add on the Behavior Tree.

167
15:06.130 --> 15:11.770
So, first of all, go 'Add Component > Panda Behaviour'.

168
15:12.310 --> 15:15.360
Add that and then it has a 'Count'. This is how many

169
15:15.510 --> 15:21.580
Behavior Tree scripts you've added on to your bot and you can put more than 1, we're just using 1

170
15:21.580 --> 15:22.670
at this point.

171
15:22.810 --> 15:30.370
You'll have a 'BT Script 0' written here. That's where we drag-and-drop our Wander.BT,

172
15:30.370 --> 15:37.750
so drag that over to there. You will immediately be able to see the little tree that you've written-up

173
15:37.870 --> 15:38.740
in that file

174
15:38.740 --> 15:43.090
and as we run it, you'll see these things run and change color.

175
15:43.090 --> 15:47.660
So this is where the debug happens, over in the Inspector.

176
15:47.770 --> 15:54.820
So press Play to run this and you'll see that the agent starts to move immediately.

177
15:54.850 --> 16:00.700
You can see over in the debug that the PickRandomDestination has run and been successful because

178
16:00.700 --> 16:01.810
it's green

179
16:01.810 --> 16:05.100
and then after that you see MoveToDestination.

180
16:05.200 --> 16:07.690
See how it's got a little timer next to it?

181
16:07.690 --> 16:14.640
That's our debug code showing up, so you know that it's running through doing that. At the moment,

182
16:14.640 --> 16:20.500
we also have this tree ticked to 'Repeat Root',

183
16:20.500 --> 16:23.790
so it's going to continue wandering and wandering and wandering

184
16:23.800 --> 16:30.160
and that's what this agent is doing. It's picking a random destination and then flicking over to move

185
16:30.550 --> 16:32.550
to the destination.

186
16:32.560 --> 16:39.700
Now the other thing that's also happening with this Behavior Tree is its Update.

187
16:39.710 --> 16:46.760
So what happens with all of those nodes is they get processed every update, 1 node at a time (and you

188
16:46.760 --> 16:54.270
can change this to different types of Updates up here). The way that a Behavior Tree gets updated is called

189
16:54.290 --> 17:01.330
its 'Tick'. So some Behavior Trees you'll be able to set that by hand and say that you want like a

190
17:01.330 --> 17:02.140
1 second tick.

191
17:02.140 --> 17:08.330
In this case, it's linked to the Unity MonoBehaviour so that it will tick over and Update with,

192
17:08.350 --> 17:14.260
in this case, Update which might be all that you want. We'll leave this exercise here and we'll have a

193
17:14.260 --> 17:17.170
closer look at Sequences in the next lecture.