WEBVTT

0
00:10.660 --> 00:17.620
In this second lecture on Sequential nodes, we'll have a closer look at how they work and some

1
00:17.620 --> 00:21.330
of the problems you can get when you're trying to create your own and run them.

2
00:21.340 --> 00:27.610
So, first of all, let's create a new Behavior Tree which is based on the 'Wander'. So create a

3
00:27.610 --> 00:28.320
new one.

4
00:28.330 --> 00:36.160
What I usually do is just duplicate the one I've got and I'm going to call that Patrol.BT.

5
00:36.640 --> 00:44.410
We're going to get the bot to patrol around the map, so we'll set it destinations that are in the particular

6
00:44.410 --> 00:45.660
corners.

7
00:45.730 --> 00:49.170
Now it's remembered what was in Wander because it was a duplicate of it.

8
00:49.380 --> 00:55.750
What we want to do is to be able to tell it where to go, rather than picking a random destination.

9
00:55.750 --> 01:01.600
So for that, we're going to need a new method back in our AI class and let's call it PickDestination

10
01:01.960 --> 01:07.300
and we'll tell it that we want it to go to our extreme points that we've set up.

11
01:07.300 --> 01:11.040
So we'll start at -100

12
01:11.260 --> 01:18.250
and this is an example of how you can pass through arguments from the Behavior Tree script into your

13
01:18.250 --> 01:19.190
methods.

14
01:19.210 --> 01:24.070
So when you declare them, they can be 'int's that you're sending through,

15
01:24.100 --> 01:26.500
they can be 'bool's, they can be 'float's.

16
01:26.500 --> 01:32.890
Let's make these floats actually, I'll put '.0' on the end because they're locations in space

17
01:33.220 --> 01:39.850
and then after that, we can reuse MoveToDestination because MoveToDestination is just monitoring our

18
01:39.930 --> 01:42.240
bot moving towards a destination.

19
01:42.310 --> 01:47.770
If you didn't have this and you just had PickDestination, PickDestination, PickDestination, PickDestination

20
01:47.770 --> 01:56.740
it would fail to move anywhere because before it got a chance to move, it would get a new

21
01:56.740 --> 02:04.840
destination set in the Nav Mesh Agent. It's MoveToDestination here that's monitoring what Nav Mesh Agent is doing in the

22
02:04.840 --> 02:10.740
background that's giving you time to get from one location to the other.

23
02:10.900 --> 02:18.310
Now there will be 4 of these (I'll just copy that and put them in here) because we've got 4 different

24
02:18.310 --> 02:19.800
corners we want to go to.

25
02:19.960 --> 02:24.440
So we'll start at -100, -100

26
02:24.520 --> 02:36.020
and then we'll go to -100, 100 and then we'll go to 100, 100 and then we'll go to 100, -100 like that.

27
02:36.020 --> 02:37.330
So save that.

28
02:37.610 --> 02:39.960
Then you want to go back to Unity.

29
02:40.160 --> 02:45.380
You want to select the Droid and you want to change its Behavior Tree to patrol, so drag-and-drop

30
02:45.380 --> 02:52.850
Patrol.BT into your Behavior Tree and you'll see here the new one that's come up.

31
02:52.850 --> 03:00.090
Now if you wait a little while, you will get an error message that will come up and these will go red

32
03:00.200 --> 03:07.310
if they don't exist as a method in your AI class. I've already written mine, so it's found it and it's

33
03:07.310 --> 03:07.590
fine.

34
03:07.610 --> 03:10.750
Even if you've got a spelling mistake in the names of your methods,

35
03:10.850 --> 03:11.720
it will show it

36
03:11.750 --> 03:15.720
and so just make sure you've checked that it's the same in either place.

37
03:15.740 --> 03:17.240
The same thing goes for your parameters.

38
03:17.240 --> 03:18.440
We're sending through floats.

39
03:18.440 --> 03:23.560
It's got to be  waiting for a float inside the AI class.

40
03:23.620 --> 03:28.460
So in your AI class (because you're probably seeing errors at the moment, let's open it up and have

41
03:28.460 --> 03:28.910
a look,

42
03:28.910 --> 03:32.870
here it is, here) above PickRandomDestination.

43
03:32.870 --> 03:39.410
I've created a new method called PickDestination with an X and a Z value for our map

44
03:39.950 --> 03:47.330
and then we're using 'dest' as a new vector which is at X and Z, rather than it being a random

45
03:47.330 --> 03:48.300
value.

46
03:48.320 --> 03:49.840
Y is set to 0

47
03:49.970 --> 03:53.120
because our ground plane is sitting at 0.

48
03:53.150 --> 03:56.810
If your ground plan isn't at 0, you might want to adjust this

49
03:56.960 --> 04:03.180
and then we run SetDestination and return with a Succeed

50
04:03.410 --> 04:08.780
and that's all we need to do, to now patrol because the code for MoveToDestination is already doing

51
04:08.780 --> 04:10.700
what we need it to do.

52
04:11.160 --> 04:19.750
So add that in, save it and then let's go back to Unity and press Play and we'll see the Droid in action

53
04:19.760 --> 04:28.400
and if you click on the Droid in the Hierarchy, you'll be able to watch each of these leaf nodes run

54
04:29.450 --> 04:34.230
as the Droid makes its way around the path. So MoveToDestination is currently running.

55
04:34.340 --> 04:39.660
It's going to flick to PickDestination (or just did then) down to the next MoveToDestination

56
04:39.890 --> 04:47.190
when it reaches each waypoint that you're setting up through the code for it to go to and because this

57
04:47.190 --> 04:53.850
'Repeat Root' is ticked at the top, it will just go round and round and round and round this sequence, which

58
04:53.850 --> 04:55.260
is what you want it to do.

59
04:56.150 --> 05:03.170
So with all that working nicely, let's force an error to occur so that you can see what happens.

60
05:03.200 --> 05:10.020
So let's go into the AI script and we're looking for PickDestination.

61
05:10.220 --> 05:15.600
So PickDestination has at the end of it a 'Task.current.Succeed();'.

62
05:15.650 --> 05:23.510
This is NOT a return statement, so it doesn't throw you out of this method and you can call it from anywhere

63
05:23.670 --> 05:28.460
inside the code. It's just going to send a Succeed back to the Behavior Tree.

64
05:28.520 --> 05:30.620
So you can actually leave it out and you won't get an error,

65
05:30.640 --> 05:36.970
which is what we're going to do. We're just going to comment it out and save it and then we'll go back into

66
05:36.980 --> 05:39.080
Unity and press Play.

67
05:40.730 --> 05:45.100
Now let's go and have a look at what we're getting here.

68
05:45.140 --> 05:49.810
You can see that PickDestination has run.

69
05:50.060 --> 05:51.430
See that it's blue?

70
05:51.440 --> 05:53.370
It hasn't finished running.

71
05:53.420 --> 05:55.550
It's green when it finishes running.

72
05:55.550 --> 06:01.070
Now you're probably wondering "Well the bot's still moving, it's still moving to the destination." and it will

73
06:01.070 --> 06:06.920
move to the destination because the Nav Mesh Agent code is what's driving it forward.

74
06:06.920 --> 06:11.630
After we go 'agent.SetDestination', the agent's going to go for it.

75
06:11.840 --> 06:16.640
It's the MoveToDestination that's sitting there monitoring, waiting for it to get to where it's got to

76
06:16.640 --> 06:19.530
go, so that it can then pick the NEXT destination

77
06:19.640 --> 06:25.420
but you can see now that it has finished that 1 path find over to here,

78
06:25.820 --> 06:29.410
it's stopped. The whole Behavior Tree has just stopped completely.

79
06:29.510 --> 06:35.840
So that Succeed is absolutely necessary and if you find that your Behavior Trees are just stopping, then

80
06:35.990 --> 06:38.480
you've forgotten to put a Succeed or a Fail.

81
06:38.480 --> 06:41.610
Now you can put a Fail in here as well

82
06:41.720 --> 06:47.610
and because it's on repeat, it will just loop around, loop around, loop around and constantly keep failing,

83
06:47.610 --> 06:54.080
so it's going to move through the sequences until it hits a Fail and then shoot you out

84
06:54.320 --> 06:56.770
and we can actually force that to happen and have a look.

85
06:56.770 --> 06:59.570
So let's go back into the code

86
07:00.920 --> 07:03.470
and I'm going to put this Succeed back in

87
07:03.470 --> 07:06.490
so that we're going to move down

88
07:06.650 --> 07:13.560
but what I want to do is have this one here, the PickRandomDestination.

89
07:13.580 --> 07:24.450
Let's set that to Fail. So for Panda, it's like that, so that will return a 'fail' when we try and do a

90
07:24.450 --> 07:25.370
PickRandomDestination.

91
07:25.560 --> 07:28.230
Let's go into our patrol

92
07:28.240 --> 07:39.340
and as the 2nd destination setting, we'll get rid of the parameters and put this as PickRandomDestination

93
07:39.970 --> 07:45.410
as we did before, when we were wandering. So save that and let's go back over

94
07:45.460 --> 07:47.550
here, press Play

95
07:47.600 --> 07:55.510
and let's watch the tree. So PickDestination has now succeeded (it's green) and we're moving to our destination,

96
07:55.510 --> 08:01.350
so this will keep ticking over until the bot gets up into the corner, at which point it's going to try

97
08:01.350 --> 08:05.430
and run this PickRandomDestination which we know is going to fail.

98
08:05.650 --> 08:07.400
So it's getting to the corner,

99
08:07.520 --> 08:16.920
it stopped and there, you can see that it's wandering around trying to turn itself because that's

100
08:16.920 --> 08:25.620
the 'agent.SetDestination' constantly getting called, over in this PickDestination and then trying to move

101
08:25.620 --> 08:29.650
to it and it's picking the same destination that we already went to.

102
08:29.700 --> 08:33.390
So when we try and do a MoveDestination, we're already there.

103
08:33.390 --> 08:36.950
Then we go and do a PickDestination which fails immediately

104
08:37.140 --> 08:42.750
and because we're looping around this route, it's stuck.

105
08:42.780 --> 08:49.580
You can see it's stuck and you can see the Success and Fail values flicking really quickly up there

106
08:49.770 --> 08:55.680
and because this is red, you know that's where you've got a failure point in your tree and you might

107
08:55.680 --> 09:02.340
want a failure point in your tree, if that's what the sequence is going to do. If a certain condition

108
09:02.400 --> 09:09.030
ISN'T met in the sequence, then you don't want to continue with the sequence.

109
09:09.030 --> 09:14.400
So let's just take that out of there and go back and repair our code.

110
09:14.460 --> 09:22.710
So in here, I'll just Ctrl-Z to get back what I had before and back in my AI script, I'm

111
09:22.710 --> 09:29.810
going to set my random destination back to Succeed...like that.

112
09:30.120 --> 09:32.840
Let's now consider an Attack sequence.

113
09:32.850 --> 09:36.700
So in this sequence, I'm going to have 3 different behaviors.

114
09:36.870 --> 09:46.400
We want the bot itself to be able to turn towards a particular destination, not MOVE to that destination.

115
09:46.410 --> 09:52.200
So that's going to involve setting the point that we want to look at and then actually doing the turning

116
09:52.890 --> 10:01.630
and then once that has happened, we can then do a firing action/ Ok, so the Behavior Tree for that, call

117
10:01.790 --> 10:07.880
it 'Attack.BT' and in there, you want something that looks like this.

118
10:07.890 --> 10:08.860
So we've got our 'Root',

119
10:08.880 --> 10:18.420
we've got our 'sequence', we've got 'TargetPlayer', 'LookAtTarget' and then 'Fire'.

120
10:18.500 --> 10:27.140
Now this means that each one of these needs to be a method in our AI class. For each of the leaf nodes in

121
10:27.140 --> 10:31.940
the new Attack tree, there needs to be a corresponding method.

122
10:31.940 --> 10:33.780
First of all, I have 'TargetPlayer'.

123
10:33.800 --> 10:41.380
All it's doing is setting a Vector3 which is declared at the top of our program to the player's position.

124
10:41.630 --> 10:48.200
That's all it's doing, so it's just setting the value of 'target'. Now doing it this way means that we could

125
10:48.200 --> 10:55.820
have other leaf nodes that will say 'look at tree' or 'look at random' and be able to set this

126
10:55.820 --> 10:59.160
'target' value without moving towards it.

127
10:59.240 --> 11:06.110
After that, I've got a 'LookAtTarget', so this is a generic one which will turn the bot around

128
11:06.110 --> 11:10.420
so it's facing towards wherever that target happens to be set.

129
11:10.640 --> 11:15.230
So just scroll up a little bit and make this a bit bigger so you can see the whole thing.

130
11:15.380 --> 11:20.770
This is pretty much the same code that we've used before

131
11:21.200 --> 11:22.710
(I'll put this down on the next line for you,

132
11:22.750 --> 11:27.200
there we go) for targeting something and moving towards it

133
11:27.200 --> 11:34.670
but in this case, we're targeting it and turning towards it. So you'll see in here the familiar

134
11:34.680 --> 11:37.300
Quaternion.Slerp code that we've used before

135
11:37.400 --> 11:45.310
and our calculation of the direction vector. So...direction...slerp towards it.

136
11:45.330 --> 11:51.590
Now I've got a Debug.Log in here, which I'm just going to get rid of because we don't need it

137
11:51.780 --> 11:59.850
and instead there's a 'Task.isInspected'. This line here is going to allow us to see the current

138
12:00.000 --> 12:09.420
angle between the bot and where our player is (in this case, the target) in the debug line inside of

139
12:09.420 --> 12:14.640
the Behavior Tree, which is fantastic because we can actually see this value changing as the bot

140
12:15.000 --> 12:26.370
turns around and then we're saying at the end, if the angle is within 5 degrees, then this task has

141
12:26.370 --> 12:27.480
succeeded.

142
12:27.540 --> 12:33.150
So we're going to keep turning, keep turning, keep turning and then we'll get a 'succeed' on this particular

143
12:33.150 --> 12:39.270
task because there hasn't been a fail and until there's a fail received by the Behavior Tree, it's still going

144
12:39.270 --> 12:47.910
to be executing this. Now after we've looked at the 'target', then we're going to fire and the fire code

145
12:48.000 --> 12:50.780
is pretty familiar. In this case,

146
12:50.790 --> 12:56.330
it's not going to be the same kind of set up that we've got here

147
12:56.490 --> 13:00.600
where you've got a public void and then a return of Succeed.

148
13:00.740 --> 13:08.880
What you can also do with these methods for these leaf nodes is have them return a 'true' or a 'false' value

149
13:08.910 --> 13:16.170
so that you can then check whether they happened or not and perform a little boolean logic in the

150
13:16.200 --> 13:17.410
Behavior Tree itself,

151
13:17.550 --> 13:20.420
even though we're not really doing that with this, in this case

152
13:20.430 --> 13:22.620
but you will see it happening in the future

153
13:22.770 --> 13:29.130
and if you're not, you could set this up as the same set-up as the other ones with the Succeed being returned

154
13:29.820 --> 13:31.120
instead.

155
13:31.320 --> 13:41.270
Now in here, we're just getting the bullet prefab and then instantiating a copy of it on the end of the bulletSpawn. Again

156
13:41.280 --> 13:48.690
the same thing we did with the little tanks demo, much earlier in the course. Then we're adding a force

157
13:49.020 --> 13:53.060
to the bullet, just to push it forward in the world environment.

158
13:53.070 --> 14:00.200
Right, so with those things done, let's save it. Save your AI script and your Attack script and go back into

159
14:00.210 --> 14:01.320
Unity.

160
14:01.370 --> 14:09.180
So I'll just select the Droid, so we can see its Behavior Tree, put 'Attack.BT' over here as the script and then

161
14:09.180 --> 14:19.800
when you're ready, press Play and the Droid turns towards our droid, ok. At the moment it's not concerned

162
14:20.160 --> 14:22.830
with walls or anything, it's just turning

163
14:22.840 --> 14:28.620
(that's what we've told it to do) to where it knows our player is and if I move the player, it's just going

164
14:28.620 --> 14:30.500
to keep turning

165
14:35.710 --> 14:37.270
and shooting like that...

166
14:44.820 --> 14:45.130
Now

167
14:45.150 --> 14:49.690
there's no accounting for the speed of the shooting in our current sequence.

168
14:49.690 --> 14:55.960
It's just going to be looping quickly through the entire tree, constantly targeting the player, looking

169
14:55.960 --> 14:58.020
at the player and then firing.

170
14:58.180 --> 15:05.830
If you wanted to add in a 'wait' in here, you can do that quite easily in this Behavior Tree system.

171
15:05.830 --> 15:14.950
If we go back into our Attack script here, is that there's 'waits' built into it, so we can put

172
15:15.880 --> 15:23.970
'Wait(0.5)' and it'll wait half-a-second before firing and then it'll loop back around again and wait

173
15:23.980 --> 15:31.080
half-a-second before firing. So just save that and go back to Unity and press Play.

174
15:31.100 --> 15:36.710
Now you can see that there will be half-a-second between each shot

175
15:38.050 --> 15:43.550
and so you can add nice delays into the Behavior Tree itself.

176
15:50.840 --> 15:51.130
All right.

177
15:51.140 --> 15:54.980
So we'll leave this lecture here. In the next lecture,

178
15:54.980 --> 16:01.930
we'll start to add in some conditions and the bot should really test whether it can see the player before

179
16:01.930 --> 16:03.390
it starts shooting.

180
16:03.440 --> 16:04.880
So we'll do that next.