In this lecture we're looking at triggers and using on trigger enter so that when the player crosses
over a trigger volume, it will print something to the console.
Let's get started.
Okay, let's start this process by adding another game object that we can use as our trigger.
So I want you to pause a video and add a box or something rectangular that we can use as a trigger.
Okay.
So in the hierarchy, right click to the object physics and we will add a square and square.
We will move a little bit above or I'll move a little bit above and I'm going to stretch it a little
bit as well.
Just hit R and stretch it a little bit that way, change the color so we differentiate it.
So clicking on my inspector, you can see that I've closed some things in here so it remembers.
I wanted it closed.
I'm going to open that up again.
The Sprite renderer, click on the white area.
Change this to a pink I think for our trigger.
What do you think the next step in the process is?
What do we need to add to this thing to know when we've passed through it, over it, around it, we
need to add a collider.
What sort of collider need to add a box collider to DH?
That box collider to DS, you can see, has taken the exact shape of our object, which is great.
Now let's see what happens when we drive our vehicle towards that new thing we've added.
If I can drive correctly and we drive into it bump, okay.
It doesn't move because there's no rigid body on it.
That's fine.
We don't need it to move, but we can bump into it.
But what if I want to say this is like a start or finish line or this is some sort of pickup I want
to drive over or some sort of indicator that says that the player passed from one zone to another.
I don't want to bounce into it.
I want to drive through it, but I still want to know when we've driven through it.
There's one really simple thing if we've clicked on our square just here within our collider, you see
is trigger.
By clicking on that, we've now said that we want this collider to be a trigger sort of collider.
Normally I wouldn't call it a trigger collider, I just call it a trigger.
But you can see we've got a collider and we've said that collider is a trigger.
Now when I click on play, I'm going to drive my car and my car is going to pass right over or through
and not bumping into it, not getting stopped by it.
Now, why is it going underneath?
While we talked about this in the first section of the course, it's because of our layer order.
This square is at Layer Order zero and our player is also at Layer Order zero.
So it's a little bit random.
Which one is going to be on top?
Which one's going to be below?
I'd like the player to always be on top of these things, so I'm going to change the order in layer
for the player cruising drive to ten.
Now when we drive over this, it should drive and actually you'll see it driving over there we go over
the object, which is cool.
What's the next step?
Well, we need to jump into our script, into our collision CSS script, similarly to the way we said
on Collision Enter, we're going to use a new method which is on trigger enter 2D.
And I'm going to give this to you as a challenge to see how well you've been paying attention.
Print When we trigger something so you use on trigger enter to DD and debug dot log to print yet another
witty message to the console when we trigger something.
So there's is your challenge pause video.
Take that on, test that it works and I'll see you back here when you're done.
Right.
So in our code, I'm going to give myself a couple of new lines to give some space.
I will type in on trigger, enter, find the 2D version, and it adds in all this stuff as it did before.
I'm just going to remove private because that's what I like to do and tidy up my curly braces.
Now from within this area, I'm going to type debug log parentheses, quotation marks and I will say,
what was that?
Not very witty once again, but I've got ouch when I bump into it.
And what was that.
Because the player pass through something, save back over into unity, make sure we're on the console
so we can see what's going on there.
Click play and we'll drive over the pink area bump and it says, What was that the moment I pass into
it?
So you can see right about now it increases my number in terms of my count bump just about there and
it only does it when we first touch it the whole way, passing through.
Nothing, nothing, nothing, nothing, nothing.
And we leave.
There's another method which is on trigger exit.
If you want to know when someone leaves a particular area, you can use that method.
But I'm going to bump, touch it and then it will increase.
The other thing you could do is you could click on your square and turn off the sprite renderer so the
player doesn't even know there's a thing there.
I drive through it.
I know it's there.
You know it's there.
That's our little secret.
But see how my count is still increasing because I'm driving over that area.
So that's the way that we can also use a trigger volume in a way that the player doesn't even know.
So there's plenty of occasions in games where we want to say, Did the player reach this point or did
something go off screen?
If it did, then do something to it, or did I go into a particular zone so triggers a super useful.
And with that, my friend, you have the foundation of many games out there.
You now know to have how to have the player go through a trigger volume and to have something happen
in the code when that happens.
And also for the player to bump into something and to have something happen in the code when that happens.
So that really is the foundation of so many things that we do in our games, the triggers and colliders.
So that's all for this lecture.
I will see you in the next one.