Hello guys.
Welcome to the video.
In this one we're going to learn how to work with the Trigger Collider.
So this is going to be useful for many things in the game including knockback and the player from a
trap and picking up fruits for a player as well.
So let's just learn.
How does it work.
We can create an empty object.
Let's just call it trigger for now.
Let's find this object.
Let's place it somewhere here and let's add a box collider to this object.
I want to make this bigger.
So I'm going to make maybe a size two by two.
And we're going to place it over here.
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
not going to work.
And that is very obvious because this is a collider and we cannot pass by the collider.
But if we click is trigger over here, then it becomes a trigger area and we can walk through the collider.
Now this is very helpful because we can not only walk through the collider, but also we can check what
entered the trigger area.
Let's make a quick script over here.
Let's do let's call it damage Trigger.
Just like that.
Now we can go inside of this script, and over here we have nothing.
But we're going to type on trigger enter to D.
So now this method will be called every time something collides with a trigger.
When we enter in the trigger, I want to be sure that player entered the trigger.
There are many ways to do that.
One of them is to use tag.
I'm going to show you how to use tags to detect player, but after that I'm not going to use it because
I prefer another way.
And I'm going to show you another way on how to do that.
Okay.
So let's do this.
If collision dot tag equals to player.
And here it is very important to have a very same name as a tag.
So type it just as I did it over here.
And then we're going to do debug log player enter trigger.
Just that.
Now let's go back.
Let's find the player and let's give it a tag of a player.
And let's select the trigger and make sure we check is trigger.
Now we can go to play mode again.
And in the console message, you'll see that player entered trigger.
Now, as you can understand, we not only can debug the message, but also we can do a knockback.
For example.
Uh, let's go ahead.
And instead of this we're going to the game manager dot instance player knockback.
Just like that.
Let's go ahead and save this.
And now you'll see.
When we go in close to this box, we're going to get knocked.
Very cool rate.
Yeah, I like it a lot.
Now, what I was talking about, I really don't like to do it this way, because this kind of reminds
me of spaghetti.
We need to go to the game manager to call the player so we can call the knockback method.
What else we can do.
And although it's not that bad, it's actually very efficient.
But sometimes people make mistake with the tags.
Sometimes tags can be used for something else, and sometimes people just don't prefer it this way as
I do.
So this is what we're going to do.
We can type here player, player.
And this is going to be a local variable of a player.
And it's going to be equals to collision GameObject Getcomponent of a player.
Now this variable is going to have a value only if we can find component of a player on this object
that entered the trigger on the collision.
And then we're going to type this.
If player not equals to null which checks if we assign the component then we're going to do player knockback.
Now this method is much more independent and we can use it just like that.
Uh let's go ahead and see if this works.
We're going to go back.
We're going to go to play mode.
And we were knocked back.
Very cool.
Right.
Now let's go back here and I'm going to show you something else.
We can check if this one is not null by just typing a question mark over here.
And then we do need this line of code just like that.
So this one basically, uh, very similar to the null check over here.
It's like asking player to knock back.
And if no one responds, then there is no knock back.
Let's go ahead and try this.
And boom knocked.
Very cool.
However, I do not recommend to use this one.
Let's just do it.
Uh.
Like so.
Okay.
Very well.
So that's all what we need here and now.
Every time we make a trap or an enemy, we can just make a trigger area on that trap and assign this
script together with the trigger.
And it's going to work just like that.
We're going to damage enemy with whatever we need.
As of now, I'm just going to delete this trigger and I want to do a bit of a clean up.
So let's make a folder.
You're going to call it scripts, and we're going to take all of the scripts we have and drag them inside
of this folder, because if we don't manage everything from the start, it can make quite a mess in
this window.
Also, for the material, we don't really have any other materials in the game, but still I'm going
to make a folder, I'm going to call it material and I'm going to drag it here.
Okay.
Very cool.
Let's go to the next video.