1
00:00:03,210 --> 00:00:07,200
In this lecture, we're adding collision to our world and collision to our characters so that we can

2
00:00:07,200 --> 00:00:11,280
drop to the ground using gravity and then stand there and not fall over.

3
00:00:11,280 --> 00:00:13,130
So let's jump in and get started.

4
00:00:13,140 --> 00:00:17,520
I think it's time for us to pre-fab our players, so I'm going to jump in my assets directory, right

5
00:00:17,520 --> 00:00:21,850
click, create a folder and we will name this prefabs.

6
00:00:21,930 --> 00:00:25,830
It's good to have all these things as prefabs because if we want the player to go from level to level,

7
00:00:25,830 --> 00:00:28,260
we need to have one player prefab that we're using.

8
00:00:28,260 --> 00:00:33,630
So I'm going to open up prefabs folder, grab my player, just drop the player down here makes a prefab.

9
00:00:33,630 --> 00:00:34,170
Excellent.

10
00:00:34,170 --> 00:00:37,650
Now because we're going to be making some changes to the player as well in this video.

11
00:00:37,650 --> 00:00:41,970
So make sure you pay attention to whether you're working on the prefab or working on the instance or

12
00:00:41,970 --> 00:00:42,930
what you're working on.

13
00:00:42,930 --> 00:00:44,370
Just a note of that.

14
00:00:44,370 --> 00:00:46,620
Now let's turn our attention to the tile map itself.

15
00:00:46,620 --> 00:00:47,940
We want to add some collision on.

16
00:00:47,940 --> 00:00:49,470
There's a couple of nifty things we can do.

17
00:00:49,470 --> 00:00:54,840
But first of all, just a note that we can apply the collision to any of our particular tile maps.

18
00:00:54,840 --> 00:00:59,430
So eventually that three tile maps in our scene at the moment and see we can change it by either selecting

19
00:00:59,430 --> 00:01:03,420
along here or we can click on the object in the hierarchy itself.

20
00:01:03,420 --> 00:01:05,970
So we want to be putting the collision on platforms.

21
00:01:05,970 --> 00:01:10,800
So when the player is touching these platforms, the player lands on them and can jump on them, etc.

22
00:01:10,830 --> 00:01:12,960
Not on the background and not on the water.

23
00:01:13,170 --> 00:01:14,040
Maybe the water bit.

24
00:01:14,040 --> 00:01:17,370
Probably not for the moment, but definitely on the platform tile map.

25
00:01:17,370 --> 00:01:21,630
So while we've got that clicked in our inspector, I'm going to scroll down ad component.

26
00:01:21,630 --> 00:01:28,680
I'm going to start by adding a tile map collider to DX and that you see will add collision into all

27
00:01:28,680 --> 00:01:31,920
of our blocks, all of our tiles that we've placed in the scene.

28
00:01:31,920 --> 00:01:32,670
That's a good start.

29
00:01:32,670 --> 00:01:33,960
We could potentially finish at this.

30
00:01:33,960 --> 00:01:37,680
But there's one issue that I've found is if you scroll all the way in, all the way in, all the way

31
00:01:37,680 --> 00:01:40,770
in, we'll see how far it's actually going to let me scroll in here all the way in.

32
00:01:41,340 --> 00:01:45,990
You can see that there's a gap where you can't see, but there is a gap between this tile on the left

33
00:01:45,990 --> 00:01:46,770
and this tile on the right.

34
00:01:46,770 --> 00:01:50,510
The collision is this tiny little gap in there, and sometimes we trip on it.

35
00:01:50,520 --> 00:01:53,790
We can now foot stuck in it and things don't go so well.

36
00:01:53,790 --> 00:01:58,950
But there's a cool thing that tile map lets us do if we jump down into add component again, if we search

37
00:01:58,950 --> 00:02:00,570
for composite.

38
00:02:00,570 --> 00:02:02,370
Collider 2D, composite.

39
00:02:02,370 --> 00:02:06,060
Collider 2D, that allows us to join all these things together.

40
00:02:06,060 --> 00:02:09,900
So when we added the composite collider component, it's also added a rigid body.

41
00:02:09,900 --> 00:02:13,560
All we need to do here is set this to static because our world is not going to move.

42
00:02:13,560 --> 00:02:16,140
It's going to stay where it is, the platforms in the future.

43
00:02:16,140 --> 00:02:19,110
We might do some moving platforms, in which case we wouldn't do it as static.

44
00:02:19,110 --> 00:02:23,310
But for now all we're doing is just having it sitting there stationary, make it static, and then it's

45
00:02:23,320 --> 00:02:26,670
going to roll up rigid body to DX so it's not getting in the way under tile map.

46
00:02:26,670 --> 00:02:29,910
Collider 2D You can see that we've got a used by a composite.

47
00:02:29,910 --> 00:02:35,820
If we click on that, then our entire world is wrapped, our tile map is wrapped with our collision.

48
00:02:35,820 --> 00:02:41,310
I'm just going to turn off for a moment the tile renderer, just so you can see, we've got this green

49
00:02:41,310 --> 00:02:45,150
outline now that is our collision that goes around all of our tiles.

50
00:02:45,150 --> 00:02:51,600
And if we're to get there and paint using our rule tile, paint a new tile just in here, then you can

51
00:02:51,600 --> 00:02:53,820
see once again, I'll just hide my visualization.

52
00:02:53,820 --> 00:02:58,740
You can see that our collision responded to that and wrapped around in a really nice, dynamic way.

53
00:02:58,740 --> 00:02:59,640
Super handy.

54
00:02:59,640 --> 00:03:01,170
It couldn't be any easier than that, huh?

55
00:03:01,260 --> 00:03:02,610
Just very straightforward.

56
00:03:02,610 --> 00:03:06,900
Now, what we need to do is make it so that our player can actually go bump and fall and land.

57
00:03:06,900 --> 00:03:11,910
Because when we kick click play, he's just standing there doing not much, just floating in space.

58
00:03:11,910 --> 00:03:13,800
So I'm going to give this to you as a challenge.

59
00:03:13,800 --> 00:03:18,330
And that challenge is to make your player fall and land that make this even clearer.

60
00:03:18,330 --> 00:03:21,660
I'm going to grab the player, hit W and just put the player up in the world.

61
00:03:21,660 --> 00:03:25,620
So let's go dunk and then land on the platform, make your player fall in land.

62
00:03:25,620 --> 00:03:28,260
So figure out what components we need to add to the player.

63
00:03:28,260 --> 00:03:30,930
So when we push play, he falls and lands on the ground.

64
00:03:30,930 --> 00:03:32,580
And now a little bit of bonus points here.

65
00:03:32,580 --> 00:03:38,400
Figure out how we can ensure the player doesn't fall over, doesn't kind of slowly rotate and go up

66
00:03:38,400 --> 00:03:42,090
and land on his face, which can happen if we do things a particular way.

67
00:03:42,090 --> 00:03:42,870
So there's a challenge.

68
00:03:42,870 --> 00:03:43,650
Pause video.

69
00:03:43,680 --> 00:03:44,640
Take that one on.

70
00:03:47,180 --> 00:03:47,690
Okay.

71
00:03:47,690 --> 00:03:51,620
So there was a little bit of a clue in the sort of collider I'm going to be using on my player because

72
00:03:51,620 --> 00:03:54,170
I was talking about having it fall over.

73
00:03:54,170 --> 00:03:57,470
So I'm going to work directly on the prefab click my little arrow here.

74
00:03:57,470 --> 00:04:04,070
Now I'm in the prefab, I'm going to click on Add Component and we'll add a capsule collider make sure

75
00:04:04,070 --> 00:04:04,670
it's the two.

76
00:04:04,670 --> 00:04:07,670
RD So you don't sit there for a while saying, why isn't this working?

77
00:04:07,670 --> 00:04:08,960
Or do a little bit of editing on this?

78
00:04:08,960 --> 00:04:12,620
And it's a big round ball that's a little bit fatter than what Ginger is.

79
00:04:12,620 --> 00:04:17,510
I'm going to click on Edit Collider, grab the handles to the right and to the left and up and down.

80
00:04:17,510 --> 00:04:20,600
Just a little bit of finessing to cover most of the player, right?

81
00:04:20,630 --> 00:04:21,140
Happy with that?

82
00:04:21,140 --> 00:04:21,680
That's good.

83
00:04:21,680 --> 00:04:22,910
And click out of the edit.

84
00:04:22,910 --> 00:04:28,790
Collider Next, what we need to do to have our player actually respond to gravity and physics is what

85
00:04:28,790 --> 00:04:29,240
we need.

86
00:04:29,270 --> 00:04:34,130
Add a rigid body, so rigid body to DH and our rigid body to DX.

87
00:04:34,130 --> 00:04:35,030
Let's open that up.

88
00:04:35,030 --> 00:04:35,630
Have a look at it.

89
00:04:35,630 --> 00:04:36,230
Dynamic.

90
00:04:36,230 --> 00:04:40,520
Yes, because we want him to be moving around unlike our world, which is going to be static.

91
00:04:40,520 --> 00:04:45,290
And I'm going to find the constraints box in here because we've got a capsule collider and we've got

92
00:04:45,290 --> 00:04:46,370
this rounded bottom.

93
00:04:46,370 --> 00:04:51,320
There's the possibility that our player will slowly kind of go boom and roll onto his face because he's

94
00:04:51,320 --> 00:04:56,810
standing on this rounded edge, the rounded edge I like better than having the square collider or the

95
00:04:56,810 --> 00:05:00,620
rectangular collider because it doesn't have these sharp edges that can catch on things.

96
00:05:00,620 --> 00:05:04,340
It's got this nice rounded edge, smooth edge makes it easier to bounce around and do stuff.

97
00:05:04,340 --> 00:05:09,770
But I need to freeze the rotation on the Z axis so we can't accidentally roll forward onto our face.

98
00:05:09,770 --> 00:05:11,330
So I think that's all I need to do.

99
00:05:11,330 --> 00:05:17,500
Let's move back out of our player prefab click on play and our play should drop to the ground and they

100
00:05:17,530 --> 00:05:20,840
bump and land nicely and not roll over fall flat on his face.

101
00:05:20,840 --> 00:05:21,530
So there we go.

102
00:05:21,530 --> 00:05:25,580
Next stage in getting our player ready for running around and jumping and platforming.

103
00:05:25,580 --> 00:05:26,030
Good stuff.

104
00:05:26,030 --> 00:05:27,200
I'll see you in the next lecture.

