WEBVTT

0
00:11.220 --> 00:18.010
In this lecture, we'll take a look at the Selector Node. The Selector Node allows you to have many different

1
00:18.010 --> 00:22.570
leaves underneath it but just pick from 1 leaf.

2
00:22.570 --> 00:28.420
Now you can do that randomly or you can do it randomly with a certain amount of chance that it would

3
00:28.420 --> 00:29.670
be picked.

4
00:29.740 --> 00:36.070
We'll do an example where we're going to have our bot go into an idle state, where it just looks

5
00:36.070 --> 00:42.370
around its environment by turning on the spot. To get it to turn, we need to add a new method.

6
00:42.550 --> 00:50.410
So in the AI class, we're going to add this task here called 'Turn' and we will give the bot an angle to turn

7
00:50.410 --> 01:00.970
through and then calculate a new target to be looking at based on that turn angle. So we add the turn angle

8
01:01.480 --> 01:12.340
which is using this Quaternion.AngleAxis method by turning through an angle around the up-axis from

9
01:12.400 --> 01:13.970
our forward direction

10
01:14.140 --> 01:22.540
and that gives you a vector that you can add to your position, which will be a point away from you but

11
01:22.540 --> 01:23.820
at that angle.

12
01:23.830 --> 01:29.010
So by setting that as the target, it'll give the bot something to turn towards.

13
01:29.020 --> 01:32.450
Just like when we targeted the player up here

14
01:32.740 --> 01:35.680
but instead we're giving it an actual angle to turn through.

15
01:35.680 --> 01:43.400
So that's the turn. That's all we need to do to our code before we can use this next Behavior Tree.

16
01:43.480 --> 01:46.930
So creating a new Behavior Tree called 'LookAround'

17
01:47.020 --> 01:49.170
and this is what I've got in mine.

18
01:49.180 --> 01:59.440
So this LookAround.BT file is going to have a 'sequence' and that sequence will be a random

19
01:59.440 --> 02:08.070
Selector Node, then the LookAtTarget and then the WaitRandom. Under the 'random' selector node,

20
02:08.290 --> 02:15.880
it's got 4 different possible versions of the turn so we can turn 90 degrees, we can turn -90

21
02:15.890 --> 02:16.390
degrees,

22
02:16.390 --> 02:21.160
we can turn 145 degrees or we can turn -27 degrees

23
02:21.490 --> 02:27.560
and having these under 'random' means that every time this is run, just one of these gets picked and

24
02:27.580 --> 02:29.730
it's the direction to turn in.

25
02:30.040 --> 02:36.600
Then the LookAtTarget will turn the bot around towards that particular direction

26
02:37.060 --> 02:40.010
and then after that, we have a WaitRandom.

27
02:40.030 --> 02:46.170
Now the WaitRandom is similar to the Wait that we've seen before that comes with our Behavior Tree

28
02:46.170 --> 02:47.050
system

29
02:47.170 --> 02:54.700
but you can give it 2 random values and it will pick a random amount of time to wait between those

30
02:54.700 --> 02:55.100
2 values.

31
02:55.120 --> 03:00.510
So it might wait for 2, might wait for 3, might wait for 4.5.

32
03:00.520 --> 03:04.360
It's just going to give you a bit of variety in that.

33
03:04.360 --> 03:12.760
So, just save that and then what you want to do is select your Droid and make sure you drag-and-drop your

34
03:12.760 --> 03:22.150
LookAround Behavior Tree onto it. Check then that you've got the nice pink script with the red parameters

35
03:22.150 --> 03:24.060
which means that everything's ready to go,

36
03:24.060 --> 03:29.300
you've got everything defined in the script and then press Play.

37
03:30.860 --> 03:37.280
Now if you watch your Behavior Tree (currently mine is waiting, there it goes) it will flip into random and

38
03:37.280 --> 03:43.520
then you'll see it pick one of the different turns to take and it's completely at random so you don't

39
03:43.520 --> 03:45.150
know which one you're going to get

40
03:45.440 --> 03:51.080
and the bot will then turn to it and you can see from the behavior of the bot, that it's

41
03:51.080 --> 03:54.570
like an Idle state, he's just there looking around himself.

42
03:57.460 --> 04:06.690
So that's not too bad. If you want to force a particular node to happen more times than something else,

43
04:06.860 --> 04:14.620
what you can do with the random node (if we open our script again) is put weighted values after it, for

44
04:14.680 --> 04:15.870
each of these.

45
04:16.060 --> 04:24.430
So if you want the first one to happen first (and mostly), let's set that to 5 and then the

46
04:24.430 --> 04:25.820
rest to 1.

47
04:25.810 --> 04:29.740
These are just weighted values, they're not a chance percentage or anything.

48
04:29.740 --> 04:33.760
So whichever has a higher number, will happen more often.

49
04:33.940 --> 04:42.760
In this case you'll get the first one (Turn 90) happen five times more than you're going to get the 1s

50
04:42.850 --> 04:45.120
occurring because of this weighting up here.

51
04:45.130 --> 04:52.390
So don't be concerned about having them add up to 100 or anything like that. Back in Unity, press Play and

52
04:52.390 --> 05:01.000
have a look at how it picks which turn to use over in the Inspector and you'll see that more often it

53
05:01.000 --> 05:04.390
picks that first one and will continue doing that

54
05:05.270 --> 05:12.720
a lot more than the others, which will be on equal weighting and that's pretty much all there is to

55
05:12.780 --> 05:13.180
Selector Nodes.