In this lecture, we'll take a look at the Selector Node. The Selector Node allows you to have many different
leaves underneath it but just pick from 1 leaf.
Now you can do that randomly or you can do it randomly with a certain amount of chance that it would
be picked.
We'll do an example where we're going to have our bot go into an idle state, where it just looks
around its environment by turning on the spot. To get it to turn, we need to add a new method.
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
through and then calculate a new target to be looking at based on that turn angle. So we add the turn angle
which is using this Quaternion.AngleAxis method by turning through an angle around the up-axis from
our forward direction
and that gives you a vector that you can add to your position, which will be a point away from you but
at that angle.
So by setting that as the target, it'll give the bot something to turn towards.
Just like when we targeted the player up here
but instead we're giving it an actual angle to turn through.
So that's the turn. That's all we need to do to our code before we can use this next Behavior Tree.
So creating a new Behavior Tree called 'LookAround'
and this is what I've got in mine.
So this LookAround.BT file is going to have a 'sequence' and that sequence will be a random
Selector Node, then the LookAtTarget and then the WaitRandom. Under the 'random' selector node,
it's got 4 different possible versions of the turn so we can turn 90 degrees, we can turn -90
degrees,
we can turn 145 degrees or we can turn -27 degrees
and having these under 'random' means that every time this is run, just one of these gets picked and
it's the direction to turn in.
Then the LookAtTarget will turn the bot around towards that particular direction
and then after that, we have a WaitRandom.
Now the WaitRandom is similar to the Wait that we've seen before that comes with our Behavior Tree
system
but you can give it 2 random values and it will pick a random amount of time to wait between those
2 values.
So it might wait for 2, might wait for 3, might wait for 4.5.
It's just going to give you a bit of variety in that.
So, just save that and then what you want to do is select your Droid and make sure you drag-and-drop your
LookAround Behavior Tree onto it. Check then that you've got the nice pink script with the red parameters
which means that everything's ready to go,
you've got everything defined in the script and then press Play.
Now if you watch your Behavior Tree (currently mine is waiting, there it goes) it will flip into random and
then you'll see it pick one of the different turns to take and it's completely at random so you don't
know which one you're going to get
and the bot will then turn to it and you can see from the behavior of the bot, that it's
like an Idle state, he's just there looking around himself.
So that's not too bad. If you want to force a particular node to happen more times than something else,
what you can do with the random node (if we open our script again) is put weighted values after it, for
each of these.
So if you want the first one to happen first (and mostly), let's set that to 5 and then the
rest to 1.
These are just weighted values, they're not a chance percentage or anything.
So whichever has a higher number, will happen more often.
In this case you'll get the first one (Turn 90) happen five times more than you're going to get the 1s
occurring because of this weighting up here.
So don't be concerned about having them add up to 100 or anything like that. Back in Unity, press Play and
have a look at how it picks which turn to use over in the Inspector and you'll see that more often it
picks that first one and will continue doing that
a lot more than the others, which will be on equal weighting and that's pretty much all there is to
Selector Nodes.