WEBVTT

00:06.100 --> 00:11.080
Welcome back to part two of producing the hide behavior.

00:11.340 --> 00:11.710
Okay.

00:11.710 --> 00:18.880
So so far we have our agent and we've managed to get him on the other side of a hiding spot, in this

00:18.880 --> 00:25.000
case the tree, and then push him at a certain distance along the vector.

00:25.000 --> 00:31.630
That's from the cop to the tree and beyond, you know, for a distance of two or a distance of five

00:31.630 --> 00:32.860
or a distance of ten.

00:33.160 --> 00:38.730
Now, what happens if you end up with objects that are all different sizes?

00:38.740 --> 00:47.890
Well, the position that we measure the cops vector from and through is the center of the actual model

00:47.890 --> 00:48.490
itself.

00:48.490 --> 00:52.510
And the tree center in this case is at the center of its base.

00:53.110 --> 00:54.730
Now it's going to work for the tree.

00:54.730 --> 01:02.560
But what happens when we want to project to something else a little bit bigger and we say we still want

01:02.560 --> 01:03.730
to go to a distance of two?

01:03.730 --> 01:05.860
Well, we can't do that.

01:05.860 --> 01:13.570
Obviously, if something is like a size of ten, it would the point would be inside of our hiding object

01:13.570 --> 01:16.870
and therefore we wouldn't be able to compute it for the net of mesh.

01:16.870 --> 01:24.700
And as we've seen before, the mesh does not like to try and set positions for its agents for points

01:24.700 --> 01:26.980
that don't exist on the mesh.

01:27.280 --> 01:32.830
And in other cases, when your agent can't actually make it to its destination, if you've got code

01:32.830 --> 01:39.490
that has to deal with that, then you'll never be able to get to your hiding place in order to continue

01:39.490 --> 01:41.020
and do something else.

01:41.020 --> 01:43.210
So how do we fix this?

01:43.780 --> 01:47.500
Well, remember the box collider that we put on the tree?

01:47.530 --> 01:49.480
That's where it comes in.

01:49.480 --> 01:56.890
What we're going to do is we're going to cast a ray from the cup through the tree and then what are

01:56.890 --> 02:04.000
we going to do is find out where that ray intersects the other side of the collider.

02:04.000 --> 02:11.380
So the collider that's at the back of the tree because that's basically where your agent can go and

02:11.380 --> 02:12.040
stand.

02:12.040 --> 02:19.600
And that's as close as the agent can stand to the obstacle or the hiding spot without sort of being

02:19.600 --> 02:20.560
inside of it.

02:21.430 --> 02:22.630
So how do we do this?

02:22.660 --> 02:29.350
Well, it's actually not as easy as it sounds, because you can cast a ray, you can do a ray cast and

02:29.350 --> 02:38.560
hit a collider on the front side, but you can't do it on the other side as it's traveling through.

02:38.680 --> 02:42.310
So when it comes out of the collider, there's no way to detect that.

02:43.030 --> 02:48.430
So what we have to do is take our ray and push it through the collider.

02:48.430 --> 02:52.930
And then when it comes out the other side, we want to turn it around and then send it back.

02:52.930 --> 02:58.840
And then wherever it hits on the other side as it's coming back, that is where we calculate our hiding

02:58.840 --> 03:07.960
spot from knowing that that spot is going to be right up against our mesh or our obstacle hiding spot,

03:07.960 --> 03:11.560
tree, whatever it is that we're using as our hiding spot.

03:12.460 --> 03:14.290
So let's implement this.

03:14.290 --> 03:16.360
Let's go back into our bot code.

03:16.360 --> 03:23.020
We're going to, first of all, grab the entire hide and copy and paste it because it's our basis that

03:23.020 --> 03:27.130
we want to build on and we're going to call this something different.

03:27.130 --> 03:29.320
So let's call it clever hide.

03:29.320 --> 03:35.710
So you've got two copies of a hiding mechanic if you want to use them, and then we'll put clever hide

03:35.710 --> 03:37.030
down here as well.

03:38.300 --> 03:40.160
So that we can run this one.

03:40.400 --> 03:43.730
Now, what we want to do is still find the closest tree.

03:43.730 --> 03:45.560
So we have the right stuff here.

03:45.560 --> 03:52.850
But what we need to do after we've got the closest tree is grab our vector to that closest tree, turn

03:52.850 --> 03:57.710
it around and do a ray cast from it before we set up our seek.

03:58.430 --> 04:07.100
So the things that we're going to need to remember from having gone through this for loop are the chosen

04:07.100 --> 04:09.440
spot, which we're already getting.

04:09.530 --> 04:13.880
We also need to remember the chosen direction.

04:13.880 --> 04:20.360
So vector three chosen direction which will equal.

04:21.100 --> 04:24.190
Vector 3.0.

04:24.670 --> 04:28.390
And we also want to get the game object that we're remembering.

04:28.390 --> 04:37.060
So a game object chosen geo, which is the tree or whatever it is that you've chosen to hide behind.

04:37.060 --> 04:37.660
So.

04:37.690 --> 04:46.300
Well, dot instance, dot get hiding spots and we will set that to the very first one so that we've

04:46.300 --> 04:49.330
basically initialized it to something.

04:49.360 --> 04:49.840
Okay.

04:49.840 --> 04:56.470
So then inside of this, if statements where we choose the one that we currently think is the closest

04:56.980 --> 05:00.730
after Chosen Spotless, just record our chosen direction.

05:01.300 --> 05:14.710
So our chosen deer is going to be our hide deer and then our chosen game object will be this world hiding

05:14.710 --> 05:17.560
spot at position I.

05:18.220 --> 05:18.610
All right.

05:18.610 --> 05:24.400
So now that we have these values, let's go down before our seek and do our little REI class to find

05:24.400 --> 05:32.860
out where the spot is behind the tree or behind the collider where we can go and sit our agent next

05:32.860 --> 05:33.880
do so.

05:33.880 --> 05:37.690
First of all, we're going to get hold of the collider that's on the object.

05:38.140 --> 05:49.300
So we go collider hide col equals chosen game object dot get component.

05:51.130 --> 05:52.120
Collider.

05:54.540 --> 06:01.500
Now we're doing a component in here which is not the most optimized thing to be running, but there

06:01.500 --> 06:04.590
really is no other option of where to put that.

06:04.590 --> 06:10.020
I guess you could go through all of the world objects as you're creating them into your world array

06:10.020 --> 06:14.910
here and keep another array of just the colliders to use.

06:15.420 --> 06:20.880
But we're also remember down the track going to make sure that we don't actually run this all of the

06:20.880 --> 06:21.510
time.

06:21.510 --> 06:25.950
So shouldn't be too tiresome on your system.

06:26.040 --> 06:28.050
Okay, so we've got our collider now.

06:28.050 --> 06:31.110
We're going to create our ray that goes back in the other direction.

06:31.110 --> 06:36.360
So let's call it our back ray and that's going to equal a new ray.

06:38.400 --> 06:40.650
From the chosen spot.

06:42.180 --> 06:45.210
In the negative chosen

06:47.340 --> 06:55.830
direction and we need to normalize it because the back ray needs a normalized direction.

06:55.830 --> 06:58.900
So now we're coming back in the other direction.

06:58.920 --> 07:07.290
Now, what you may want to do also is with this one here is to push out this ray that you're going through

07:07.290 --> 07:12.210
because you're creating this hiding spot at a distance of ten and then turning around and coming back

07:12.210 --> 07:12.600
the other way.

07:12.630 --> 07:18.690
Now, if your object happens to be way bigger than ten distance to get across, you're still going to

07:18.690 --> 07:20.490
end up sitting in the center of it.

07:20.490 --> 07:27.540
And when you do a projection back the other way, you won't have come back and hit the other side.

07:27.930 --> 07:30.240
So you might just have to do some testing with that.

07:30.270 --> 07:30.750
Okay.

07:30.750 --> 07:39.870
So next thing we're going to do here is we're going to create a ray cast, hit info.

07:40.850 --> 07:46.850
And we're going to create a float distance of how far we want to actually project our rated test if

07:46.850 --> 07:48.050
we've hit anything.

07:48.860 --> 07:56.750
So we're going to say 100, which is a fair way to project it and make sure that this distance here

07:56.750 --> 08:00.350
is definitely longer than this distance here.

08:00.590 --> 08:06.320
Otherwise, if you project this like for a distance of one, because this is ten away up here, it's

08:06.320 --> 08:09.190
never going to hit it and you'll end up hitting nothing.

08:09.200 --> 08:10.570
So just watch that.

08:10.580 --> 08:10.940
Okay.

08:10.940 --> 08:22.040
So now we do our hide coal, so it's our cleaner dot recast and we're using the back ray out info and

08:22.040 --> 08:23.000
distance.

08:25.610 --> 08:26.120
Okay.

08:26.120 --> 08:28.340
So what do we get from that?

08:28.340 --> 08:36.620
Well, we get the hit point at the back, which is going to be stored inside of info.

08:36.650 --> 08:47.210
So our seek position now is going to be not our chosen spot, but it will be the info dot point where

08:47.210 --> 08:48.560
the hit happened.

08:48.680 --> 08:57.860
Plus you still want to move away from the object a bit to give the agent a bit of room because the agent

08:57.860 --> 09:02.090
has a certain size and you still need to just sort of push it away.

09:02.090 --> 09:03.920
So I'm just going to push it back away again.

09:03.920 --> 09:15.560
Using our chosen direction that we were using before normalized and multiply it by five was quite good

09:15.560 --> 09:19.790
for the size of these particular agents, and you could put it down to two if you like.

09:19.790 --> 09:25.310
Again, you'd have to test it with the size of your agents and how far away that you think you can be

09:25.310 --> 09:26.480
and get away with it.

09:27.290 --> 09:28.820
Okay, so that's how clever hide.

09:28.820 --> 09:29.600
Let's save that.

09:29.600 --> 09:33.830
Go back into unity and we'll press play.

09:34.400 --> 09:35.870
And this time.

09:37.330 --> 09:40.000
We will hide and we're going to hide.

09:40.360 --> 09:41.920
We have a look up here.

09:43.640 --> 09:45.660
Quite close to the tree.

09:45.680 --> 09:53.930
Now, if we put that down, go back into the boat here and change this distance to say two and save

09:53.930 --> 09:58.430
it just to see if we get a bit closer this time.

09:59.610 --> 10:00.030
Okay.

10:00.110 --> 10:02.060
And he's going to go around the tree.

10:03.140 --> 10:03.920
And.

10:05.600 --> 10:06.260
Harding.

10:06.410 --> 10:11.730
Oh, right under the tree, you can see there.

10:11.990 --> 10:14.900
So he's nice and close to the tree.

10:15.110 --> 10:15.680
Okay.

10:15.680 --> 10:23.600
But if we select that tree, which is this one here, you'll see that the hit position coming back would

10:23.600 --> 10:25.880
have been possibly over here.

10:25.880 --> 10:29.900
So that's as close as he's getting to that particular position.

10:29.900 --> 10:35.080
Remember, he's not going to be outside this collider because it's not stopping the agent this collider.

10:35.090 --> 10:35.570
Okay.

10:35.570 --> 10:37.220
We'll leave this video here.

10:37.220 --> 10:44.570
When we come back, in part three of this hiding, we're going to modify the behavior of the rubber

10:44.570 --> 10:48.800
so that you can actually sneak up behind him with the cop.
