Welcome back to part two of producing the hide behavior.
Okay.
So so far we have our agent and we've managed to get him on the other side of a hiding spot, in this
case the tree, and then push him at a certain distance along the vector.
That's from the cop to the tree and beyond, you know, for a distance of two or a distance of five
or a distance of ten.
Now, what happens if you end up with objects that are all different sizes?
Well, the position that we measure the cops vector from and through is the center of the actual model
itself.
And the tree center in this case is at the center of its base.
Now it's going to work for the tree.
But what happens when we want to project to something else a little bit bigger and we say we still want
to go to a distance of two?
Well, we can't do that.
Obviously, if something is like a size of ten, it would the point would be inside of our hiding object
and therefore we wouldn't be able to compute it for the net of mesh.
And as we've seen before, the mesh does not like to try and set positions for its agents for points
that don't exist on the mesh.
And in other cases, when your agent can't actually make it to its destination, if you've got code
that has to deal with that, then you'll never be able to get to your hiding place in order to continue
and do something else.
So how do we fix this?
Well, remember the box collider that we put on the tree?
That's where it comes in.
What we're going to do is we're going to cast a ray from the cup through the tree and then what are
we going to do is find out where that ray intersects the other side of the collider.
So the collider that's at the back of the tree because that's basically where your agent can go and
stand.
And that's as close as the agent can stand to the obstacle or the hiding spot without sort of being
inside of it.
So how do we do this?
Well, it's actually not as easy as it sounds, because you can cast a ray, you can do a ray cast and
hit a collider on the front side, but you can't do it on the other side as it's traveling through.
So when it comes out of the collider, there's no way to detect that.
So what we have to do is take our ray and push it through the collider.
And then when it comes out the other side, we want to turn it around and then send it back.
And then wherever it hits on the other side as it's coming back, that is where we calculate our hiding
spot from knowing that that spot is going to be right up against our mesh or our obstacle hiding spot,
tree, whatever it is that we're using as our hiding spot.
So let's implement this.
Let's go back into our bot code.
We're going to, first of all, grab the entire hide and copy and paste it because it's our basis that
we want to build on and we're going to call this something different.
So let's call it clever hide.
So you've got two copies of a hiding mechanic if you want to use them, and then we'll put clever hide
down here as well.
So that we can run this one.
Now, what we want to do is still find the closest tree.
So we have the right stuff here.
But what we need to do after we've got the closest tree is grab our vector to that closest tree, turn
it around and do a ray cast from it before we set up our seek.
So the things that we're going to need to remember from having gone through this for loop are the chosen
spot, which we're already getting.
We also need to remember the chosen direction.
So vector three chosen direction which will equal.
Vector 3.0.
And we also want to get the game object that we're remembering.
So a game object chosen geo, which is the tree or whatever it is that you've chosen to hide behind.
So.
Well, dot instance, dot get hiding spots and we will set that to the very first one so that we've
basically initialized it to something.
Okay.
So then inside of this, if statements where we choose the one that we currently think is the closest
after Chosen Spotless, just record our chosen direction.
So our chosen deer is going to be our hide deer and then our chosen game object will be this world hiding
spot at position I.
All right.
So now that we have these values, let's go down before our seek and do our little REI class to find
out where the spot is behind the tree or behind the collider where we can go and sit our agent next
do so.
First of all, we're going to get hold of the collider that's on the object.
So we go collider hide col equals chosen game object dot get component.
Collider.
Now we're doing a component in here which is not the most optimized thing to be running, but there
really is no other option of where to put that.
I guess you could go through all of the world objects as you're creating them into your world array
here and keep another array of just the colliders to use.
But we're also remember down the track going to make sure that we don't actually run this all of the
time.
So shouldn't be too tiresome on your system.
Okay, so we've got our collider now.
We're going to create our ray that goes back in the other direction.
So let's call it our back ray and that's going to equal a new ray.
From the chosen spot.
In the negative chosen
direction and we need to normalize it because the back ray needs a normalized direction.
So now we're coming back in the other direction.
Now, what you may want to do also is with this one here is to push out this ray that you're going through
because you're creating this hiding spot at a distance of ten and then turning around and coming back
the other way.
Now, if your object happens to be way bigger than ten distance to get across, you're still going to
end up sitting in the center of it.
And when you do a projection back the other way, you won't have come back and hit the other side.
So you might just have to do some testing with that.
Okay.
So next thing we're going to do here is we're going to create a ray cast, hit info.
And we're going to create a float distance of how far we want to actually project our rated test if
we've hit anything.
So we're going to say 100, which is a fair way to project it and make sure that this distance here
is definitely longer than this distance here.
Otherwise, if you project this like for a distance of one, because this is ten away up here, it's
never going to hit it and you'll end up hitting nothing.
So just watch that.
Okay.
So now we do our hide coal, so it's our cleaner dot recast and we're using the back ray out info and
distance.
Okay.
So what do we get from that?
Well, we get the hit point at the back, which is going to be stored inside of info.
So our seek position now is going to be not our chosen spot, but it will be the info dot point where
the hit happened.
Plus you still want to move away from the object a bit to give the agent a bit of room because the agent
has a certain size and you still need to just sort of push it away.
So I'm just going to push it back away again.
Using our chosen direction that we were using before normalized and multiply it by five was quite good
for the size of these particular agents, and you could put it down to two if you like.
Again, you'd have to test it with the size of your agents and how far away that you think you can be
and get away with it.
Okay, so that's how clever hide.
Let's save that.
Go back into unity and we'll press play.
And this time.
We will hide and we're going to hide.
We have a look up here.
Quite close to the tree.
Now, if we put that down, go back into the boat here and change this distance to say two and save
it just to see if we get a bit closer this time.
Okay.
And he's going to go around the tree.
And.
Harding.
Oh, right under the tree, you can see there.
So he's nice and close to the tree.
Okay.
But if we select that tree, which is this one here, you'll see that the hit position coming back would
have been possibly over here.
So that's as close as he's getting to that particular position.
Remember, he's not going to be outside this collider because it's not stopping the agent this collider.
Okay.
We'll leave this video here.
When we come back, in part three of this hiding, we're going to modify the behavior of the rubber
so that you can actually sneak up behind him with the cop.