Rodeo.
In this lecture, I'm going to be showing you a technique to stop our player being able to do this midair
jump and fly off onto the screen.
And the way we'll be doing that is using layers.
We've already had a look at layers a couple of times.
We've looked at sorting layers.
So let me click on the platform tile map here.
You can see under layer we've currently got default.
That means the collision layer.
I like to think of it as collision layer.
If we go and click on Add Layer, you can see that we've got sorting layers which we've played around
with.
That's the visibility, what will be displayed first and displayed seconds so that our platforms are
on top of the background.
We've also got these other layers in here.
The way we mostly use these is when we're looking at collision, but there's a few other usages.
For example, layers are useful if you have the same functionality across multiple game objects.
What do I mean by that?
Well, if you want a whole bunch of things ignored by the camera or a whole bunch of things to be not
clickable or a whole bunch of things like in our case, to be doing a collision check.
And if we jump over to the main camera over here, you can see that the main camera uses this sort of
collision for the culling mask.
What's a culling mask?
That's a way of saying what can you see or not see?
The mask is where we group a bunch of things together.
So if you see my little main camera view down here, our preview, you can see that we can see all our
level and stuff.
If I was to go into the culling mask and select on nothing, we'd see nothing, just the solid background.
That is because everything has been culled by the camera.
What we'll do, we'll look back on everything here.
And so we could set up a new layer that said Only show through the camera only shows certain things.
I only show, say the ground or the green things or the enemies or whatever.
You can turn things on and off in the camera using this culling mask for us.
We want a ground layer.
So I'm going to click back on my platform's tile map just so I'm in the right ballpark, click on layer,
add a layer, and then use a layer number six or wherever.
It doesn't matter what number you attach this to, I'll create a ground with an uppercase G to keep
that convention.
So now that we've created ground, click back on the platform's tile map again and change the layer
to be the ground layer.
One thing we do need to have a quick look at here, if you go to edit project settings and find physics
to scroll down, you can see there's a little matrix down here which says which things will collide
with which other things.
And you can see that we've got ground and that is lining up with default, our etc. We've got a few
default kind of things in here.
So if you go to uncheck something in here that would say this thing can't collide with that thing.
So if we had another layer in here which was, for example, enemies, and if we unchecked the relationship
between enemies and ground, then there's a chance the enemies would just drop through the ground and
not actually collide.
So if sometimes if you get a little bit of issue and you're like, why are things not bumping into each
other?
You might just want to come in and check that matrix, that collision matrix, just to make sure things
are checked appropriately for us.
For now, we're just going to have everything colliding because that's the way it's going to work in
our game.
But you could absolutely have something in your game where you say, Well, I want the player to be
able to run straight through something and not bump into them, whereas I want the enemies to bump into
those things so you can play around with your collision matrix that way using the layers.
What are we going to do?
Well, we're going to stop the player jumping any time they please that we're going to say only jump
when you're touching the ground layer using Collider 2d dot is touching layers.
Let's have a quick look at a couple of Unity docs.
Collider 2D is touching layers.
This is going to return a ball, which will be true or false depending upon whether or not this collider.
So whether wherever we place this in our case, it's going to be on the player, whether the player's
collider is touching any collider on the specified layer mask or not.
So in other words, is it touching any of the colliders, all these many colliders we might happen to
have placed on the ground layer?
And the other doc is layer mask, target mask.
And you can see here it returns the layer mask created from the layer names.
So we need to know that we're dealing with the ground layer and the ground layer is going to generate
a mask and we need to have a way of getting that mask.
And so this all leads to a challenge where I'd like you to finish the logic to stop our player multi
jumping.
I've given you a lot of clues, but not the exact exact recipe for doing this.
You will need to use the collider.
2D is touching layers and you will also need to use the layer masking mask.
You'll need to make sure that you're accessing the player's collider and also you'll need an if statement
in there somewhere.
So give that a go.
Pause a video, take on that challenge.
See you back here when you're done.
First thing I'll do is to set up a reference to our collider.
Now our particular collider on the player is a capsule collider.
So I'll specifically say, let's find the capsule.
Collider Capsule.
Collider 2D is going to be the type and then we'll call this my capsule collider semicolon.
And then in start we need to get our component.
So my capsule collider equals get component.
What's the type?
It's going to be the capsule collider to dx parentheses semicolon.
So we're looking at on jump and in here we've got some functionality.
If you press the button, then you can jump.
We could add in a you know, and add some more criteria.
Or we could wrap all of this in a if statement that says, if you are not touching, then do these things
or we can do it another way, which says if you're not touching the ground, then don't progress to
any of this, just return.
Don't do any of this stuff.
So the way we're going to write that, to say, if I'm not doing a particular thing, what am I not
doing?
Well, if my capsule collider dot is touching layers and then which layers are we interested in?
Well, this is where we need to use our layer mask, get layer masks, get mask.
And then the particular layer we're interested in is ground within uppercase G.
We remember that if you're not touching, then we could write it like this.
Just simply return.
That means get out of this method and don't go any further.
I'm just going to tidy this up so it's a little bit easier to to read quickly.
And I'll put all this on the one line so we can add return up here.
Return.
There we go.
All on the one line.
Let's save head back to unity, make sure that we have all the things hooked up.
So the platform.
Yep, that does say ground.
That's cool.
The player does have their capsule collider we'll click on play.
We've got an error that's going to be Rick doing something dumb.
And you know what?
After pausing the video and messing around here for a while, I've discovered that I spelt something
incorrectly.
I need to spell is touching layers with an uppercase.
I then saving jump back over into unity.
My error should go away with a different sort of warning that says Rick.
Make sure you check your spelling done to head click on play.
Now let's see if our functionality is as we want it.
I can jump jump but I can't jump over and over.
Excellent.
Despite tapping the spacebar a whole bunch.
So there we go.
We've stopped our player doing the extra jumps.
You can only now jump when you are touching the ground.
Okay, I'll see in the next lecture.