Hiding has got to be one of my favorite steering behaviours because it's kind of like a little bit sneaky
and you can also create things that aren't trying to just hide from a particular character but are hiding
as they're sort of sneaking up.
So hiding and pursuing, which gives you really interesting dynamics in a game environment.
So in order to do this, it's actually quite simple, but there's an awful lot of work involved.
First of all, you need a whole bunch of obstacles that your agent can hide behind.
So we've got our tree that's been sitting there the whole time.
We're going to use that now in order to figure out where the best spot to hide is.
If the robber is going to hide from the cop, then what we do is we get a vector from the cop to the
tree and keep going through the center of the tree to the opposite side, which means that if you're
on the exact opposite side of the tree from the cop, then you're obviously hidden from the cop.
Because if you tried to do a recast from the cop to the robber or the robber to the cop, they wouldn't
be able to see each other because it would hit the tree and therefore you are hidden.
So first of all, what we need to do is define what all the hiding spots in the actual world are.
So back in our environment, if we have a look in the hierarchy under the environment object, you'll
find the tree object that's been sitting there for quite a while and if you click on it, you'll see
that it's tagged with the tag hide in lower case.
Now if yours doesn't happen to be because of the way it is imported, make sure you just create a new
tag and set the tree to hide.
And then we can use that hide tag for any object you want to put in the environment to hide behind.
Remember also that the tree is a navigation static.
Okay, so that means over here we've got it set to navigation static.
And when we then bake our navigation mesh, it's going to exclude the tree.
So if we just hit bake and it will do a quick bake for us, we're going to find that it has been eliminated
as a spot that you can stand.
So if you go and add multiple trees, always come back and do this, bake on your navigation.
Okay, so now that we've got that, we also want to make the tree a collider now, not because we want
our character to collide with it, but because at some point we want to be able to cast a ray through
it and see if we can see the cup.
So select the tree and go to the inspector, add component and I'm going to add a collider.
We want to add a box collider.
That's just because it's going to be simple.
Now with the tree you will get a box collider that sits quite neatly around that tree.
You'll be able to see over in the scene view.
If you've got some other object and it's not fitting neatly, then you may want to just adjust the center
and the size over in the inspector for that.
Collider And now that you've got this tree with the tag and the collider, you could essentially make
a pre-fab of it or copy and paste or duplicate it around the environment.
And then you've got lots of places to hide, right?
So we've got all these hiding places.
We've got one at the moment.
It's always good to test with just one thing so that we can figure out what went wrong.
If it does, we need to pick up all of these trees at the very beginning of pressing play.
So I'm going to create a singleton and I'm going to call that singleton the world, and it's going to
hold on to all of our hide positions.
So let's call it world and let's go into here.
Now, what we're going to do with this is not make it into a mono behavior.
Let's get rid of that and we're going to make it a public sealed class.
And then we're going to create a private static read only world instance which will equal a new world.
And we'll also create a private static game object array which will hold all of our obstacles and let's
call them hiding spots.
Not that they're more create all of our constructors so static world.
And in this constructor, it's actually going to go through the world and grab all of the hiding spots.
So hiding spots.
Equals game object.
Find game objects with tag.
Make sure it's the find game objects for the nest so that you get the whole array.
And we want them with tags with hide which are all in lowercase.
So then we need to finish off our singleton a private world.
Which is basically empty and a public static world.
Instance.
With a.
Get in there.
So get return instance.
Which would be a lowercase I because we're talking about this one up here, I.
And then we'll also have another method public game object.
A right, which gets the hiding spots.
So get hiding spots.
And in there, we're going to return hiding spots.
Now that means that this object is going to go and get all the hiding spots and any agents in the environment
just have to ask the world for them from the world.
Singleton And they don't have to keep getting them all of the time.
Okay, so save that.
And now let's go back to Unity.
The next thing we're going to do is to change our bot to now do the hide.
So open up your bot script and let's now add in a hide method.
So down here we're going to put in void hide.
Now what Hyde needs to do is to find the best hiding place.
Now, that can be the closest hiding spot if you want him to hide really quickly, or it can be the
furthest hiding spot in this case.
We're going to find the closest hiding spot to the agent and go and hide there.
Okay.
So we need a distance value to hold the distance that we're comparing things against.
So initially, let's just set that to math f infinity so that it's really, really large and that anything
you can pair it with is got to be smaller.
And then we need a vector three for our chosen spot.
And initially we'll set that to vector 3.0.
Okay.
Then we're going to loop through all of the hiding spots with a four loop.
So four int i equals zero i is less than world instance dot get hiding spots.
Dot length.
I.
Plus.
Okay.
Then we're going to create a vector three, which will be our hide.
Direction and that's going to equal our tree.
So our world dot instance.
Don't get hiding.
Sports or sports brackets.
I dot transformed position minus target transformed position.
So this is the vector from the cop to the tree.
Okay.
So vector three for our hide position.
Is going to equal the same thing again.
So I'm just going to grab this whole position of the tree.
So that's the position of the tree.
Then what we're going to do is actually add in a little bit more room so that you actually go past the
tree.
So to do that, you just add in the high direction vector to that and we want to normalize it.
So the normalized version, so we know the length of one and then we can set a distance behind that
tree that we want to hide.
In this case, we're just going to hide at a distance of five behind the tree.
And you can play around with that depending on how close you want to be to that particular tree.
And we will actually improve on this algorithm later on and get rid of that five.
But for now, let's set it to five.
Okay, so we've got that.
Now what we're going to do is check if the next tree that comes along is actually closer.
And if it is, we will use that as our chosen spot instead.
So we want if vector three dot distance this dot transform dot position, hide, pause.
So this is the distance between our agent and the hiding position.
If that's less than dist, then that's where we're going to go and hide.
So in here we'll put chosen spot equals hide.
Pause.
And then we'll update.
Distance.
So distance is going to equal the vector three distance here that we just tested.
So grab that and put it down there.
So it's going to now be the distance to the closest tree that we've found so far.
Okay.
And then we'll finish our for loop.
This here should be dissed, not what it's updated to.
So dissed.
And now that we've got that, we can actually go agent dot set destination to the chosen spot.
And if we really want to be consistent rather than doing that, because that's what Seek is doing anyway,
we'll just use Seek in case we do something different to seek.
Okay, so that's our hide.
Quite straightforward when you think about it.
Save that.
Now, what we're going to do is in our update, we will set this to be hiding.
Okay, so let's save it.
Now let's go back into unity, and we're pretty much ready to play and test this out.
So press play and off our agent goes and hides behind the tree.
Right.
So now if I move over to here.
He's going to go over there and if I move again, he's going to go there.
So he's constantly hiding.
Behind the tree, which is nice, great for us.
But if you think about the processing that's going on now, the hide algorithm is running every update.
If he's already hidden from the cop, then we don't need to keep running that until a point comes where
he's not hidden anymore.
And we will fix that in the next video.
But for now, you probably want to add a few more trees to see how this works.
So let's just go into the environment and grab the tree that you've got.
Am I going to go control DH, hit the W key and then just make a new tree and move it around control
DH And move it around control DH Over there.
And what did I say?
You have to remember to do.
You have to go into your navigation and you have to remember to bake to make sure that you've done that
for the new nav mesh.
Okay, so press play.
Now he's going to go and hide behind that tree because it is actually the closest and you can actually
see that.
And now because of the way he's currently programmed, you might not be able to.
Force him to go to a different tree depending on whether he can find one that's closer.
No.
Now, one way we can do that is if we go back into the bot code and remember this little five we put
about how far away we are from the tree.
If we hide behind a tree at a certain distance from that tree and then that distance ends up being closer
to another tree, later on he will move.
So let's update this to, let's say ten, which I think is probably going to be stretching things a
bit as far as the distance.
But let's just press play.
And.
It's got to hide behind that tree because it's the closest, but it's going to go to a distance of ten.
So let's just bring this down.
You can see that he's now over here.
So I'm going to go and chase him up with my cop.
And hopefully push him.
Now he goes and hides behind that tree at a distance of ten.
Okay.
So that's the basic hide method.
We're going to come back in the next video in part two and actually improve on that.