WEBVTT

0
00:11.320 --> 00:15.070
So far we've looked at some pretty simple Sequential Behavior Trees.

1
00:15.070 --> 00:23.290
Now we're going to add in a condition that tests if something is 'true' or 'false' before the Behavior Tree

2
00:23.290 --> 00:24.960
continues to run

3
00:25.030 --> 00:30.940
and so we're going to add in a 'Decision Node' within the code.

4
00:30.940 --> 00:34.260
We're going to do that inside of our Attack

5
00:34.270 --> 00:39.520
but first of all, we need to write our method that's going to do the decision for us.

6
00:39.520 --> 00:46.030
Currently we're attacking all the time. You only really want the bot to attack if it can truly

7
00:46.030 --> 00:47.110
see the player

8
00:47.110 --> 00:52.080
and in this case, when you start off, it can't because the player is behind a wall.

9
00:52.150 --> 00:57.440
Also if the player is outside some visible range, you don't want it to attack either.

10
00:57.580 --> 01:05.920
Now if you remember at the very top of this program, there is a visibleRange and a shotRange set up, so

11
01:06.220 --> 01:15.550
this visibleRange (which is 80) is going to determine if the player is within 80 units of the bot, then

12
01:15.550 --> 01:21.670
it can see it. It will also test at the same time if there's no wall between the player and the bot, which

13
01:21.670 --> 01:28.090
is a nice enemy situation because you want the bot to be aware that there is infrastructure around

14
01:28.090 --> 01:28.180
it.

15
01:28.180 --> 01:36.700
So as the last method in here underneath the Fire, I'll add in a new one which we'll use in our Behavior Tree

16
01:36.730 --> 01:38.170
code.

17
01:38.170 --> 01:40.350
So it's called SeePlayer.

18
01:40.630 --> 01:41.710
It's a boolean

19
01:41.710 --> 01:48.220
so it's like we had before but this time we're actually going to use it as a logic test in the

20
01:48.250 --> 01:49.480
Behavior Tree.

21
01:49.480 --> 01:57.280
So what this is doing is working out the vector from the

22
01:57.280 --> 01:59.370
bot to the player

23
01:59.530 --> 02:03.500
and then we're also going to have a RaycastHit.

24
02:03.940 --> 02:12.520
So we'll raycast along that distance vector to see if we hit something and if we do hit something (which is

25
02:12.610 --> 02:17.800
in this case, a wall) then we obviously can't see the player because there's a wall in the road, So I've got a little

26
02:17.800 --> 02:18.480
bool set

27
02:18.480 --> 02:22.130
here, saying 'seeWall = false' to start with.

28
02:22.180 --> 02:28.900
I'm also doing a Debug.DrawRay in the environment, so we can see where that distance vector is

29
02:28.900 --> 02:30.600
being projected.

30
02:30.610 --> 02:35.840
This is really nice if you're trying to debug visually what's going on and where your vectors are.

31
02:35.980 --> 02:38.520
I always use this.

32
02:38.740 --> 02:46.180
Now it's going to project a ray, do the Physics.Raycast as we've done before and then see if we have hit

33
02:46.420 --> 02:48.770
something called a 'wall'.

34
02:48.970 --> 02:53.140
If we have, then seeWall will equal 'true'.

35
02:53.260 --> 02:59.490
Now just for a moment back in Unity, if you go to the Map and drop that down, there's all these walls

36
02:59.650 --> 03:06.160
and each one has been tagged with 'wall' and that's how that Physics.Raycast is going to work.

37
03:06.250 --> 03:14.420
Now make sure that your ray that you're projecting does actually go through the wall (or whatever object

38
03:14.440 --> 03:17.400
you're trying to detect as an obstacle)

39
03:17.500 --> 03:20.880
and that's why this Debug.DrawRay is such a great idea.

40
03:21.880 --> 03:31.870
After doing that, I'm also printing the seeWall value in some debug code for the Behavior Tree itself

41
03:31.870 --> 03:37.210
so that you can see it when you're working through the different nodes and what they're doing,

42
03:37.210 --> 03:39.070
which is also quite useful.

43
03:39.190 --> 03:43.750
Then finally, to determine whether we can see the player or not,

44
03:43.750 --> 03:51.820
the test is: If the magnitude of that direction vector distance between the two players is less than the

45
03:51.820 --> 04:03.050
visible range AND there's no wall in the way. This time, we're not concerned whether the bot is facing

46
04:03.110 --> 04:04.700
in the right direction or not.

47
04:04.700 --> 04:09.620
So let's just imagine it has a little radar system on board and it doesn't need to be looking in

48
04:09.620 --> 04:17.420
a direction to be able to tell if the player is nearby. Now if the player is in that radius and there's

49
04:17.420 --> 04:23.590
no wall in the way, then we're going to return 'true' that we do see the player,

50
04:23.930 --> 04:27.390
otherwise we will return a 'false'.

51
04:27.410 --> 04:31.650
So this is the method for our new node.

52
04:31.700 --> 04:36.020
Ok, so we'll save that and then we'll go into the Attack script

53
04:36.080 --> 04:43.070
and what we're going to do is add a new node into our tree above the 'sequence'. So above here,

54
04:44.310 --> 04:52.090
I'm now going to add this. So the code is 'while SeePlayer'

55
04:52.140 --> 05:00.750
Now this 'while' node is a special testing node and it will allow you to run something that is a true

56
05:00.750 --> 05:02.260
or a false value

57
05:02.490 --> 05:06.660
and even invert it to check (it doesn't have to be true to run,

58
05:06.720 --> 05:11.060
it can also be false) and it won't stop your tree from running.

59
05:11.170 --> 05:20.220
Ok, so you could even put, for example, 'while not SeePlayer' if you wanted to switch the logic around and

60
05:20.220 --> 05:23.380
we will a little later, but not in this case here.

61
05:23.460 --> 05:27.780
Now this 'while' node can only have 1 child.

62
05:27.950 --> 05:34.730
So we need to make sure that we've indented all of these like that, otherwise the Behavior Tree system

63
05:34.730 --> 05:36.430
will complain about it.

64
05:36.440 --> 05:44.890
So now the sequence of shooting is only going to happen if the SeePlayer method gives us a 'true'up here.

65
05:44.890 --> 05:47.810
So let's save that and let's test it out.

66
05:47.810 --> 05:55.220
So back in Unity, click on the Droid to check that the script is ok and you'll get pink text where everything's

67
05:55.220 --> 05:57.660
fine with the Attack script.

68
05:57.680 --> 06:00.680
Ok, let's press Play and have a look at what we get.

69
06:00.710 --> 06:10.940
So over in the Behavior Tree, while SeePlayer is sitting there, wall = True, you can see that there.

70
06:10.940 --> 06:17.780
You can also see in the scene that debug ray going through and hitting the wall, which is causing the

71
06:17.780 --> 06:23.750
'wall = True' here and it's making us so that we can't see the player.

72
06:23.750 --> 06:25.510
Let's move the player

73
06:26.660 --> 06:37.870
around into view, so if I come around the corner and there you go. As soon as I come into view and I'm within

74
06:37.990 --> 06:41.510
the visual range, then the Droid starts firing

75
06:41.530 --> 06:47.950
and if I turn around and go back again, it will stop as soon as there is a wall in the road.

76
06:50.790 --> 06:57.840
If I go through this wall because I'm not constrained as the player by the NavMesh, it no longer

77
06:58.230 --> 07:05.670
can see me. If I come back in, it will and because of the previous code it will also turn towards the

78
07:05.670 --> 07:08.950
player, in order to shoot

79
07:09.110 --> 07:15.980
and you can see the Behavior Tree ticking along quite nicely with that little bit of logic. So you're

80
07:15.980 --> 07:22.910
starting to see the power of Behavior Trees, that you can create a whole bunch of methods that do

81
07:22.940 --> 07:30.560
a single action for your player and then you can string them together in the Behavior Tree, which makes

82
07:30.560 --> 07:33.700
it a lot easier for someone who's not really familiar with the code

83
07:33.710 --> 07:41.040
but it also makes your A.I. quite powerful where you can mix-and-match different behaviors. Just for interests

84
07:41.040 --> 07:41.330
sake,

85
07:41.330 --> 07:50.330
let's go back into Attack and change that code up at the top with the while-statement to be a 'not' and

86
07:50.330 --> 07:56.150
reverse that logic, so that when we DON'T see the player, we're going to attack the player, which doesn't

87
07:56.150 --> 08:04.060
make logical sense but what that means is that while you're not in visible view of the bot with your player

88
08:04.070 --> 08:07.320
character, it's going to be shooting in your direction.

89
08:07.430 --> 08:09.110
So just save that. While I'm here,

90
08:09.110 --> 08:13.130
I should also mention this while-statement because of the used to normal programming, you're going to think

91
08:13.520 --> 08:20.450
while statement means 'looping'. In the case of Behavior Trees, it doesn't mean looping.

92
08:20.450 --> 08:22.190
So this is NOT a loop.

93
08:22.230 --> 08:24.700
If anything, it's an if-statement,

94
08:24.710 --> 08:28.860
if you want to think of it as something, so it's not going to cause this to loop around inside of here

95
08:29.210 --> 08:37.030
and it's just called 'while'. Right, so save that. Let's go back and have a look at how that affects our Behavior Tree.

96
08:37.040 --> 08:43.610
So, let's press Play and we would expect we're going to get shot at straight away because we can't see

97
08:43.610 --> 08:44.300
the player.

98
08:44.480 --> 08:50.030
Now if I move INTO range, then we won't get shot at

99
08:50.300 --> 08:55.300
and in fact that whole sequence stops running, so we're not even going to turn and look at the player

100
08:55.640 --> 09:02.290
because this bit here is not being satisfied, as far as being able to see the player.

101
09:02.490 --> 09:02.700
Right.

102
09:02.700 --> 09:07.800
So that's how you can add some logic in as well.

103
09:07.800 --> 09:16.440
Now you don't have to use the 'while not' in this case because the SeePlayer could be part of the sequence.

104
09:16.830 --> 09:27.360
If you put SeePlayer in the sequence (let's just go back in to here and take that out of there and put

105
09:27.360 --> 09:28.480
it down here)

106
09:30.320 --> 09:35.070
then it's quite legitimately allowed to fail...

107
09:38.340 --> 09:45.330
and because this is a sequence, the rest of it won't run anyway so let's save it and have a little look

108
09:45.330 --> 09:46.090
at it.

109
09:47.030 --> 09:48.990
Check that it's reloaded in here.

110
09:49.190 --> 09:56.460
We now have a sequence. If we press Play...ok, we can't see the player.

111
09:56.460 --> 10:03.120
So the sequence has stopped with the SeePlayer and let's try and move closer to it to see what happens...

112
10:03.840 --> 10:06.870
it will fire the SeePlayer

113
10:07.110 --> 10:10.220
and then the rest will run as it did before.

114
10:10.350 --> 10:17.010
So in this case, the while-statement is not helping us with the SeePlayer because it has to be true

115
10:17.040 --> 10:19.920
in order for the rest of the code to run.

116
10:19.920 --> 10:26.670
So in this case, what we've done is turned the tree on its head as far as this SeePlayer is now a

117
10:26.700 --> 10:34.200
Leaf Node, whereas before it was a Decorator Node because it had children underneath it and it was allowing

118
10:34.200 --> 10:40.680
you to make a decision and the decision in that case comes into its own when you want to invert the

119
10:40.680 --> 10:41.760
value that's coming out of it.

120
10:41.760 --> 10:48.980
So the 'while not' is going to be a lot more common in this particular case but it depends on what you're

121
10:48.990 --> 10:49.800
trying to do.

122
10:49.950 --> 10:51.710
So that's the sequence

123
10:51.720 --> 10:58.900
and also adding in that logic. In the next video, we will look at the Selector Nodes.