1
00:00:00,000 --> 00:00:04,080
Welcome back, my fellow game developers.

2
00:00:04,080 --> 00:00:08,240
In this video, we are taking our game to the next level.

3
00:00:08,235 --> 00:00:10,235
When we run our game.

4
00:00:10,230 --> 00:00:13,470
You'll notice that not only we walk around,

5
00:00:13,470 --> 00:00:15,530
but if we try to leave our map,

6
00:00:15,525 --> 00:00:17,145
there we go. We cannot.

7
00:00:17,145 --> 00:00:19,835
I swear I'm still pressing the achy.

8
00:00:19,830 --> 00:00:23,040
I'm still trying to go down, but it doesn't work.

9
00:00:23,040 --> 00:00:24,540
I'm still trying to go out.

10
00:00:24,540 --> 00:00:28,860
It doesn't work. So we are confined in this little space.

11
00:00:28,855 --> 00:00:32,785
We do that using rigid bodies, using colliders.

12
00:00:32,780 --> 00:00:37,670
And this is really nice because finally we can feel the weight of our game.

13
00:00:37,670 --> 00:00:39,140
So even though we could walk,

14
00:00:39,140 --> 00:00:40,790
we could see we can do anything,

15
00:00:40,790 --> 00:00:43,610
but now we actually have weight to our movement.

16
00:00:43,610 --> 00:00:46,330
We can feel the world around us.

17
00:00:46,325 --> 00:00:48,955
With that said, let's not waste any more time.

18
00:00:48,950 --> 00:00:51,420
And let's get started.

19
00:00:51,424 --> 00:00:53,214
O k.

20
00:00:53,210 --> 00:00:58,760
So currently, when we run our game,

21
00:00:58,760 --> 00:01:01,240
we can move and all that's great.

22
00:01:01,235 --> 00:01:04,915
But as you can see, when we try to hit the wall or something,

23
00:01:04,910 --> 00:01:06,470
we just move bastard.

24
00:01:06,470 --> 00:01:08,990
And it's as if there is nothing in this world.

25
00:01:08,990 --> 00:01:14,680
It feels empty, it feels not real even though we are moving in it.

26
00:01:14,675 --> 00:01:17,495
So how are we going to fix that?

27
00:01:17,495 --> 00:01:20,905
We are going to use something called a collider.

28
00:01:20,900 --> 00:01:24,820
Let me show you what a collider is in the Unity documentation,

29
00:01:24,815 --> 00:01:28,655
as you can see, always referred in Unity documentation,

30
00:01:28,655 --> 00:01:30,385
you can get there through Google.

31
00:01:30,380 --> 00:01:32,540
It's super easy, super effective.

32
00:01:32,539 --> 00:01:36,459
Let's see what we have description based class of colliders.

33
00:01:36,455 --> 00:01:39,055
We have box, sphere, capsule,

34
00:01:39,050 --> 00:01:42,460
a mesh collider, physical materials and rigid bodies.

35
00:01:42,455 --> 00:01:45,145
Something that we will learn in just a bit of

36
00:01:45,140 --> 00:01:47,780
an object with a collider needs to be moved.

37
00:01:47,780 --> 00:01:51,170
During gameplay, you can attach a rigid body component

38
00:01:51,170 --> 00:01:55,580
and we'll just get the rigid body a bit of patients,

39
00:01:55,580 --> 00:01:58,300
you can see all the properties that we can have,

40
00:01:58,295 --> 00:02:01,325
the bounds, the contact offset, the material.

41
00:02:01,325 --> 00:02:05,575
We have public methods such as re-cast closest point.

42
00:02:05,570 --> 00:02:09,160
We have messages which are extremely important and we'll be using

43
00:02:09,155 --> 00:02:13,375
a lot of Trigger Enter on trigger exit on Trigger Stay.

44
00:02:13,370 --> 00:02:18,260
You can read the descriptions right here of what each method, what each property,

45
00:02:18,260 --> 00:02:21,290
what each message means,

46
00:02:21,290 --> 00:02:23,810
what it does, all the inheritance,

47
00:02:23,810 --> 00:02:26,920
what we can use, the compare tag will be very important.

48
00:02:26,915 --> 00:02:29,105
You can see that we have a lot.

49
00:02:29,105 --> 00:02:31,565
You don't have to understand every single one of them.

50
00:02:31,565 --> 00:02:34,865
It's just nice to know that you can,

51
00:02:34,865 --> 00:02:37,595
if you have something in mind to implement,

52
00:02:37,595 --> 00:02:41,425
you can always go into Unit documentations and see if what

53
00:02:41,420 --> 00:02:45,160
you're looking for is available right here.

54
00:02:45,155 --> 00:02:46,885
So with that said,

55
00:02:46,880 --> 00:02:50,020
let's go back in here and let's go ahead and add

56
00:02:50,015 --> 00:02:53,455
a box collider to one of these walls right here.

57
00:02:53,450 --> 00:02:55,150
Let's start with the bottom one.

58
00:02:55,145 --> 00:03:00,425
So the way we do this is we are going to right-click in here.

59
00:03:00,425 --> 00:03:02,135
Or let's go to the background.

60
00:03:02,135 --> 00:03:04,145
Let's add a component in here.

61
00:03:04,145 --> 00:03:07,435
We're going to make it a box collider 2D,

62
00:03:07,430 --> 00:03:11,180
because we are working into this box collider is 3D.

63
00:03:11,180 --> 00:03:12,970
This one is 2D.

64
00:03:12,965 --> 00:03:13,955
We click on it.

65
00:03:13,955 --> 00:03:17,305
You can see that it fills the entire room immediately,

66
00:03:17,300 --> 00:03:18,580
but we don't want that.

67
00:03:18,575 --> 00:03:20,795
We actually want to edit this,

68
00:03:20,790 --> 00:03:22,750
or we can click on the Edit right here.

69
00:03:22,745 --> 00:03:24,605
You can see we can make it a trigger.

70
00:03:24,605 --> 00:03:25,945
We can set an offset,

71
00:03:25,940 --> 00:03:27,280
we can set the size,

72
00:03:27,275 --> 00:03:30,485
so we can make this smaller on the y-axis,

73
00:03:30,485 --> 00:03:34,015
or we can simply click on these three connected dots

74
00:03:34,010 --> 00:03:37,640
right here and just move it manually.

75
00:03:37,640 --> 00:03:40,370
So there you go.

76
00:03:40,370 --> 00:03:45,110
So now we have this collider right here on

77
00:03:45,110 --> 00:03:50,420
the wall or the bottom or whatever you wanna call it, we can unclick.

78
00:03:50,420 --> 00:03:52,820
This will run the game.

79
00:03:52,820 --> 00:03:54,440
And now our player,

80
00:03:54,440 --> 00:03:55,540
when he moves down,

81
00:03:55,535 --> 00:03:57,925
oh, well, what's happening?

82
00:03:57,920 --> 00:04:03,020
The player still hitting the wall and he is moving past. Why is that?

83
00:04:03,020 --> 00:04:06,700
Well, because we don't have a collider on our layer.

84
00:04:06,695 --> 00:04:10,465
So this is going to be your challenge.

85
00:04:10,460 --> 00:04:13,340
Your challenge is to add a collider to the player.

86
00:04:13,340 --> 00:04:15,230
So at a collider,

87
00:04:15,230 --> 00:04:18,470
a capsule, it doesn't have to be a capsule.

88
00:04:18,470 --> 00:04:21,890
It depends on the size and the frame of your player.

89
00:04:21,890 --> 00:04:23,810
The one that true personally got,

90
00:04:23,810 --> 00:04:26,840
or if you're using the same one as I am,

91
00:04:26,840 --> 00:04:32,620
make sure it's only player parent object, not the body.

92
00:04:32,615 --> 00:04:37,885
And change the size of the collider to the appropriate size of your player.

93
00:04:37,880 --> 00:04:39,740
So with these instructions,

94
00:04:39,740 --> 00:04:44,580
pause the video right now and go do the challenge.

95
00:04:44,580 --> 00:04:46,810
Okay, welcome back.

96
00:04:46,810 --> 00:04:49,120
So how did you get on with that?

97
00:04:49,120 --> 00:04:54,150
We are going to add a capsule collider 2D.

98
00:04:54,145 --> 00:04:55,015
There we go.

99
00:04:55,015 --> 00:05:00,015
We have the capsule, capsule shell and not even sure how it's pronounced.

100
00:05:00,010 --> 00:05:01,200
So we click on here.

101
00:05:01,195 --> 00:05:03,795
You can see the little green line in here.

102
00:05:03,790 --> 00:05:06,770
So we are going to take down.

103
00:05:06,774 --> 00:05:13,724
There we go and make it just about the size of our layer.

104
00:05:13,720 --> 00:05:17,260
Now, let's see if that works.

105
00:05:17,260 --> 00:05:19,620
If we run our game.

106
00:05:19,615 --> 00:05:22,695
Now when we go down walk,

107
00:05:22,690 --> 00:05:25,260
we are still moving through the object.

108
00:05:25,255 --> 00:05:26,885
Why is it not working?

109
00:05:26,880 --> 00:05:29,810
I should have probably warned you about this.

110
00:05:29,810 --> 00:05:34,580
Now we are going to add a Rigid Body 2D.

111
00:05:34,580 --> 00:05:36,980
And now when we run the game,

112
00:05:36,980 --> 00:05:40,780
we shall see that we hit the wall.

113
00:05:40,775 --> 00:05:42,995
But I didn't press anything.

114
00:05:42,995 --> 00:05:46,025
If I move up and let go, we fall down.

115
00:05:46,025 --> 00:05:46,975
Why is that?

116
00:05:46,970 --> 00:05:48,460
What is happening?

117
00:05:48,455 --> 00:05:51,175
Well, because when you add a rigid body,

118
00:05:51,170 --> 00:05:53,160
you also add gravity.

119
00:05:53,164 --> 00:05:57,684
Let's delve a little bit deeper into what a rigid body is.

120
00:05:57,680 --> 00:06:03,560
So it control of an object's position through physics simulation.

121
00:06:03,560 --> 00:06:06,940
So when you add a rigid body to a game object,

122
00:06:06,935 --> 00:06:08,855
you add physics to it.

123
00:06:08,855 --> 00:06:16,105
Rigid body object will be pulled downwards by gravity without adding any additional code.

124
00:06:16,100 --> 00:06:20,320
And we can also apply our own forces to them,

125
00:06:20,315 --> 00:06:22,975
which I mean by them, it's the rigid body.

126
00:06:22,970 --> 00:06:28,730
You can also go into unit is documentations and delve deeper into what rigid body is.

127
00:06:28,730 --> 00:06:32,540
And in fact, we are going to need to delve into it.

128
00:06:32,540 --> 00:06:34,190
So I'm going to go in here.

129
00:06:34,190 --> 00:06:37,400
Rigid body unity docs.

130
00:06:37,400 --> 00:06:38,780
There we go.

131
00:06:38,780 --> 00:06:40,070
There we go.

132
00:06:40,070 --> 00:06:42,790
We see the docs Unity 3D.

133
00:06:42,785 --> 00:06:46,495
And you can see the description, the properties,

134
00:06:46,490 --> 00:06:50,020
the public methods and so on and so forth.

135
00:06:50,015 --> 00:06:52,355
So let's go back and again,

136
00:06:52,355 --> 00:06:56,135
we can see right here we can change the mass, the angular drag.

137
00:06:56,134 --> 00:07:00,024
We can change the way the collision is detected, discrete, continuous,

138
00:07:00,020 --> 00:07:02,530
but it takes a bit more processing power,

139
00:07:02,525 --> 00:07:04,405
the sleep mode, the constraints,

140
00:07:04,400 --> 00:07:06,590
we're actually going to use them in just a bit.

141
00:07:06,590 --> 00:07:07,520
You'll see why.

142
00:07:07,520 --> 00:07:11,250
And we can change this from dynamic two kinematic, static.

143
00:07:11,254 --> 00:07:14,214
And each one of those are explained right here.

144
00:07:14,210 --> 00:07:16,330
But the main difference,

145
00:07:16,325 --> 00:07:18,535
whereas it explained somewhere in here,

146
00:07:18,530 --> 00:07:23,150
I'm not sure where you can read about each one to delve deeper into it,

147
00:07:23,150 --> 00:07:27,980
but know that dynamic means to be CAN affected by outside forces.

148
00:07:27,980 --> 00:07:30,290
Kinematic means it doesn't get affected.

149
00:07:30,290 --> 00:07:33,560
And static means is just like a big rock.

150
00:07:33,560 --> 00:07:35,480
It doesn't move at all.

151
00:07:35,480 --> 00:07:37,780
You can see right here the gravity scale.

152
00:07:37,775 --> 00:07:41,215
Let's set it to 0, save that, run the game.

153
00:07:41,210 --> 00:07:44,870
And now you'll notice that we're not fall anymore.

154
00:07:44,870 --> 00:07:46,310
So now when we go down,

155
00:07:46,310 --> 00:07:50,090
you can see that we can no longer go under this.

156
00:07:50,090 --> 00:07:52,720
But what, what, what happened?

157
00:07:52,715 --> 00:07:54,955
And you can see that we move around,

158
00:07:54,950 --> 00:07:56,360
we need to add a box collider.

159
00:07:56,360 --> 00:08:00,090
But what happened in here and what's the, this jittering?

160
00:08:00,094 --> 00:08:03,814
So first thing we'll fix as the flipping things.

161
00:08:03,815 --> 00:08:06,485
So this is because we are rotating.

162
00:08:06,485 --> 00:08:07,765
You can see right here,

163
00:08:07,760 --> 00:08:10,850
it's actually because the physics has calculated that

164
00:08:10,850 --> 00:08:14,020
we should rotate it because of the capsule right here.

165
00:08:14,015 --> 00:08:18,605
So we'll freeze the rotation around the z-axis while Z,

166
00:08:18,605 --> 00:08:26,065
because that show you if we click on the rotate tool up here or simply hit the Iike.

167
00:08:26,060 --> 00:08:28,580
We can see when we rotate,

168
00:08:28,580 --> 00:08:34,640
it's the z-axis that rotates us and we can clearly see it when we go out from 2D,

169
00:08:34,640 --> 00:08:37,370
you can click on this to the right here, we go out.

170
00:08:37,370 --> 00:08:40,240
You'll notice that the z-axis, and you can see it.

171
00:08:40,235 --> 00:08:41,155
There we go.

172
00:08:41,150 --> 00:08:45,070
It's around the z-axis that we are rotating.

173
00:08:45,065 --> 00:08:47,215
So we'll stop the to the,

174
00:08:47,210 --> 00:08:48,970
we'll set this to 0,

175
00:08:48,965 --> 00:08:51,055
will freeze on the Z.

176
00:08:51,050 --> 00:08:53,320
And now when we run our game,

177
00:08:53,315 --> 00:08:57,295
we go down. And there we go.

178
00:08:57,290 --> 00:09:00,350
We no longer flip if we move from left to right,

179
00:09:00,350 --> 00:09:02,930
but we still have this jittering.

180
00:09:02,930 --> 00:09:04,660
And what is this jittering?

181
00:09:04,655 --> 00:09:06,275
Where is it coming from?

182
00:09:06,275 --> 00:09:09,475
Well, it's a matter of code.

183
00:09:09,470 --> 00:09:15,350
We can see right here that we are moving the player using his transform, that position.

184
00:09:15,350 --> 00:09:18,320
So we have two colliding forces.

185
00:09:18,320 --> 00:09:22,900
We have the transformed position trying to go through the swallow,

186
00:09:22,895 --> 00:09:28,295
but we have the physics from the both colliders trying to push the player away.

187
00:09:28,295 --> 00:09:30,775
So this is creating this jittering.

188
00:09:30,770 --> 00:09:32,060
How do we fix this?

189
00:09:32,060 --> 00:09:34,990
Well, we are going to start moving the player,

190
00:09:34,985 --> 00:09:37,105
not using his transform position,

191
00:09:37,100 --> 00:09:40,090
but using his velocity.

192
00:09:40,085 --> 00:09:43,135
And velocity is available on the rigid body.

193
00:09:43,130 --> 00:09:47,120
If you find that somewhere right here, I'm not sure what it is.

194
00:09:47,120 --> 00:09:48,460
Let's see.

195
00:09:48,455 --> 00:09:49,825
Velocity.

196
00:09:49,820 --> 00:09:50,390
There you go.

197
00:09:50,390 --> 00:09:52,190
We can add velocity in here,

198
00:09:52,190 --> 00:09:54,920
angular velocity, the normal velocity.

199
00:09:54,920 --> 00:09:57,010
We can change it right here.

200
00:09:57,005 --> 00:09:58,355
So let's do that.

201
00:09:58,355 --> 00:10:02,245
If we go and you know what,

202
00:10:02,240 --> 00:10:03,550
let's add it up here.

203
00:10:03,545 --> 00:10:06,175
We'll create a serialized field.

204
00:10:06,170 --> 00:10:09,820
This will be a variable of type rigid body 2D,

205
00:10:09,815 --> 00:10:15,395
and we'll just call it the player rigid body.

206
00:10:15,395 --> 00:10:19,165
Save that. Oh, and one more thing you've noticed,

207
00:10:19,160 --> 00:10:20,740
and I forgot to tell you this.

208
00:10:20,735 --> 00:10:23,005
It's the way we name our variable.

209
00:10:23,000 --> 00:10:28,480
The first word should always have the first letter not capitalized,

210
00:10:28,475 --> 00:10:32,065
while all subsequent words will be capitalized.

211
00:10:32,060 --> 00:10:33,290
Why is that?

212
00:10:33,290 --> 00:10:35,110
Let's take an example from here.

213
00:10:35,105 --> 00:10:38,075
You'll notice that player is not capitalized.

214
00:10:38,075 --> 00:10:42,685
Rigid body is by the B for the body is not.

215
00:10:42,680 --> 00:10:44,380
So if we save that,

216
00:10:44,375 --> 00:10:46,475
we'll look in the inspector.

217
00:10:46,475 --> 00:10:48,745
You'll notice right here, the variable,

218
00:10:48,740 --> 00:10:54,980
the way it's written will be player rigid body with rigid body as a single word.

219
00:10:54,980 --> 00:11:00,100
If we go back, we make this be capitalized.

220
00:11:00,095 --> 00:11:01,085
We save that.

221
00:11:01,085 --> 00:11:02,635
We go back in the inspector,

222
00:11:02,630 --> 00:11:08,310
you'll notice that now the B is capitalized and it's a separate word.

223
00:11:08,305 --> 00:11:12,995
So every time we capitalize one of the words, it becomes separate.

224
00:11:12,995 --> 00:11:14,255
But we don't want that.

225
00:11:14,255 --> 00:11:15,865
We want it as a single one.

226
00:11:15,860 --> 00:11:18,730
You can see the same example and the movement speed.

227
00:11:18,725 --> 00:11:20,915
When we go back, there we go.

228
00:11:20,915 --> 00:11:26,795
So we've just created a reference to the rigid body on the player,

229
00:11:26,795 --> 00:11:30,665
but we haven't added it because you can see right now that the player rigid body,

230
00:11:30,665 --> 00:11:33,455
and there's just not how we do that.

231
00:11:33,455 --> 00:11:37,775
We simply click on the rigid body right here and we drag it up.

232
00:11:37,775 --> 00:11:39,235
So now you can see,

233
00:11:39,230 --> 00:11:40,540
as the name suggests,

234
00:11:40,535 --> 00:11:44,935
it's the rigid body on the dune layer.

235
00:11:44,930 --> 00:11:47,990
We save that back in our game.

236
00:11:47,990 --> 00:11:53,480
And here we are going to use instead of the transform position delta and all of that,

237
00:11:53,480 --> 00:11:56,630
I'm just going to comment this outlet on, we'll delete it,

238
00:11:56,630 --> 00:11:59,360
but I just want to have this all in front of

239
00:11:59,360 --> 00:12:02,930
you so you know the process of what's happening.

240
00:12:02,930 --> 00:12:07,180
So now we take the players rigid body,

241
00:12:07,175 --> 00:12:10,015
access the velocity on it.

242
00:12:10,010 --> 00:12:19,030
And it's going to be equal to the movement input times the movement speed.

243
00:12:19,025 --> 00:12:22,375
And because we are using the velocity,

244
00:12:22,370 --> 00:12:25,550
this will not require any Time.deltaTime.

245
00:12:25,550 --> 00:12:27,250
We save that.

246
00:12:27,245 --> 00:12:29,815
We go back in here.

247
00:12:29,810 --> 00:12:32,300
Let's move this guy up a bit.

248
00:12:32,300 --> 00:12:36,230
So now when we run our game, We move around.

249
00:12:36,230 --> 00:12:39,140
And if we try to move through the wall, there you go.

250
00:12:39,140 --> 00:12:42,920
You can see that we no longer have the jittering

251
00:12:42,920 --> 00:12:47,590
because we have the two physics engines working.

252
00:12:47,585 --> 00:12:50,455
Well, not the 2, 1 physics engine working on

253
00:12:50,450 --> 00:12:53,870
the both libraries because of the rigid body.

254
00:12:53,870 --> 00:12:57,880
And finally, we'll just add a couple of more colliders to the background.

255
00:12:57,875 --> 00:13:02,375
So I'm going to add a box collider 2D.

256
00:13:02,375 --> 00:13:05,915
I'm going to make this a bit smaller.

257
00:13:05,915 --> 00:13:12,095
And now it's never recommended that you add multiple box gliders on the same game object,

258
00:13:12,095 --> 00:13:14,505
but for now we'll make an exception.

259
00:13:14,509 --> 00:13:17,079
This will rarely happen.

260
00:13:17,075 --> 00:13:18,665
So there you go.

261
00:13:18,665 --> 00:13:21,155
Let's add a box collider like that.

262
00:13:21,155 --> 00:13:23,575
Let's add another one.

263
00:13:23,570 --> 00:13:26,530
And this one will be to the side.

264
00:13:26,525 --> 00:13:28,075
So we don't go out,

265
00:13:28,070 --> 00:13:30,080
we'll just make it a little bit bigger.

266
00:13:30,080 --> 00:13:34,150
And let's add the last box collider,

267
00:13:34,145 --> 00:13:39,325
and this one will be the one on the other side.

268
00:13:39,320 --> 00:13:42,560
There we go. We save all of that.

269
00:13:42,560 --> 00:13:44,720
We run our game.

270
00:13:44,720 --> 00:13:46,460
And now we walk down.

271
00:13:46,460 --> 00:13:49,130
We can't walk to the left, we can't.

272
00:13:49,130 --> 00:13:51,280
We walked to the right, we can't.

273
00:13:51,275 --> 00:13:52,675
And if we try to move up,

274
00:13:52,670 --> 00:13:55,490
we also can't, but there is a slight problem.

275
00:13:55,490 --> 00:13:59,110
You can see that we still have space in here to walk up to.

276
00:13:59,105 --> 00:14:03,295
So what I'm going to do is I'm going to just make

277
00:14:03,290 --> 00:14:08,410
the players collider a little bit smaller on the top.

278
00:14:08,405 --> 00:14:11,285
So let's just make it around here.

279
00:14:11,285 --> 00:14:19,205
We can even maybe change this from a capsule to something else and making it like that.

280
00:14:19,205 --> 00:14:20,485
Oops.

281
00:14:20,480 --> 00:14:21,410
Where is it?

282
00:14:21,410 --> 00:14:23,620
We change the position,

283
00:14:23,615 --> 00:14:27,835
so we move it on the X. Yep, there we go.

284
00:14:27,830 --> 00:14:29,840
We run our game.

285
00:14:29,840 --> 00:14:33,340
And we can see that we kind of move up.

286
00:14:33,335 --> 00:14:34,885
We still have this bit of space.

287
00:14:34,880 --> 00:14:36,860
This isn't a huge problem.

288
00:14:36,860 --> 00:14:39,590
We can even make B instead of a capsule,

289
00:14:39,590 --> 00:14:42,790
make a box collider or something else.

290
00:14:42,785 --> 00:14:48,515
But you can see that it all works of fine and dandy with that said,

291
00:14:48,515 --> 00:14:50,795
make sure to commit your changes.

292
00:14:50,795 --> 00:14:54,775
And in the next video will be aiming our gun around using

293
00:14:54,770 --> 00:14:59,800
the direction or where our mouse is pointing because currently,

294
00:14:59,795 --> 00:15:02,005
just have this gun is not doing much.

295
00:15:02,000 --> 00:15:04,980
I'll see you in the next one.

