WEBVTT

00:13.020 --> 00:17.710
Welcome back to part two of producing the Hyde behavior.

00:18.210 --> 00:25.560
OK, so so far we have our agent and we've managed to get him on the other side of a hiding spot, in

00:25.560 --> 00:31.860
this case, the tree, and then push him at a certain distance along the vector.

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

00:38.580 --> 00:39.490
distance of ten.

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

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

00:54.690 --> 00:55.320
itself.

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

01:00.030 --> 01:01.560
Now it's going to work for the tree.

01:01.560 --> 01:09.210
But what happens where we want to project to something else a little bit bigger and we say we still

01:09.210 --> 01:10.540
want to go to a distance or two?

01:10.560 --> 01:12.690
Well, we can't do that.

01:12.690 --> 01:21.120
Obviously, if something is like a size of 10, the point would be inside of our hiding object and therefore

01:21.120 --> 01:23.700
we wouldn't be able to compute it for the Nimish.

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

01:31.140 --> 01:33.600
points that don't exist on the net mesh.

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

01:39.690 --> 01:46.320
that has to do with that, then you'll never be able to get to your hiding place in order to continue

01:46.320 --> 01:47.640
and do something else.

01:47.910 --> 01:49.800
So how do we fix this?

01:50.700 --> 01:54.060
Well, remember the box, Clodagh, that we put on the tree?

01:54.510 --> 01:56.160
That's where it comes in.

01:56.320 --> 02:01.950
What we're going to do is we had a cast array from the cop through the tree.

02:02.580 --> 02:10.860
And then what we're going to do is find out where that right intersects the other side of the chloro.

02:10.860 --> 02:18.840
So the collider that's at the back of the tree, because that's basically where you can go and stand.

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

02:26.400 --> 02:27.190
inside of it.

02:28.350 --> 02:29.460
So how do we do this?

02:29.490 --> 02:36.180
Well, it's actually not as easy as it sounds, because you can cast array, you can do array cast and

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

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

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

02:55.290 --> 03:00.090
And then when it comes out the other side, we want to turn it around and then send it back and then

03:00.090 --> 03:06.060
wherever it hits on the other side as it's coming back, that is where we calculate our hiding spot

03:06.060 --> 03:15.150
from knowing that that spot is going to be right up against our mesh or our obstacle hiding spot tree,

03:15.330 --> 03:18.230
whatever it is that we're using as a hiding spot.

03:19.440 --> 03:21.120
So let's implement this.

03:21.120 --> 03:22.980
Let's go back into our bot code.

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

03:29.730 --> 03:33.950
that we're going to build on and we're going to call this something different.

03:33.960 --> 03:36.170
So let's call it clever hard.

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

03:42.540 --> 03:43.670
down here as well.

03:45.210 --> 03:47.770
So that we can run this one now.

03:47.880 --> 03:52.380
What we want to do is still find the closest tree, so we have the right stuff here.

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

03:59.700 --> 04:04.320
around and do a recast from it before we set up our seke.

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

04:13.890 --> 04:16.040
spot, which we're already getting.

04:16.500 --> 04:20.700
We also need to remember the chosen direction.

04:20.700 --> 04:26.940
So Vector three chosen direction, which will equal.

04:27.910 --> 04:38.380
Vector 3.0, and we also want to get the game object that we're remembering, so game object chosen

04:39.730 --> 04:43.900
GEO, which is the tree or whatever it is that you've chosen to hide behind.

04:43.900 --> 04:49.230
So Worldat instance, dockett hiding spots.

04:50.140 --> 04:55.900
And we will set that to the very first one so that we've basically initialized it to something.

04:56.290 --> 05:03.100
OK, so then inside of this is stepmoms where we choose the one that we currently think is the closest

05:03.850 --> 05:07.380
after chosen spotless, just record our chosen direction.

05:08.170 --> 05:09.040
So we are chosen.

05:09.040 --> 05:21.970
Dyax is going to be our hide D or R, and then our chosen game object will be this world hiding spot

05:21.970 --> 05:24.030
at position I.

05:25.030 --> 05:30.820
All right, so now that we have these values, let's go down before our seek and do our little rakas

05:30.820 --> 05:39.250
to find out where the spot is behind the tree or behind the clodagh where we can go and sit our agent

05:39.250 --> 05:40.690
next do so.

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

05:44.770 --> 05:51.400
OK, so we go Clodagh hide coal equals chosen game.

05:51.400 --> 05:55.810
Object dot get component.

05:57.960 --> 05:58.770
Collina.

06:01.490 --> 06:08.180
Now we're doing a get component in here, which is not the most optimized thing to be running, but

06:08.180 --> 06:11.420
there really is no other option of where to put that.

06:11.420 --> 06:16.820
I guess you could go through all of the world objects as you're creating them into your world right

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

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

06:27.680 --> 06:28.340
time.

06:28.340 --> 06:32.570
So it shouldn't be too tiresome on your system.

06:32.930 --> 06:34.880
OK, so we've got our collider now.

06:34.880 --> 06:37.940
We're going to create our ride that goes back in the other direction.

06:37.940 --> 06:42.950
So let's call it our back Ray, and that's going to equal a new ray.

06:45.290 --> 06:47.210
From the chosen spot.

06:49.020 --> 07:00.300
In the negative, chosen, desired direction, and we need to normalize it because the Beccaro needs

07:00.300 --> 07:02.430
a normalized direction.

07:02.820 --> 07:05.500
So now we're coming back in the other direction.

07:05.940 --> 07:14.130
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:14.130 --> 07:19.020
because you're creating this hiding spot at a distance of ten and then turning around and coming back

07:19.020 --> 07:19.440
the other way.

07:19.470 --> 07:25.500
Now, if your object happens to be way bigger than 10 distance to get across, you're still going to

07:25.500 --> 07:27.320
end up sitting in the center of it.

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

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

07:37.200 --> 07:46.380
Okay, so next thing we're going to do here is we're going to create an array cast hit info.

07:47.650 --> 07:53.890
And we're going to create a float distance of how far we want to actually project our rightest if we've

07:53.890 --> 08:02.320
hit anything, so we're going to say 100, which is a fair way to project it and make sure that this

08:02.320 --> 08:06.960
distance here is definitely longer than this distance here.

08:07.450 --> 08:13.360
Otherwise, if you project this like for distance of one, because this is 10 away up here, it's never

08:13.360 --> 08:15.840
going to hit it and you'll end up hitting nothing.

08:16.150 --> 08:17.200
So I just watch that.

08:17.470 --> 08:19.560
OK, so now we do our hyd call.

08:19.570 --> 08:29.590
So it's our Claure Dot recast and we're using the Bakrie out info and distance.

08:32.440 --> 08:35.170
OK, so what do we get from that?

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

08:43.660 --> 08:54.010
So our seek position now is going to be not our chosen spot, but it'll be the info dot point where

08:54.010 --> 08:55.150
the hit happened.

08:55.660 --> 09:04.420
Plus, you still want to move away from the object a bit to give the agent a bit of room because the

09:04.420 --> 09:08.920
agent has a certain size and you still need to just sort of push it away.

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

09:10.750 --> 09:22.210
Using our chosen direction that we were using before Dot normalized and multiply it by five was quite

09:22.210 --> 09:24.460
good for the size of these particular agents.

09:24.460 --> 09:26.500
And you could put it down to two, if you like.

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

09:32.140 --> 09:33.110
and get away with it.

09:34.150 --> 09:40.480
OK, so that's how clever hard, let's say, that go back into unity and we'll press play.

09:41.350 --> 09:46.590
And this time we will hide and we're going to hide.

09:47.170 --> 09:48.460
We have a look up here.

09:50.470 --> 09:57.250
Quite close to the tree now, if we put that down, go back into the bottle here and change this distance

09:57.250 --> 10:05.040
to cyto and save it, just to see if we get a bit closer this time.

10:06.340 --> 10:08.680
OK, and he's going to go around the tree.

10:09.870 --> 10:10.500
And.

10:12.400 --> 10:23.050
Hiding, oh, right under the tree you can see there, so he's nice and close to the tree, OK, but

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

10:30.430 --> 10:32.560
have been possibly over here.

10:32.860 --> 10:36.750
So that's as close as he's getting to that particular hit position.

10:36.760 --> 10:40.840
Remember, he's not going to be outside this quarter because it's not stopping the agent.

10:40.990 --> 10:44.410
This collider only calculating a position.

10:44.680 --> 10:52.120
And then we've got the stopping distance that we set on our robot earlier when he was chasing the cop

10:52.120 --> 10:57.550
and will also make a difference to how far away we are from our stopping position.

10:57.550 --> 11:04.180
So if we go back to our inspector on the robot and have a look at our stopping distance, too, if you

11:04.180 --> 11:08.320
want it to be much more accurate, you could set it to zero.

11:08.320 --> 11:10.920
Chances are you might get sort of stuck somewhere.

11:11.200 --> 11:13.600
I'll set it to one and let's just have another.

11:13.600 --> 11:16.310
Look how close we get.

11:16.330 --> 11:22.750
And now we're coming over and you can see that he's got a lot closer to that tree and where the collider

11:22.750 --> 11:27.750
point is, he's practically almost right on that collider there.

11:27.790 --> 11:29.310
So that's much nicer.

11:30.280 --> 11:32.830
Now, we need to test it with something else that's a bit bigger.

11:32.980 --> 11:35.410
OK, so let's just zoom out here.

11:36.130 --> 11:43.660
And what I'm going to do is move the trees out of the road so that they're definitely not the closest

11:43.660 --> 11:46.210
thing for him to hide behind.

11:48.950 --> 11:55.640
Then you'll find in your assets are standard assets folder with the old prototyping standard assets

11:55.640 --> 12:01.130
that Unity used to have, you have a look in the prefabs that you'll find all sorts of different basic

12:01.400 --> 12:03.650
objects, stairs, rooms, houses.

12:04.430 --> 12:08.780
And what we're going to do is just add in, let's say, this House prototype here.

12:08.790 --> 12:11.420
So I just drag and drop that in.

12:12.110 --> 12:15.530
Obviously, we want to make it a little bit smaller because it is enormous.

12:16.580 --> 12:22.970
OK, so let's put that say there and I'll just check that we are touching the ground with it.

12:24.820 --> 12:30.310
OK, now we want to set that to be a hiding spot.

12:30.320 --> 12:35.640
So over an inspector with that selected, you want to select hide for tag.

12:36.260 --> 12:39.920
We also want to add in a box, Clodagh.

12:40.340 --> 12:42.230
So go down to the bottom.

12:42.230 --> 12:43.440
You'll see there's a mesh clutter.

12:43.550 --> 12:47.570
Don't just turn it off, get rid of it altogether.

12:47.750 --> 12:52.700
Because what I have found is that even if you turn it off and leave it on there, it will still cause

12:52.700 --> 12:53.390
problems.

12:53.780 --> 12:58.040
And instead we're going to add in this box clutter.

12:58.460 --> 13:02.920
OK, so now we've got that house and we're ready to go.

13:02.930 --> 13:08.740
We should say this and press play and off he goes.

13:08.750 --> 13:12.410
Now he's gone straight through because you know what I forgot to do?

13:12.710 --> 13:14.270
Break the mesh again.

13:14.840 --> 13:18.530
But before you do, don't forget to set it as a navigation static.

13:19.070 --> 13:22.100
So navigation and make the mesh.

13:22.880 --> 13:30.400
And now if we press play, what is going to do is go and hide on the other side of this house.

13:31.610 --> 13:38.330
Now, if we just manipulate this around a bit so we can see where he ended up from a top view, you

13:38.330 --> 13:47.480
can see that he is in fact, where you would expect that Ray cast to come back from the police through

13:47.840 --> 13:53.690
that claw and then back to that other side of the house, which is exactly where he's stopped.

13:56.160 --> 14:02.640
So now with that all working, as we expected, we can go back into our book code and add in that little

14:02.640 --> 14:09.930
bit of extra that I spoke about in part one, which was making sure that this high doesn't happen.

14:09.930 --> 14:18.930
Every update basically we want to check if we need to hide and we're going to say that the agent needs

14:18.930 --> 14:23.710
to hide if he can see the target.

14:24.210 --> 14:30.890
So the first thing that we're going to add is a little method called Can see Target.

14:31.350 --> 14:34.770
And up here above our update, we'll add that in.

14:34.780 --> 14:39.960
So it's going to be Bool Kansi target.

14:41.400 --> 14:45.720
Then inside of here, we're going to do a recast.

14:45.720 --> 14:47.430
So array cast.

14:48.740 --> 14:51.170
Hit right cast.

14:53.010 --> 14:53.730
In far.

14:55.660 --> 15:00.760
Victim three ready to target.

15:01.300 --> 15:09.270
So we're going to create a ride going from the position of our agent to the position of our target.

15:09.700 --> 15:19.360
So Target dot transformed position on this dot, transform to position.

15:21.270 --> 15:23.760
And then we will say if.

15:25.350 --> 15:26.160
Physics.

15:27.950 --> 15:34.800
Don't recast this dot transformed position.

15:36.600 --> 15:39.660
Right to target.

15:41.810 --> 15:45.320
And the apt for that will be a recast info.

15:48.500 --> 15:58.520
If the object that we hit, so if recast info, dot transform dot game object.

16:00.450 --> 16:07.710
Dog tag equals whatever tag we've got on our policemen or our cop, which is cop, but just check that

16:07.710 --> 16:11.610
your projected pick that up and we will return.

16:11.760 --> 16:12.120
True.

16:12.150 --> 16:18.180
So this is going to perform a Rika's, which if we're hiding behind something and can't be seen by the

16:18.180 --> 16:24.120
cop, then we're not going to hit the cop with this.

16:24.120 --> 16:24.570
Right.

16:24.600 --> 16:27.760
Actually going to hit the Cleide of the object that we're hiding behind.

16:28.360 --> 16:30.670
OK, got a little spelling mistake up here.

16:30.690 --> 16:31.480
Let's fix that.

16:31.490 --> 16:34.290
My cast info and we're returning.

16:34.290 --> 16:35.160
True.

16:35.850 --> 16:39.440
Or else we're going to return false.

16:39.870 --> 16:41.850
So that's our Kansi Tugg.

16:41.850 --> 16:44.490
Now in the update, what we can do is go if.

16:45.740 --> 16:47.390
Can see Target.

16:49.380 --> 16:56.700
Then we will perform our target, hiding from our target ability.

16:57.450 --> 17:00.030
All right, so save that and switch back into unity.

17:01.530 --> 17:06.180
First of all, let's just check that our cop is tagged and it's not.

17:06.180 --> 17:06.860
So that's good.

17:06.900 --> 17:13.350
So we're going to add tag for our cop and put in cop as the new tag.

17:13.530 --> 17:17.810
Select the cop and make it a cop, OK?

17:17.850 --> 17:18.540
Very important.

17:18.840 --> 17:19.170
All right.

17:19.170 --> 17:20.400
So Pressplay.

17:20.880 --> 17:24.260
OK, so he can see he's going to go and hide at that spot.

17:24.270 --> 17:29.250
Now, it's not going to move at all until I start to come around the corner and he's going to go in

17:29.250 --> 17:29.880
the corner.

17:32.740 --> 17:38.950
And he's gone and hid on the other side because of the way that we have projected from the cop through

17:38.950 --> 17:40.360
that object.

17:40.390 --> 17:43.270
OK, so he's now over the.

17:44.890 --> 17:47.220
So let's just walk around the corner.

17:50.040 --> 17:55.220
And as soon as we become visible, it's going to go and hide somewhere else.

17:57.460 --> 17:57.650
Oh.

17:59.410 --> 18:03.070
Now, the reason he's not hiding just around the corner.

18:03.220 --> 18:09.220
I mean, he is in this case, but as soon as I get around that corner, if you watch, I get around

18:09.220 --> 18:16.480
this corner here, he's now going to go over here somewhere because that's where the vector is being

18:16.480 --> 18:17.270
projected.

18:18.640 --> 18:22.690
He could get away with just hiding up here around that corner.

18:22.720 --> 18:26.350
But what we've done isn't that complex, that it will allow that.

18:26.350 --> 18:31.630
But that's OK because he's much further away and he's much safer there than just being around the next

18:31.630 --> 18:32.250
corner.

18:33.980 --> 18:38.240
OK, so that's our steering mechanic of hide.
