WEBVTT

00:00.000 --> 00:00.420
Hello guys.

00:00.420 --> 00:01.410
Welcome to the video.

00:01.410 --> 00:05.070
In this one we're going to learn how to work with the Trigger Collider.

00:05.070 --> 00:10.170
So this is going to be useful for many things in the game including knockback and the player from a

00:10.170 --> 00:13.320
trap and picking up fruits for a player as well.

00:13.440 --> 00:14.730
So let's just learn.

00:14.730 --> 00:15.930
How does it work.

00:15.930 --> 00:18.330
We can create an empty object.

00:19.220 --> 00:21.290
Let's just call it trigger for now.

00:21.800 --> 00:23.360
Let's find this object.

00:24.300 --> 00:29.160
Let's place it somewhere here and let's add a box collider to this object.

00:30.170 --> 00:32.180
I want to make this bigger.

00:32.180 --> 00:34.610
So I'm going to make maybe a size two by two.

00:36.060 --> 00:37.860
And we're going to place it over here.

00:37.950 --> 00:43.530
Now let's go to the game and you can see if I come to this collider and I try to walk inside, it's

00:43.530 --> 00:44.430
not going to work.

00:44.430 --> 00:48.570
And that is very obvious because this is a collider and we cannot pass by the collider.

00:48.570 --> 00:55.800
But if we click is trigger over here, then it becomes a trigger area and we can walk through the collider.

00:55.800 --> 01:02.370
Now this is very helpful because we can not only walk through the collider, but also we can check what

01:02.370 --> 01:04.470
entered the trigger area.

01:05.010 --> 01:07.320
Let's make a quick script over here.

01:07.320 --> 01:11.730
Let's do let's call it damage Trigger.

01:12.330 --> 01:13.200
Just like that.

01:14.580 --> 01:19.290
Now we can go inside of this script, and over here we have nothing.

01:19.290 --> 01:23.220
But we're going to type on trigger enter to D.

01:23.550 --> 01:28.080
So now this method will be called every time something collides with a trigger.

01:28.080 --> 01:33.030
When we enter in the trigger, I want to be sure that player entered the trigger.

01:33.030 --> 01:34.500
There are many ways to do that.

01:34.500 --> 01:36.450
One of them is to use tag.

01:36.450 --> 01:40.920
I'm going to show you how to use tags to detect player, but after that I'm not going to use it because

01:40.920 --> 01:42.180
I prefer another way.

01:42.180 --> 01:44.160
And I'm going to show you another way on how to do that.

01:44.160 --> 01:44.550
Okay.

01:44.550 --> 01:45.720
So let's do this.

01:45.720 --> 01:50.640
If collision dot tag equals to player.

01:50.640 --> 01:54.090
And here it is very important to have a very same name as a tag.

01:54.120 --> 01:56.880
So type it just as I did it over here.

01:56.880 --> 02:02.790
And then we're going to do debug log player enter trigger.

02:03.950 --> 02:04.850
Just that.

02:05.000 --> 02:06.530
Now let's go back.

02:07.260 --> 02:11.550
Let's find the player and let's give it a tag of a player.

02:11.820 --> 02:16.380
And let's select the trigger and make sure we check is trigger.

02:16.740 --> 02:18.630
Now we can go to play mode again.

02:20.260 --> 02:24.940
And in the console message, you'll see that player entered trigger.

02:24.970 --> 02:30.040
Now, as you can understand, we not only can debug the message, but also we can do a knockback.

02:30.040 --> 02:31.180
For example.

02:31.510 --> 02:33.430
Uh, let's go ahead.

02:36.230 --> 02:42.020
And instead of this we're going to the game manager dot instance player knockback.

02:43.090 --> 02:44.110
Just like that.

02:44.110 --> 02:46.120
Let's go ahead and save this.

02:48.780 --> 02:50.040
And now you'll see.

02:50.040 --> 02:53.610
When we go in close to this box, we're going to get knocked.

02:54.630 --> 02:55.830
Very cool rate.

02:56.640 --> 02:58.050
Yeah, I like it a lot.

02:58.080 --> 03:02.850
Now, what I was talking about, I really don't like to do it this way, because this kind of reminds

03:02.850 --> 03:03.840
me of spaghetti.

03:03.840 --> 03:09.750
We need to go to the game manager to call the player so we can call the knockback method.

03:10.780 --> 03:12.400
What else we can do.

03:12.430 --> 03:15.130
And although it's not that bad, it's actually very efficient.

03:15.130 --> 03:17.650
But sometimes people make mistake with the tags.

03:17.650 --> 03:23.590
Sometimes tags can be used for something else, and sometimes people just don't prefer it this way as

03:23.590 --> 03:24.100
I do.

03:24.310 --> 03:25.960
So this is what we're going to do.

03:25.990 --> 03:28.660
We can type here player, player.

03:28.660 --> 03:31.090
And this is going to be a local variable of a player.

03:31.090 --> 03:36.190
And it's going to be equals to collision GameObject Getcomponent of a player.

03:36.430 --> 03:44.350
Now this variable is going to have a value only if we can find component of a player on this object

03:44.350 --> 03:46.900
that entered the trigger on the collision.

03:46.900 --> 03:48.310
And then we're going to type this.

03:48.310 --> 03:56.170
If player not equals to null which checks if we assign the component then we're going to do player knockback.

03:56.170 --> 04:00.610
Now this method is much more independent and we can use it just like that.

04:01.330 --> 04:04.900
Uh let's go ahead and see if this works.

04:05.530 --> 04:06.970
We're going to go back.

04:08.960 --> 04:10.370
We're going to go to play mode.

04:11.660 --> 04:14.600
And we were knocked back.

04:15.190 --> 04:15.850
Very cool.

04:15.850 --> 04:16.540
Right.

04:16.570 --> 04:19.540
Now let's go back here and I'm going to show you something else.

04:20.080 --> 04:26.530
We can check if this one is not null by just typing a question mark over here.

04:26.860 --> 04:30.190
And then we do need this line of code just like that.

04:34.850 --> 04:39.650
So this one basically, uh, very similar to the null check over here.

04:39.650 --> 04:42.200
It's like asking player to knock back.

04:42.200 --> 04:44.990
And if no one responds, then there is no knock back.

04:45.050 --> 04:46.970
Let's go ahead and try this.

04:53.700 --> 04:55.830
And boom knocked.

04:56.280 --> 04:57.180
Very cool.

04:57.210 --> 05:00.240
However, I do not recommend to use this one.

05:00.240 --> 05:03.090
Let's just do it.

05:03.090 --> 05:03.450
Uh.

05:04.510 --> 05:05.350
Like so.

05:07.500 --> 05:08.130
Okay.

05:09.700 --> 05:10.630
Very well.

05:10.630 --> 05:13.090
So that's all what we need here and now.

05:13.090 --> 05:19.270
Every time we make a trap or an enemy, we can just make a trigger area on that trap and assign this

05:19.270 --> 05:20.920
script together with the trigger.

05:20.920 --> 05:22.180
And it's going to work just like that.

05:22.180 --> 05:24.490
We're going to damage enemy with whatever we need.

05:24.490 --> 05:29.320
As of now, I'm just going to delete this trigger and I want to do a bit of a clean up.

05:29.320 --> 05:31.030
So let's make a folder.

05:31.770 --> 05:37.020
You're going to call it scripts, and we're going to take all of the scripts we have and drag them inside

05:37.020 --> 05:43.650
of this folder, because if we don't manage everything from the start, it can make quite a mess in

05:43.650 --> 05:45.150
this window.

05:45.480 --> 05:50.610
Also, for the material, we don't really have any other materials in the game, but still I'm going

05:50.610 --> 05:55.500
to make a folder, I'm going to call it material and I'm going to drag it here.

05:55.530 --> 05:56.130
Okay.

05:56.130 --> 05:56.880
Very cool.

05:56.880 --> 05:58.590
Let's go to the next video.
