Welcome back to part two of producing the Hyde behavior.
OK, 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 distance of five or
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 where we want to project to something else a little bit bigger and we say we still
want to go to a distance or two?
Well, we can't do that.
Obviously, if something is like a size of 10, the point would be inside of our hiding object and therefore
we wouldn't be able to compute it for the Nimish.
And as we've seen before, the nerve mesh does not like to try and set positions for its agents for
points that don't exist on the net mesh.
And in other cases, when your agent can't actually make it to its destination, if you've got a code
that has to do 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, Clodagh, that we put on the tree?
That's where it comes in.
What we're going to do is we had a cast array from the cop through the tree.
And then what we're going to do is find out where that right intersects the other side of the chloro.
So the collider that's at the back of the tree, because that's basically where you 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 array, you can do array 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 a 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're going to build on and we're going to call this something different.
So let's call it clever hard.
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
around and do a recast from it before we set up our seke.
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 game object chosen
GEO, which is the tree or whatever it is that you've chosen to hide behind.
So Worldat instance, dockett hiding spots.
And we will set that to the very first one so that we've basically initialized it to something.
OK, so then inside of this is stepmoms where we choose the one that we currently think is the closest
after chosen spotless, just record our chosen direction.
So we are chosen.
Dyax is going to be our hide D or R, 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 rakas
to find out where the spot is behind the tree or behind the clodagh 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.
OK, so we go Clodagh hide coal equals chosen game.
Object dot get component.
Collina.
Now we're doing a get 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 right
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 it shouldn't be too tiresome on your system.
OK, so we've got our collider now.
We're going to create our ride 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, desired direction, and we need to normalize it because the Beccaro 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 10 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 an array cast hit info.
And we're going to create a float distance of how far we want to actually project our rightest 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 distance of one, because this is 10 away up here, it's never
going to hit it and you'll end up hitting nothing.
So I just watch that.
OK, so now we do our hyd call.
So it's our Claure Dot recast and we're using the Bakrie out info and distance.
OK, 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'll 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 Dot 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.
OK, so that's how clever hard, let's say, 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 bottle here and change this distance
to cyto and save it, just to see if we get a bit closer this time.
OK, and he's going to go around the tree.
And.
Hiding, oh, right under the tree you can see there, so he's nice and close to the tree, OK, 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 hit position.
Remember, he's not going to be outside this quarter because it's not stopping the agent.
This collider only calculating a position.
And then we've got the stopping distance that we set on our robot earlier when he was chasing the cop
and will also make a difference to how far away we are from our stopping position.
So if we go back to our inspector on the robot and have a look at our stopping distance, too, if you
want it to be much more accurate, you could set it to zero.
Chances are you might get sort of stuck somewhere.
I'll set it to one and let's just have another.
Look how close we get.
And now we're coming over and you can see that he's got a lot closer to that tree and where the collider
point is, he's practically almost right on that collider there.
So that's much nicer.
Now, we need to test it with something else that's a bit bigger.
OK, so let's just zoom out here.
And what I'm going to do is move the trees out of the road so that they're definitely not the closest
thing for him to hide behind.
Then you'll find in your assets are standard assets folder with the old prototyping standard assets
that Unity used to have, you have a look in the prefabs that you'll find all sorts of different basic
objects, stairs, rooms, houses.
And what we're going to do is just add in, let's say, this House prototype here.
So I just drag and drop that in.
Obviously, we want to make it a little bit smaller because it is enormous.
OK, so let's put that say there and I'll just check that we are touching the ground with it.
OK, now we want to set that to be a hiding spot.
So over an inspector with that selected, you want to select hide for tag.
We also want to add in a box, Clodagh.
So go down to the bottom.
You'll see there's a mesh clutter.
Don't just turn it off, get rid of it altogether.
Because what I have found is that even if you turn it off and leave it on there, it will still cause
problems.
And instead we're going to add in this box clutter.
OK, so now we've got that house and we're ready to go.
We should say this and press play and off he goes.
Now he's gone straight through because you know what I forgot to do?
Break the mesh again.
But before you do, don't forget to set it as a navigation static.
So navigation and make the mesh.
And now if we press play, what is going to do is go and hide on the other side of this house.
Now, if we just manipulate this around a bit so we can see where he ended up from a top view, you
can see that he is in fact, where you would expect that Ray cast to come back from the police through
that claw and then back to that other side of the house, which is exactly where he's stopped.
So now with that all working, as we expected, we can go back into our book code and add in that little
bit of extra that I spoke about in part one, which was making sure that this high doesn't happen.
Every update basically we want to check if we need to hide and we're going to say that the agent needs
to hide if he can see the target.
So the first thing that we're going to add is a little method called Can see Target.
And up here above our update, we'll add that in.
So it's going to be Bool Kansi target.
Then inside of here, we're going to do a recast.
So array cast.
Hit right cast.
In far.
Victim three ready to target.
So we're going to create a ride going from the position of our agent to the position of our target.
So Target dot transformed position on this dot, transform to position.
And then we will say if.
Physics.
Don't recast this dot transformed position.
Right to target.
And the apt for that will be a recast info.
If the object that we hit, so if recast info, dot transform dot game object.
Dog tag equals whatever tag we've got on our policemen or our cop, which is cop, but just check that
your projected pick that up and we will return.
True.
So this is going to perform a Rika's, which if we're hiding behind something and can't be seen by the
cop, then we're not going to hit the cop with this.
Right.
Actually going to hit the Cleide of the object that we're hiding behind.
OK, got a little spelling mistake up here.
Let's fix that.
My cast info and we're returning.
True.
Or else we're going to return false.
So that's our Kansi Tugg.
Now in the update, what we can do is go if.
Can see Target.
Then we will perform our target, hiding from our target ability.
All right, so save that and switch back into unity.
First of all, let's just check that our cop is tagged and it's not.
So that's good.
So we're going to add tag for our cop and put in cop as the new tag.
Select the cop and make it a cop, OK?
Very important.
All right.
So Pressplay.
OK, so he can see he's going to go and hide at that spot.
Now, it's not going to move at all until I start to come around the corner and he's going to go in
the corner.
And he's gone and hid on the other side because of the way that we have projected from the cop through
that object.
OK, so he's now over the.
So let's just walk around the corner.
And as soon as we become visible, it's going to go and hide somewhere else.
Oh.
Now, the reason he's not hiding just around the corner.
I mean, he is in this case, but as soon as I get around that corner, if you watch, I get around
this corner here, he's now going to go over here somewhere because that's where the vector is being
projected.
He could get away with just hiding up here around that corner.
But what we've done isn't that complex, that it will allow that.
But that's OK because he's much further away and he's much safer there than just being around the next
corner.
OK, so that's our steering mechanic of hide.