WEBVTT

00:01.290 --> 00:07.410
Hiding has got to be one of my favorite steering behaviours because it's kind of like a little bit sneaky

00:07.410 --> 00:13.920
and you can also create things that aren't trying to just hide from a particular character but are hiding

00:13.920 --> 00:15.510
as they're sort of sneaking up.

00:15.510 --> 00:22.130
So hiding and pursuing, which gives you really interesting dynamics in a game environment.

00:22.140 --> 00:27.960
So in order to do this, it's actually quite simple, but there's an awful lot of work involved.

00:27.960 --> 00:32.970
First of all, you need a whole bunch of obstacles that your agent can hide behind.

00:32.970 --> 00:36.240
So we've got our tree that's been sitting there the whole time.

00:36.240 --> 00:43.560
We're going to use that now in order to figure out where the best spot to hide is.

00:43.560 --> 00:51.510
If the robber is going to hide from the cop, then what we do is we get a vector from the cop to the

00:51.510 --> 00:59.250
tree and keep going through the center of the tree to the opposite side, which means that if you're

00:59.370 --> 01:08.610
on the exact opposite side of the tree from the cop, then you're obviously hidden from the cop.

01:08.610 --> 01:15.030
Because if you tried to do a recast from the cop to the robber or the robber to the cop, they wouldn't

01:15.030 --> 01:19.950
be able to see each other because it would hit the tree and therefore you are hidden.

01:20.610 --> 01:28.560
So first of all, what we need to do is define what all the hiding spots in the actual world are.

01:29.550 --> 01:35.940
So back in our environment, if we have a look in the hierarchy under the environment object, you'll

01:35.940 --> 01:42.090
find the tree object that's been sitting there for quite a while and if you click on it, you'll see

01:42.090 --> 01:46.230
that it's tagged with the tag hide in lower case.

01:46.230 --> 01:51.330
Now if yours doesn't happen to be because of the way it is imported, make sure you just create a new

01:51.330 --> 01:54.390
tag and set the tree to hide.

01:54.390 --> 02:01.230
And then we can use that hide tag for any object you want to put in the environment to hide behind.

02:01.260 --> 02:06.960
Remember also that the tree is a navigation static.

02:06.960 --> 02:12.000
Okay, so that means over here we've got it set to navigation static.

02:12.150 --> 02:19.890
And when we then bake our navigation mesh, it's going to exclude the tree.

02:20.190 --> 02:29.370
So if we just hit bake and it will do a quick bake for us, we're going to find that it has been eliminated

02:29.370 --> 02:30.720
as a spot that you can stand.

02:30.720 --> 02:36.810
So if you go and add multiple trees, always come back and do this, bake on your navigation.

02:36.930 --> 02:45.330
Okay, so now that we've got that, we also want to make the tree a collider now, not because we want

02:45.330 --> 02:51.390
our character to collide with it, but because at some point we want to be able to cast a ray through

02:51.390 --> 02:53.880
it and see if we can see the cup.

02:53.880 --> 03:00.750
So select the tree and go to the inspector, add component and I'm going to add a collider.

03:00.750 --> 03:02.310
We want to add a box collider.

03:02.310 --> 03:04.530
That's just because it's going to be simple.

03:04.770 --> 03:10.020
Now with the tree you will get a box collider that sits quite neatly around that tree.

03:10.020 --> 03:12.390
You'll be able to see over in the scene view.

03:12.480 --> 03:20.010
If you've got some other object and it's not fitting neatly, then you may want to just adjust the center

03:20.010 --> 03:23.190
and the size over in the inspector for that.

03:23.190 --> 03:29.610
Collider And now that you've got this tree with the tag and the collider, you could essentially make

03:29.610 --> 03:34.590
a pre-fab of it or copy and paste or duplicate it around the environment.

03:34.590 --> 03:38.850
And then you've got lots of places to hide, right?

03:38.850 --> 03:40.520
So we've got all these hiding places.

03:40.550 --> 03:41.910
We've got one at the moment.

03:41.910 --> 03:46.920
It's always good to test with just one thing so that we can figure out what went wrong.

03:46.920 --> 03:53.280
If it does, we need to pick up all of these trees at the very beginning of pressing play.

03:53.280 --> 03:58.020
So I'm going to create a singleton and I'm going to call that singleton the world, and it's going to

03:58.020 --> 04:01.980
hold on to all of our hide positions.

04:02.610 --> 04:08.400
So let's call it world and let's go into here.

04:08.430 --> 04:14.340
Now, what we're going to do with this is not make it into a mono behavior.

04:15.390 --> 04:22.200
Let's get rid of that and we're going to make it a public sealed class.

04:22.200 --> 04:35.610
And then we're going to create a private static read only world instance which will equal a new world.

04:37.710 --> 04:49.260
And we'll also create a private static game object array which will hold all of our obstacles and let's

04:49.260 --> 04:53.100
call them hiding spots.

04:54.090 --> 05:00.990
Not that they're more create all of our constructors so static world.

05:04.650 --> 05:10.170
And in this constructor, it's actually going to go through the world and grab all of the hiding spots.

05:10.200 --> 05:11.970
So hiding spots.

05:13.190 --> 05:15.230
Equals game object.

05:15.830 --> 05:19.880
Find game objects with tag.

05:20.400 --> 05:24.710
Make sure it's the find game objects for the nest so that you get the whole array.

05:24.710 --> 05:30.770
And we want them with tags with hide which are all in lowercase.

05:31.820 --> 05:35.870
So then we need to finish off our singleton a private world.

05:36.670 --> 05:43.000
Which is basically empty and a public static world.

05:44.570 --> 05:45.770
Instance.

05:48.610 --> 05:49.960
With a.

05:51.240 --> 05:52.320
Get in there.

05:52.320 --> 05:56.640
So get return instance.

05:59.250 --> 06:04.290
Which would be a lowercase I because we're talking about this one up here, I.

06:04.950 --> 06:11.130
And then we'll also have another method public game object.

06:12.090 --> 06:15.390
A right, which gets the hiding spots.

06:15.390 --> 06:18.450
So get hiding spots.

06:19.780 --> 06:24.430
And in there, we're going to return hiding spots.

06:26.970 --> 06:32.250
Now that means that this object is going to go and get all the hiding spots and any agents in the environment

06:32.250 --> 06:35.700
just have to ask the world for them from the world.

06:35.700 --> 06:38.850
Singleton And they don't have to keep getting them all of the time.

06:38.850 --> 06:40.230
Okay, so save that.

06:41.220 --> 06:43.650
And now let's go back to Unity.

06:43.650 --> 06:51.180
The next thing we're going to do is to change our bot to now do the hide.

06:51.510 --> 06:56.850
So open up your bot script and let's now add in a hide method.

06:56.850 --> 07:00.360
So down here we're going to put in void hide.

07:02.700 --> 07:08.550
Now what Hyde needs to do is to find the best hiding place.

07:08.550 --> 07:16.290
Now, that can be the closest hiding spot if you want him to hide really quickly, or it can be the

07:16.290 --> 07:19.050
furthest hiding spot in this case.

07:19.050 --> 07:24.720
We're going to find the closest hiding spot to the agent and go and hide there.

07:25.110 --> 07:25.350
Okay.

07:25.410 --> 07:30.900
So we need a distance value to hold the distance that we're comparing things against.

07:30.900 --> 07:38.730
So initially, let's just set that to math f infinity so that it's really, really large and that anything

07:38.730 --> 07:40.890
you can pair it with is got to be smaller.

07:41.070 --> 07:45.810
And then we need a vector three for our chosen spot.

07:46.470 --> 07:51.930
And initially we'll set that to vector 3.0.

07:53.100 --> 07:53.380
Okay.

07:53.490 --> 07:57.600
Then we're going to loop through all of the hiding spots with a four loop.

07:57.600 --> 08:10.740
So four int i equals zero i is less than world instance dot get hiding spots.

08:12.170 --> 08:13.370
Dot length.

08:15.250 --> 08:15.630
I.

08:15.640 --> 08:15.910
Plus.

08:15.910 --> 08:16.600
Plus.

08:17.740 --> 08:18.030
Okay.

08:18.070 --> 08:21.820
Then we're going to create a vector three, which will be our hide.

08:22.840 --> 08:27.760
Direction and that's going to equal our tree.

08:27.760 --> 08:31.240
So our world dot instance.

08:32.160 --> 08:34.590
Don't get hiding.

08:36.380 --> 08:41.090
Sports or sports brackets.

08:41.660 --> 08:51.860
I dot transformed position minus target transformed position.

08:51.860 --> 08:55.850
So this is the vector from the cop to the tree.

08:56.610 --> 08:56.890
Okay.

08:56.960 --> 09:01.880
So vector three for our hide position.

09:03.070 --> 09:06.700
Is going to equal the same thing again.

09:06.700 --> 09:12.310
So I'm just going to grab this whole position of the tree.

09:14.690 --> 09:16.780
So that's the position of the tree.

09:16.790 --> 09:23.960
Then what we're going to do is actually add in a little bit more room so that you actually go past the

09:23.960 --> 09:24.550
tree.

09:24.560 --> 09:32.570
So to do that, you just add in the high direction vector to that and we want to normalize it.

09:32.570 --> 09:37.880
So the normalized version, so we know the length of one and then we can set a distance behind that

09:37.880 --> 09:39.350
tree that we want to hide.

09:39.710 --> 09:44.390
In this case, we're just going to hide at a distance of five behind the tree.

09:44.390 --> 09:49.040
And you can play around with that depending on how close you want to be to that particular tree.

09:49.040 --> 09:54.410
And we will actually improve on this algorithm later on and get rid of that five.

09:54.440 --> 09:56.540
But for now, let's set it to five.

09:56.840 --> 09:58.370
Okay, so we've got that.

09:58.370 --> 10:03.500
Now what we're going to do is check if the next tree that comes along is actually closer.

10:03.500 --> 10:07.100
And if it is, we will use that as our chosen spot instead.

10:07.700 --> 10:21.290
So we want if vector three dot distance this dot transform dot position, hide, pause.

10:21.290 --> 10:25.700
So this is the distance between our agent and the hiding position.

10:25.700 --> 10:31.430
If that's less than dist, then that's where we're going to go and hide.

10:31.430 --> 10:36.770
So in here we'll put chosen spot equals hide.

10:38.970 --> 10:40.020
Pause.

10:41.440 --> 10:42.650
And then we'll update.

10:42.670 --> 10:43.690
Distance.

10:43.690 --> 10:51.220
So distance is going to equal the vector three distance here that we just tested.

10:51.220 --> 10:54.190
So grab that and put it down there.

10:54.190 --> 11:00.160
So it's going to now be the distance to the closest tree that we've found so far.

11:00.760 --> 11:01.270
Okay.

11:01.270 --> 11:04.150
And then we'll finish our for loop.

11:05.730 --> 11:09.390
This here should be dissed, not what it's updated to.

11:09.420 --> 11:10.680
So dissed.

11:12.630 --> 11:22.650
And now that we've got that, we can actually go agent dot set destination to the chosen spot.

11:25.160 --> 11:32.360
And if we really want to be consistent rather than doing that, because that's what Seek is doing anyway,

11:32.360 --> 11:36.230
we'll just use Seek in case we do something different to seek.

11:36.650 --> 11:38.780
Okay, so that's our hide.

11:38.870 --> 11:42.380
Quite straightforward when you think about it.

11:42.740 --> 11:43.570
Save that.

11:43.580 --> 11:49.610
Now, what we're going to do is in our update, we will set this to be hiding.

11:50.540 --> 11:52.290
Okay, so let's save it.

11:52.310 --> 11:58.130
Now let's go back into unity, and we're pretty much ready to play and test this out.

11:58.130 --> 12:03.470
So press play and off our agent goes and hides behind the tree.

12:03.770 --> 12:04.070
Right.

12:04.070 --> 12:07.220
So now if I move over to here.

12:08.870 --> 12:12.680
He's going to go over there and if I move again, he's going to go there.

12:12.680 --> 12:14.660
So he's constantly hiding.

12:16.010 --> 12:20.060
Behind the tree, which is nice, great for us.

12:20.060 --> 12:27.440
But if you think about the processing that's going on now, the hide algorithm is running every update.

12:27.740 --> 12:35.600
If he's already hidden from the cop, then we don't need to keep running that until a point comes where

12:35.600 --> 12:37.060
he's not hidden anymore.

12:37.070 --> 12:39.890
And we will fix that in the next video.

12:39.890 --> 12:43.790
But for now, you probably want to add a few more trees to see how this works.

12:43.790 --> 12:49.250
So let's just go into the environment and grab the tree that you've got.

12:49.250 --> 12:58.220
Am I going to go control DH, hit the W key and then just make a new tree and move it around control

12:58.250 --> 13:05.840
DH And move it around control DH Over there.

13:05.840 --> 13:07.220
And what did I say?

13:07.220 --> 13:08.330
You have to remember to do.

13:08.330 --> 13:14.960
You have to go into your navigation and you have to remember to bake to make sure that you've done that

13:15.680 --> 13:17.120
for the new nav mesh.

13:17.120 --> 13:18.920
Okay, so press play.

13:19.100 --> 13:25.010
Now he's going to go and hide behind that tree because it is actually the closest and you can actually

13:25.010 --> 13:25.760
see that.

13:26.000 --> 13:31.510
And now because of the way he's currently programmed, you might not be able to.

13:34.220 --> 13:38.540
Force him to go to a different tree depending on whether he can find one that's closer.

13:40.410 --> 13:41.110
No.

13:41.310 --> 13:47.160
Now, one way we can do that is if we go back into the bot code and remember this little five we put

13:47.160 --> 13:49.450
about how far away we are from the tree.

13:49.470 --> 13:54.540
If we hide behind a tree at a certain distance from that tree and then that distance ends up being closer

13:54.540 --> 13:57.060
to another tree, later on he will move.

13:57.060 --> 14:01.320
So let's update this to, let's say ten, which I think is probably going to be stretching things a

14:01.320 --> 14:02.910
bit as far as the distance.

14:03.360 --> 14:05.490
But let's just press play.

14:06.260 --> 14:07.580
And.

14:08.380 --> 14:12.580
It's got to hide behind that tree because it's the closest, but it's going to go to a distance of ten.

14:12.580 --> 14:14.380
So let's just bring this down.

14:14.380 --> 14:16.330
You can see that he's now over here.

14:17.490 --> 14:21.420
So I'm going to go and chase him up with my cop.

14:22.940 --> 14:26.260
And hopefully push him.

14:26.270 --> 14:30.080
Now he goes and hides behind that tree at a distance of ten.

14:31.180 --> 14:31.420
Okay.

14:31.740 --> 14:35.010
So that's the basic hide method.

14:35.160 --> 14:41.310
We're going to come back in the next video in part two and actually improve on that.
