Welcome back.
In this video we will have a look at is trigger.
And you have seen physical bodies already.
You have seen colliders, all those kind of things.
Now let's add a new component, which is a trigger and it's actually not even a new component.
And that's the beauty, because it is simply the collider itself.
So you can make a collider is trigger.
For example, our player could be a trigger or a new object.
So let's create a new object into our game, a 3D object.
Let's go with a cube.
And what I will do with the cube, I will change the position to reset.
So I'm going to reset it and I'm going to drag it a little bit up and to the side.
Now let's change the scale of Y to, let's say five and the scale of X two.
Let's go with three.
So now we will have a wall here and maybe it's better if I move the wall to the side and change the
X to one and to Z two.
Let's go with four.
So now I have this huge ball here and what I want to happen is if I walk through or if the ball passes
into this cube, I want something to be triggered.
So I want something to happen.
For example, I want the whole thing to disappear.
So let's say I want the wall to disappear, which is this cube.
So let's give it a is trigger.
The cube itself gets an is trigger.
Let's have a look.
What that does is it deactivates its collider in terms of it's not a wall anymore so it's not a wall
that we cannot pass through anymore.
So now as you can see, our ball simply ignores it.
It just walks through.
So if we want this wall to do anything, whenever something passes it or hits it, then we need to create
a new script.
So I'm just going to call the script trigger script.
So let's go ahead and create a new script, right click create and then see sharp script and call it
trigger script.
Now I'm going to assign that trigger script to my cube.
So let's go ahead and add the component scripts and trigger script.
And now let's open trigger script up.
And there is only one thing that I need in here, so I won't need to start an update method.
And again, it's not that important that you understand the code in itself, but it's more important
that you understand the principles.
So whenever something needs to happen, when our object is hit by another object, we need to use the
on trigger enter method.
There's a method called on trigger.
Enter is called when the collider other enters the trigger and other could be our player for example.
So let's do that.
I just pressed enter.
I chose the on trigger entry method and now it's created for me automatically here.
So what I can enter here is, for example, print triggered.
So I'm just gonna print something onto the console and you will see what that does in a second.
So let's do that.
So now our cube, it's really important that our cube has this trigger script.
Now we run the code.
And let's pass through our trigger.
And as you can see on the console here, it says Trigger.
So now let's trigger once again.
Let's try to get the ball back.
There we are and let's hit it again.
You can see it's called three.
It was called three times already, and that's because we passed through it three times.
What you could use that for is, for example, to get rid of this whole game object.
So let's do that.
Let's go back into the code and we call Trigger now and we also call the destroy method.
And I'm just going to give game object.
So what that will do and I'm just going to use game object as the parameter, what the parameter is,
what the destroy method is.
If you don't know those things yet, we will get into that later.
So the destroy method, what it will do, it will simply destroy the game object that contains this
trigger script.
So that's what this line here says.
So let's have a look how this looks in game.
So I'm going to simply trigger this event.
And you see, our wall has disappeared.
So if we set up our game slightly differently, so I'm just going to go ahead and position my ball at
the bottom straight away and move it to the left.
And now I'm going to move my cube a little bit and I'm going to duplicate that cube multiple times,
and now I move the cubes one after the other.
So let's just move them.
And this one as well.
And the last one as well.
So now if I start it off, what will happen is that one after the other will disappear that we are.
I simply duplicated them with using control dx or command d based on which system you are using.
So right click duplicate is another option.
So if you duplicate it this way, then the object will be duplicated at the same position.
So that's why you cannot see that there are two objects.
So you need to move one in order to see that there are two objects, in fact.
So let's go ahead and do that again.
And now, as you can see, the destroy method is called.
Now, what we could do is deactivate.
That is trigger onto any of our objects or all of them are objects, at least of our cube objects.
So let's do that real quick, except for the last two.
And now what I want to add as well is a rigid body to all of them, or except while the first ones at
least.
So let's add a physics rigid body to cube cube.
One, two, three.
And that's it.
So physics.
Granted, Buddy, and add physics.
It's important that you use physics and not physics to DX because otherwise it won't work.
Now let's run it again.
And now let's try to hit it with our ball.
And as you can see, the first one still had a trigger.
Now, boom, boom.
And those two, they just fall off.
So the ones that had a trigger, they disappeared and the ones that didn't have a trigger.
So for five.
And I think that's it.
They had a trigger still, so they disappeared.
But the other ones didn't disappear.
Because they didn't have the trigger method activated and that's why they weren't destroyed.
So now let's get rid of the trigger method from each of them.
And what that will allow us to do is to have a little domino game.
So let's go ahead and hit them off.
Maybe we should make our player a bit bigger.
I'm going to pause the game again.
Go ahead and change the radius.
Or actually the scale of our player to, let's say three times three times three.
And let's drag it up a little bit.
And now what we could do is.
Go to here, you can use global or local.
So now we are global and now I can move your camera towards the front and move it up a little bit.
So now you have a better angle at things.
So let's start it again.
And now let's move it.
And as you can see, not all of them are moving.
Why is that one not moving?
Maybe you can figure it out.
As you can see now, I can move it just at the edge of it.
Maybe you know why it's not moving.
I hope you know it.
It is because it doesn't have a rigid body.
So Q.
Q one, two and three have a rigid body, but they are not here in the order of their naming, so.
Q One is the first one.
The cube is the right one.
Two is this one and three is that one.
So the fourth is the one to the right and the fifth is this one.
And those two, they don't have a physics body.
That's why they cannot be influenced by physics.
So let's change that.
Let's give them a physics body as well.
Physics and rigid body.
Now, let's go ahead and play that again.
Or maybe let's even get rid of this central one.
So let's delete this that cube here, delete and let's run it again.
And now let's hit it.
And as you can see, we have this little domino game here.
And let's try to get on top of it.
Yeah.
Perfect.
Our ball can with its force, it can move on top of it.
It can move around those objects.
And now you could go ahead and play, create your own little game, a little domino game or a little
collection game where you can collect game objects and make them disappear with the destroy method.
All right.
So that's it for now.
Regarding Collider's rigid body and its trigger.
We will use those extensively in our games.
So no worries if you don't really get a grasp of it completely yet.
But we will get there and you will see it within real games.
So see you in the next video.