1
00:00:03,140 --> 00:00:03,770
Rodeo.

2
00:00:03,770 --> 00:00:08,150
In this lecture, I'm going to be showing you a technique to stop our player being able to do this midair

3
00:00:08,150 --> 00:00:10,250
jump and fly off onto the screen.

4
00:00:10,250 --> 00:00:13,070
And the way we'll be doing that is using layers.

5
00:00:13,070 --> 00:00:15,680
We've already had a look at layers a couple of times.

6
00:00:15,680 --> 00:00:17,390
We've looked at sorting layers.

7
00:00:17,390 --> 00:00:20,420
So let me click on the platform tile map here.

8
00:00:20,450 --> 00:00:22,700
You can see under layer we've currently got default.

9
00:00:22,700 --> 00:00:24,830
That means the collision layer.

10
00:00:24,830 --> 00:00:26,330
I like to think of it as collision layer.

11
00:00:26,330 --> 00:00:31,010
If we go and click on Add Layer, you can see that we've got sorting layers which we've played around

12
00:00:31,010 --> 00:00:31,160
with.

13
00:00:31,160 --> 00:00:36,410
That's the visibility, what will be displayed first and displayed seconds so that our platforms are

14
00:00:36,410 --> 00:00:37,490
on top of the background.

15
00:00:37,490 --> 00:00:39,710
We've also got these other layers in here.

16
00:00:39,710 --> 00:00:44,120
The way we mostly use these is when we're looking at collision, but there's a few other usages.

17
00:00:44,120 --> 00:00:50,180
For example, layers are useful if you have the same functionality across multiple game objects.

18
00:00:50,180 --> 00:00:50,990
What do I mean by that?

19
00:00:50,990 --> 00:00:55,670
Well, if you want a whole bunch of things ignored by the camera or a whole bunch of things to be not

20
00:00:55,670 --> 00:01:00,290
clickable or a whole bunch of things like in our case, to be doing a collision check.

21
00:01:00,290 --> 00:01:05,690
And if we jump over to the main camera over here, you can see that the main camera uses this sort of

22
00:01:05,690 --> 00:01:07,670
collision for the culling mask.

23
00:01:07,670 --> 00:01:08,620
What's a culling mask?

24
00:01:08,690 --> 00:01:11,570
That's a way of saying what can you see or not see?

25
00:01:11,570 --> 00:01:14,810
The mask is where we group a bunch of things together.

26
00:01:14,810 --> 00:01:19,460
So if you see my little main camera view down here, our preview, you can see that we can see all our

27
00:01:19,460 --> 00:01:20,570
level and stuff.

28
00:01:20,570 --> 00:01:25,490
If I was to go into the culling mask and select on nothing, we'd see nothing, just the solid background.

29
00:01:25,490 --> 00:01:28,340
That is because everything has been culled by the camera.

30
00:01:28,340 --> 00:01:30,560
What we'll do, we'll look back on everything here.

31
00:01:30,560 --> 00:01:35,810
And so we could set up a new layer that said Only show through the camera only shows certain things.

32
00:01:35,810 --> 00:01:39,860
I only show, say the ground or the green things or the enemies or whatever.

33
00:01:39,860 --> 00:01:43,370
You can turn things on and off in the camera using this culling mask for us.

34
00:01:43,370 --> 00:01:44,720
We want a ground layer.

35
00:01:44,720 --> 00:01:49,010
So I'm going to click back on my platform's tile map just so I'm in the right ballpark, click on layer,

36
00:01:49,010 --> 00:01:52,760
add a layer, and then use a layer number six or wherever.

37
00:01:52,760 --> 00:01:59,030
It doesn't matter what number you attach this to, I'll create a ground with an uppercase G to keep

38
00:01:59,030 --> 00:01:59,840
that convention.

39
00:01:59,840 --> 00:02:04,460
So now that we've created ground, click back on the platform's tile map again and change the layer

40
00:02:04,460 --> 00:02:05,980
to be the ground layer.

41
00:02:05,990 --> 00:02:11,510
One thing we do need to have a quick look at here, if you go to edit project settings and find physics

42
00:02:11,510 --> 00:02:16,850
to scroll down, you can see there's a little matrix down here which says which things will collide

43
00:02:16,850 --> 00:02:18,350
with which other things.

44
00:02:18,350 --> 00:02:24,740
And you can see that we've got ground and that is lining up with default, our etc. We've got a few

45
00:02:24,780 --> 00:02:26,180
default kind of things in here.

46
00:02:26,180 --> 00:02:30,320
So if you go to uncheck something in here that would say this thing can't collide with that thing.

47
00:02:30,320 --> 00:02:36,050
So if we had another layer in here which was, for example, enemies, and if we unchecked the relationship

48
00:02:36,050 --> 00:02:40,130
between enemies and ground, then there's a chance the enemies would just drop through the ground and

49
00:02:40,130 --> 00:02:41,090
not actually collide.

50
00:02:41,090 --> 00:02:45,500
So if sometimes if you get a little bit of issue and you're like, why are things not bumping into each

51
00:02:45,500 --> 00:02:45,830
other?

52
00:02:45,830 --> 00:02:50,660
You might just want to come in and check that matrix, that collision matrix, just to make sure things

53
00:02:50,660 --> 00:02:52,820
are checked appropriately for us.

54
00:02:52,820 --> 00:02:56,300
For now, we're just going to have everything colliding because that's the way it's going to work in

55
00:02:56,300 --> 00:02:56,870
our game.

56
00:02:56,870 --> 00:03:00,050
But you could absolutely have something in your game where you say, Well, I want the player to be

57
00:03:00,050 --> 00:03:04,970
able to run straight through something and not bump into them, whereas I want the enemies to bump into

58
00:03:04,970 --> 00:03:09,290
those things so you can play around with your collision matrix that way using the layers.

59
00:03:09,290 --> 00:03:10,070
What are we going to do?

60
00:03:10,070 --> 00:03:15,260
Well, we're going to stop the player jumping any time they please that we're going to say only jump

61
00:03:15,260 --> 00:03:21,560
when you're touching the ground layer using Collider 2d dot is touching layers.

62
00:03:21,590 --> 00:03:23,540
Let's have a quick look at a couple of Unity docs.

63
00:03:23,540 --> 00:03:25,460
Collider 2D is touching layers.

64
00:03:25,460 --> 00:03:32,060
This is going to return a ball, which will be true or false depending upon whether or not this collider.

65
00:03:32,060 --> 00:03:36,260
So whether wherever we place this in our case, it's going to be on the player, whether the player's

66
00:03:36,260 --> 00:03:40,430
collider is touching any collider on the specified layer mask or not.

67
00:03:40,430 --> 00:03:44,600
So in other words, is it touching any of the colliders, all these many colliders we might happen to

68
00:03:44,600 --> 00:03:46,190
have placed on the ground layer?

69
00:03:46,190 --> 00:03:48,650
And the other doc is layer mask, target mask.

70
00:03:48,650 --> 00:03:53,540
And you can see here it returns the layer mask created from the layer names.

71
00:03:53,540 --> 00:03:58,850
So we need to know that we're dealing with the ground layer and the ground layer is going to generate

72
00:03:58,850 --> 00:04:01,550
a mask and we need to have a way of getting that mask.

73
00:04:01,550 --> 00:04:05,900
And so this all leads to a challenge where I'd like you to finish the logic to stop our player multi

74
00:04:05,900 --> 00:04:06,320
jumping.

75
00:04:06,320 --> 00:04:10,760
I've given you a lot of clues, but not the exact exact recipe for doing this.

76
00:04:10,760 --> 00:04:12,740
You will need to use the collider.

77
00:04:12,740 --> 00:04:17,750
2D is touching layers and you will also need to use the layer masking mask.

78
00:04:17,750 --> 00:04:22,730
You'll need to make sure that you're accessing the player's collider and also you'll need an if statement

79
00:04:22,730 --> 00:04:23,540
in there somewhere.

80
00:04:23,540 --> 00:04:24,800
So give that a go.

81
00:04:24,800 --> 00:04:26,360
Pause a video, take on that challenge.

82
00:04:26,360 --> 00:04:27,560
See you back here when you're done.

83
00:04:29,260 --> 00:04:32,890
First thing I'll do is to set up a reference to our collider.

84
00:04:32,890 --> 00:04:36,400
Now our particular collider on the player is a capsule collider.

85
00:04:36,400 --> 00:04:38,740
So I'll specifically say, let's find the capsule.

86
00:04:38,740 --> 00:04:40,840
Collider Capsule.

87
00:04:40,840 --> 00:04:47,470
Collider 2D is going to be the type and then we'll call this my capsule collider semicolon.

88
00:04:47,470 --> 00:04:50,020
And then in start we need to get our component.

89
00:04:50,030 --> 00:04:53,590
So my capsule collider equals get component.

90
00:04:53,590 --> 00:04:54,520
What's the type?

91
00:04:54,520 --> 00:04:59,440
It's going to be the capsule collider to dx parentheses semicolon.

92
00:04:59,440 --> 00:05:02,140
So we're looking at on jump and in here we've got some functionality.

93
00:05:02,140 --> 00:05:04,000
If you press the button, then you can jump.

94
00:05:04,000 --> 00:05:08,170
We could add in a you know, and add some more criteria.

95
00:05:08,170 --> 00:05:16,480
Or we could wrap all of this in a if statement that says, if you are not touching, then do these things

96
00:05:16,480 --> 00:05:21,940
or we can do it another way, which says if you're not touching the ground, then don't progress to

97
00:05:21,940 --> 00:05:23,200
any of this, just return.

98
00:05:23,200 --> 00:05:24,250
Don't do any of this stuff.

99
00:05:24,250 --> 00:05:28,330
So the way we're going to write that, to say, if I'm not doing a particular thing, what am I not

100
00:05:28,330 --> 00:05:28,630
doing?

101
00:05:28,630 --> 00:05:34,780
Well, if my capsule collider dot is touching layers and then which layers are we interested in?

102
00:05:34,780 --> 00:05:39,850
Well, this is where we need to use our layer mask, get layer masks, get mask.

103
00:05:39,850 --> 00:05:44,620
And then the particular layer we're interested in is ground within uppercase G.

104
00:05:44,620 --> 00:05:49,540
We remember that if you're not touching, then we could write it like this.

105
00:05:49,540 --> 00:05:50,890
Just simply return.

106
00:05:50,890 --> 00:05:54,160
That means get out of this method and don't go any further.

107
00:05:54,160 --> 00:05:58,180
I'm just going to tidy this up so it's a little bit easier to to read quickly.

108
00:05:58,180 --> 00:06:02,170
And I'll put all this on the one line so we can add return up here.

109
00:06:02,350 --> 00:06:03,250
Return.

110
00:06:03,670 --> 00:06:04,240
There we go.

111
00:06:04,240 --> 00:06:05,140
All on the one line.

112
00:06:05,140 --> 00:06:10,210
Let's save head back to unity, make sure that we have all the things hooked up.

113
00:06:10,210 --> 00:06:11,410
So the platform.

114
00:06:11,410 --> 00:06:12,760
Yep, that does say ground.

115
00:06:12,760 --> 00:06:13,240
That's cool.

116
00:06:13,240 --> 00:06:17,050
The player does have their capsule collider we'll click on play.

117
00:06:17,080 --> 00:06:19,780
We've got an error that's going to be Rick doing something dumb.

118
00:06:19,780 --> 00:06:20,800
And you know what?

119
00:06:20,800 --> 00:06:26,290
After pausing the video and messing around here for a while, I've discovered that I spelt something

120
00:06:26,290 --> 00:06:27,310
incorrectly.

121
00:06:27,310 --> 00:06:30,970
I need to spell is touching layers with an uppercase.

122
00:06:30,970 --> 00:06:34,180
I then saving jump back over into unity.

123
00:06:34,180 --> 00:06:38,170
My error should go away with a different sort of warning that says Rick.

124
00:06:38,170 --> 00:06:42,370
Make sure you check your spelling done to head click on play.

125
00:06:42,880 --> 00:06:45,250
Now let's see if our functionality is as we want it.

126
00:06:45,250 --> 00:06:48,790
I can jump jump but I can't jump over and over.

127
00:06:48,790 --> 00:06:49,390
Excellent.

128
00:06:49,390 --> 00:06:51,640
Despite tapping the spacebar a whole bunch.

129
00:06:51,640 --> 00:06:52,270
So there we go.

130
00:06:52,270 --> 00:06:54,430
We've stopped our player doing the extra jumps.

131
00:06:54,430 --> 00:06:57,430
You can only now jump when you are touching the ground.

132
00:06:57,430 --> 00:06:58,810
Okay, I'll see in the next lecture.

