WEBVTT

0
00:11.840 --> 00:17.520
Thus far, we've created a whole bunch of little Behavior Trees and now it's time to put them all together.

1
00:17.780 --> 00:22.150
If we were to draw them in a diagram, they'd look something like this

2
00:22.220 --> 00:28.070
and what you're seeing on the screen is a close representation of what we're heading towards but it's

3
00:28.070 --> 00:33.530
not complete (by any means) because there's not enough room on the screen to do it

4
00:33.530 --> 00:41.870
but you can see the key areas. You can see all of the key trees that we've created thus far and all of

5
00:41.870 --> 00:45.370
the ways that they are interconnected.

6
00:45.380 --> 00:49.240
The question is: How do you put them all together?

7
00:49.250 --> 00:57.670
because they're not a 'sequence' and they're not something that should be selected from. What I've done here is just

8
00:57.670 --> 01:05.050
one way that you could logically structure them and you'll see that there's a new node type added right

9
01:05.050 --> 01:12.790
at the top called 'Parallel'. Parallel allows you to run 2 or more sides of the tree at once

10
01:12.910 --> 01:20.530
(in this case, it's just 2) and one side is called the Patrol and the other side is called

11
01:20.650 --> 01:21.530
Dead.

12
01:21.700 --> 01:28.150
The Dead side is taking the code that we've already written for the Dead tree.

13
01:28.870 --> 01:35.950
The Patrol combines all of the behaviors that the bot can do when it's alive.

14
01:36.010 --> 01:45.580
We want the nodes under Patrol to run in a quasi kind of sequential order, so that each Update, the bot is checking

15
01:45.580 --> 01:50.260
whether it should Flee, Attack, Look Around or Wander.

16
01:51.210 --> 01:54.360
We'll start by going straight to the bots

17
01:54.390 --> 01:59.490
AI Behavior Tree, which is combining everything we've done thus far.

18
01:59.700 --> 02:08.640
So create a new script called 'BotAI.BT' and into that we will combine all of the trees.

19
02:08.760 --> 02:16.740
You don't have to put all of the trees into one file, you can put them in separate files and attach

20
02:16.740 --> 02:22.140
them all to the Panda Behavior that's attached to your agent.

21
02:22.150 --> 02:28.050
However, I find it a lot easier to see what's going on when we're debugging if they're all in one single

22
02:28.050 --> 02:29.250
file.

23
02:29.250 --> 02:31.810
So here's what they're going to look like.

24
02:31.890 --> 02:36.990
First of all, we start off with our tree 'Root' node.

25
02:37.020 --> 02:40.260
This is where the tree will start running from.

26
02:40.260 --> 02:46.080
Then we have our 'parallel' node which will run its children at the same time

27
02:46.380 --> 02:52.740
and you can see there's 1 tree called Patrol that will run and 1 tree called Dead.

28
02:52.740 --> 02:54.940
We've already written the Dead one.

29
02:54.960 --> 03:03.260
These are prefixed by a 'repeat' node, a 'mute' node and then, of course, the 'tree' node. The 'repeat' node will

30
03:03.270 --> 03:06.490
just continue repeating (over and over again)

31
03:06.540 --> 03:16.650
this part of the entire tree. 'mute' doesn't care whether the node underneath it succeeds or fails, it just

32
03:16.650 --> 03:18.050
keeps running it.

33
03:18.180 --> 03:24.220
So it's saying "Don't listen to whether you succeed or fail" which allows this Dead tree

34
03:24.220 --> 03:24.750
here,

35
03:24.780 --> 03:31.120
when your agent's not dead, to keep running and keep checking if the agent's dead.

36
03:31.170 --> 03:33.270
Now what's in each of these trees?

37
03:33.360 --> 03:40.380
You'll see that's a separate part in the code for each tree, so there's Patrol...and here's Attack and each one is

38
03:40.380 --> 03:41.130
named.

39
03:41.130 --> 03:43.980
They don't have the Root any more at the top.

40
03:43.980 --> 03:49.090
They need a name so that they can find how they're all linked together.

41
03:49.440 --> 03:56.550
Now you could put this in one great big tree without all these little subtrees but it would become

42
03:56.550 --> 04:04.790
quite a mess and very difficult to debug. The Patrol tree has a sequence and it's going to call the

43
04:04.800 --> 04:07.490
'Flee' tree, so it's going to check if we can flee.

44
04:07.500 --> 04:15.540
That's also then going to go on and say 'while our health is not less than 40' we will do our little sequence

45
04:15.630 --> 04:17.790
of Attack,

46
04:18.030 --> 04:26.610
LookAround and Wander. This is the code for the Attack tree, taken straight out of our little file that

47
04:26.610 --> 04:28.850
we made before, for Attack.

48
04:29.010 --> 04:35.870
The only difference is I've changed Attack here, which had Root back in the Attack file, if I find it...

49
04:35.870 --> 04:44.260
it's just there, you can see and it's the same for all of the others. So Dead is down here.

50
04:44.370 --> 04:50.410
We've got Flee, LookAround and then Wander.

51
04:50.410 --> 05:00.360
So once you've got that, you can save it and then you want to go to your Droid and set that as your

52
05:00.360 --> 05:01.060
Behavior Tree.

53
05:01.120 --> 05:07.900
We don't need to change anything in the AI class code at the moment because we've not created any new

54
05:07.900 --> 05:09.160
methods.

55
05:09.190 --> 05:15.280
Your tree, if it's got pink and red all the way through it, means that it's all ready to go, it's found all the

56
05:15.280 --> 05:16.980
methods it needs.

57
05:17.230 --> 05:21.550
Ok, so let's press Play and see what happens.

58
05:21.590 --> 05:21.900
Right.

59
05:21.950 --> 05:30.880
If we go over to the Inspector and have a look, both of the 'parallel' things are running and the bot itself

60
05:30.910 --> 05:42.610
is just sitting there...under Patrol. It has fired off the 'Flee' and the Flee it's fired off is down here and

61
05:42.610 --> 05:51.040
it's saying 'while IsHealthLessThan(40.0)', so it can't satisfy this node and

62
05:51.040 --> 05:58.040
therefore nothing else is going to run. Up here, where that Flee is being called,

63
05:58.120 --> 06:05.190
it's failing that tree, therefore nothing else is happening because a sequence isn't going to continue

64
06:05.200 --> 06:14.620
when a node in it fails, so we immediately have a problem with our tree not being able

65
06:14.620 --> 06:20.890
to continue and go through all of the processes that the bot can do when it's still alive.

66
06:20.890 --> 06:27.070
Now the same thing has happened for the dead tree but that's ok because our bot isn't dead, so it's

67
06:27.070 --> 06:33.680
stuck on this 'IsHealthLessThan(0.1)' which it's not, so it's not going to immediately explode.

68
06:33.810 --> 06:34.360
It's all right,

69
06:34.360 --> 06:35.750
it's running parallel up here.

70
06:35.770 --> 06:40.340
The problem is down here with the Flee, that we want to keep going.

71
06:40.340 --> 06:46.320
So if this tree (for Flee) fails, we still want it to go and check the others.

72
06:46.330 --> 06:58.640
So stop playing and I'll introduce you to a new node type. Go back into your bot tree code. In Patrol,

73
06:58.640 --> 07:01.920
we'll make 2 changes. Where there's 'sequence'

74
07:01.920 --> 07:02.560
here,

75
07:02.610 --> 07:08.370
we're going to call that 'fallback' and this other 'sequence' down here,

76
07:08.400 --> 07:12.300
we will call that 'fallback'.

77
07:12.350 --> 07:22.200
Now the fallback node is a sequence node but it allows the nodes underneath it to fail.

78
07:22.230 --> 07:29.190
So even though they do fail, it will still go ahead and keep running the things underneath it

79
07:29.280 --> 07:36.480
and so this is what we want to use in this case because Flee will fail when we don't have to flee and

80
07:36.480 --> 07:38.070
things like Wander and LookAround,

81
07:38.070 --> 07:43.410
they're going to fail when we're in the range of the player and we're actually attacking them and Attack's going

82
07:43.400 --> 07:45.150
to fail when the player's not around,

83
07:45.150 --> 07:47.700
so we need to allow for that.

84
07:47.700 --> 07:54.330
So save that now and let's go back here and press Play again.

85
07:54.570 --> 08:02.820
Now when we run it, you'll find that the bot will move and it's going to respond to the code that allows

86
08:02.820 --> 08:10.200
it to turn around because we're setting in our fallback: Attack (which fails because it can't attack at

87
08:10.200 --> 08:15.680
the moment) then it has a LookAround tree (which you can do

88
08:15.900 --> 08:17.610
and so it just looks around).

89
08:17.610 --> 08:24.500
You'll see that the Wander isn't getting triggered whatsoever, it's not getting down that far

90
08:24.540 --> 08:32.100
and if we look at the LookAround (let's go down further) you'll see that within the LookAround, there's nothing

91
08:32.130 --> 08:34.960
in here that's going to cause it to fail

92
08:35.010 --> 08:44.200
while it is healthy and away from the player. So we can force it to fail

93
08:44.210 --> 08:53.210
every now and then, so it will allow a callback, up to the Patrol tree which will call the Wander, so

94
08:53.210 --> 08:57.470
that LookAround can finish. It doesn't really have a finished state.

95
08:57.470 --> 08:58.900
I guess that's what I'm saying

96
08:59.000 --> 09:02.540
and we want it to go back up here, so that Wander can run.

97
09:02.540 --> 09:03.860
So stop playing,

98
09:03.860 --> 09:06.000
open up your code again

99
09:06.200 --> 09:12.110
and in the LookAround, we're going to add another node in this sequence.

100
09:12.140 --> 09:20.720
So after we've waited a random amount of time, we will put another 'random' in here and then as children

101
09:20.720 --> 09:32.390
to that, we'll put 'Fail' and 'Succeed'. So randomly, it will either fail or succeed which will throw it back

102
09:32.510 --> 09:41.420
out of this tree to the Patrol tree, where it will be allowed to go on to the Wander. Save that and then press

103
09:41.440 --> 09:45.150
Play.

104
09:45.150 --> 09:45.540
All right.

105
09:45.540 --> 09:54.910
So the Droid now has done a LookAround but it's also triggered a Wander as well, as you saw happen there

106
09:55.110 --> 10:00.240
and then it goes back into LookAround...

107
10:00.480 --> 10:05.990
and when that fails, it will Wander again and here you can see it going off on a wander.

108
10:06.000 --> 10:07.340
Now if we're lucky...

109
10:07.350 --> 10:08.040
yep,

110
10:08.100 --> 10:13.250
as it goes past here, what's happened is the Attack has been triggered

111
10:14.040 --> 10:20.880
and that's because it has a SeePlayer and a TargetPlayer and a LookAtTarget, which is what made it

112
10:20.880 --> 10:23.190
turn towards the player

113
10:23.190 --> 10:27.220
and you don't want that to happen unless it's actually in Attack mode.

114
10:27.420 --> 10:33.600
Now it's come into here (again it's back in Attack mode) and it's starting to Attack our lpayer because it's

115
10:33.600 --> 10:39.960
found it, which is interesting in itself but we don't really want that happening so we have to add a

116
10:39.960 --> 10:46.820
few more conditional statements into our tree to make it behave exactly as we want it to.

117
10:47.610 --> 10:54.360
When the bot sees the player and goes to attack it (or knows that it's able to attack it), it should be able

118
10:54.360 --> 11:03.900
to move towards the player as well, so that it gets into range and we can modify the Attack script to

119
11:03.900 --> 11:04.320
do that.

120
11:04.320 --> 11:07.360
So back in our full Behavior Tree,

121
11:07.380 --> 11:14.510
we're going to replace the Attack sequence that we had before with something a little more complicated.

122
11:14.500 --> 11:18.540
So let me put it in, right so I'll go through and explain it.

123
11:18.540 --> 11:22.140
First of all, we're going to check if we can see the player in Attack,

124
11:22.140 --> 11:23.890
before we continue

125
11:23.910 --> 11:32.720
and then if we can, we'll start a sequence that does TargetPlayer, LookAtPlayer. Then we're going to have

126
11:32.720 --> 11:36.290
a fallback on 2 'whiles'.

127
11:36.410 --> 11:39.260
The first one will be

128
11:39.380 --> 11:41.040
'while ShotLinedUp'

129
11:41.280 --> 11:52.510
and then 'while not ShotLinedUp'. If a shot IS lined-up, then the sequence will be 'Wait and Fire', which

130
11:52.510 --> 11:54.500
we had before, for shooting.

131
11:54.700 --> 12:03.670
If the shot IS NOT lined-up, then we want to 'SetTargetDestination' and 'MoveToDestination'.

132
12:03.670 --> 12:11.630
So setting target destination will be setting the player as that particular target.

133
12:11.950 --> 12:17.950
The 2 new methods here, that we have to write and add to the AI script is ShotLinedUp and

134
12:17.950 --> 12:19.250
SetTargetDestination.

135
12:19.270 --> 12:26.550
So let's go back to those scripts and at the bottom under the other tasks, we will add these.

136
12:26.560 --> 12:30.090
So the first one is SetTargetDestination.

137
12:30.250 --> 12:40.750
We've already set the target itself because we used (in our script) TargetPlayer, which sets that target

138
12:40.870 --> 12:44.280
value to equal the position of the player.

139
12:44.290 --> 12:47.870
That's why we're only setting a target destination down here

140
12:48.010 --> 12:56.350
and what that's doing is telling our NavMesh to now move towards the target.

141
12:56.510 --> 13:01.910
The next one is the ShotLinedUp, which is returning a 'true' or 'false'

142
13:02.000 --> 13:04.670
if we're able to shoot at the player.

143
13:04.850 --> 13:12.740
So again, this is a visibility-type problem where we're testing the distance being within the shot range

144
13:12.740 --> 13:21.650
value (which is defined up the top) and that the angle between the forward vector of your agent and the

145
13:21.890 --> 13:27.940
vector to the target is less than 1 (so quite a small narrow angle).

146
13:27.980 --> 13:30.560
If it IS, then we can shoot.

147
13:30.560 --> 13:36.730
If it's NOT, then we will move and adjust our position to get closer to the player.

148
13:36.800 --> 13:38.320
So, save that

149
13:38.600 --> 13:44.210
and let's go back into Unity and press Play to see how this now plays out.

150
13:45.620 --> 13:51.890
The bot is going to look around and also wander. Now when it goes past the player

151
13:51.950 --> 13:59.200
this time, it's noticed the player and turned towards him and is coming in for an attack.

152
13:59.200 --> 14:04.850
So rather than completely ignoring and going off and saying "I didn't see you", it DID see us, so now we

153
14:04.850 --> 14:09.340
can come back and let's start shooting at it, at this point,

154
14:09.350 --> 14:11.160
so it's health will go down.

155
14:11.330 --> 14:13.560
Off it goes, it's going to wander away...

156
14:14.370 --> 14:16.130
find a hiding place...

157
14:16.170 --> 14:20.040
Now if we just leave it alone (we can't see it there, can we?

158
14:20.070 --> 14:22.140
Let's just move around.

159
14:22.430 --> 14:23.400
Ok).

160
14:23.760 --> 14:30.420
When it's health gets above 40, it'll go back doing whatever it was doing before, which is looking around

161
14:30.540 --> 14:37.800
and wandering and now it's not going to attack unless it finds us again or we go and approach it and

162
14:37.800 --> 14:39.090
try shooting at it.

163
14:39.270 --> 14:42.240
All the while, it's health will keep going up.

164
14:42.420 --> 14:48.980
Ok, so that is Behaviour Trees in a pretty tight little nutshell.

165
14:49.080 --> 14:54.560
There's still a lot for you to learn. What I would suggest is now you know your way around them,

166
14:54.600 --> 15:03.120
I would go to the pandabehaviour.com website, where the PandaBT plugin comes from and read through

167
15:03.120 --> 15:09.420
the documentation because it's very helpful to see what nodes are available, what they do and how you should

168
15:09.420 --> 15:10.360
structure them.

169
15:10.440 --> 15:18.480
Also, download the full Panda Behaviour plugin from the Asset Store because it has some great examples

170
15:18.690 --> 15:24.090
that come with it and some of them are really quite complex and they'll show you how to do some amazing

171
15:24.090 --> 15:25.920
things with Behavior Trees.