Hoarding has got to be one of my favorite steering behaviors 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 is 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 lowercase.
Now, if yours doesn't happen to be because of the way it 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.
OK, so that means over here we've got it set to navigation static and when we then make our navigation
mesh, it's going to exclude the tree.
OK, so if we just hit back and we'll do a quick break 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 bike on your navigation.
OK, 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 ride through
it and see if we can see the cop.
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 a tree, you will get a box collider that sits quite neatly around that tree.
You'll be able to see over in the same 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 of it 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 prefab
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 hard positions.
OK, 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 model 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 will also create a private.
Static game object array, which will hold all of our obstacles and let's call them hiding spots.
That, OK, they're more critical of our constructor's 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 dot, find game objects with tag, make sure it's the fun game objects for the nest
so that you get the whole array and we want them with tags with hyd, which are all in lowercase.
OK, so then we need to finish off our singleton of private world.
Which is basically empty and a public static world.
Instant's.
With a.
Get in there, so get return instant's.
Which will 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.
Array, which gets the hiding spots, so get hiding spots.
And there we're going to return hiding spots.
Now, that means that this object is going to go and get all hot 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.
OK, 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.
OK, 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 them to hide really quickly or it can be the furtherest hiding spot in this case, we're
going to find the closest hiding spot to the agent and go and hide there.
OK, so we need a distance value to hold the distance that we're comparing things against.
So initially, let's just set that to --.
If dot infinity so that it's really, really large and that anything you compare 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 three
zero.
OK, then we're going to loop through all of the hiding spots with a full loop.
So for int I equals zero or is less than worldat instance doget hiding spots.
Length.
I plus, plus.
OK, then we're going to create a victim three, which will be our hide.
Direction, and that's going to equal our tree, so our world instant's.
Don't get hiding.
Spots or spots, brackets I dot transformed position minus target, dot transformed position.
So this is the vector from the cop to the tree.
OK, so Victor 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 hard 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.
OK, so we've got that.
Now, what are we 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.
OK, so we want if vector three don't distance this dot transform drop 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.
Paw's.
And then we'll update distance, so distance is going to equal the victor through 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 trade that we've found so far.
OK, and then we'll finish our for loop.
This he should be dissed, not what it's updated to so destroy.
And now that we've got that, we can actually go Agent Dot Sitt destination to the chosen spot.
And if we really want to be consistent rather than doing that, because that's what Seke is doing anyway,
what just you see in case we do something different to seek.
OK, 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 will will sit this to be hiding.
OK, 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 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 not 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.
And I going to go controlled.
I hit the key and then just make a new tree and move it around, control D and move it around.
Drill D there.
And what did I say?
You have to remember.
Do you have to go into your navigation and you have to remember to bike to make sure that you've done
that for the new NAV Meshach.
So Pressplay 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.
For seemed to go to a different tree, depending on whether you can find months closer.
No, now, one way we can do that is if we go back into the 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 this update this to, let's say 10, 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 going to hide behind that tree because it's the closest, but it's going to go to a distance of
10.
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 you push him now he goes and hides behind that tree at a distance of 10.
And so if I come back over here and force him around the other side.
At a distance of 10, oh, I don't know which one to be closer to.
Yeah, that one there.
OK, so it's gone over there.
OK, great.
And you can see that a distance of ten of hiding behind a tree looks a little bit unnatural because
if you were to hide behind a tree, you'd be right up against the tree.
And as I said, we will get around to fixing that.
OK, so that's the basic hyd method.
We're going to come back in the next video in part two and actually improve on that.