1
00:00:03,170 --> 00:00:05,570
In this lecture, I'm going to be adding a second game object.

2
00:00:05,570 --> 00:00:11,240
And also we're going to be playing around with colliders and rigid bodies so objects can bump into each

3
00:00:11,240 --> 00:00:15,320
other and use the unity physics system to apply force to one another.

4
00:00:15,320 --> 00:00:17,060
So let's jump in and get started.

5
00:00:17,060 --> 00:00:19,430
Let's create ourselves something to bump into.

6
00:00:19,430 --> 00:00:21,650
So a little micro challenge for you here.

7
00:00:21,650 --> 00:00:26,750
Add a new sprite into our scene, which is a square or a circle.

8
00:00:28,090 --> 00:00:28,450
Okay.

9
00:00:28,450 --> 00:00:32,189
So over in our hierarchy, right click to the object sprites.

10
00:00:32,200 --> 00:00:36,640
I'm going to add a circle, I think, and then gives a chance to name it.

11
00:00:36,640 --> 00:00:38,560
I'll leave it as circle for the moment.

12
00:00:38,560 --> 00:00:40,600
I'm not exactly sure what it's going to be yet.

13
00:00:40,600 --> 00:00:41,920
I'm going to click on that circle hit.

14
00:00:42,490 --> 00:00:44,230
Just move it over to the side.

15
00:00:44,230 --> 00:00:46,690
Well, here we can also play around with the scaling of it.

16
00:00:46,690 --> 00:00:51,730
So I might do that, hit R on the keyboard and just scale it up a little bit around about there.

17
00:00:51,730 --> 00:00:52,030
You know what?

18
00:00:52,030 --> 00:00:54,940
I'm just going to type in to tab two.

19
00:00:54,940 --> 00:00:56,050
Tab two.

20
00:00:56,080 --> 00:01:00,520
When I say tab, I'm not typing tab, I'm just hitting tab to move to the next cell so that I can type

21
00:01:00,520 --> 00:01:01,120
in 2 to 2.

22
00:01:01,120 --> 00:01:01,480
There we go.

23
00:01:01,480 --> 00:01:05,080
We've got a two sized circle to bump into.

24
00:01:05,080 --> 00:01:07,750
I'm also going to change the color from within Sprite Renderer.

25
00:01:07,750 --> 00:01:09,160
You see color as a field.

26
00:01:09,160 --> 00:01:15,340
Click on the white space in there and change the color circle thing around the outside and then circle

27
00:01:15,340 --> 00:01:16,510
thing on the inside.

28
00:01:16,540 --> 00:01:18,520
It's kind of a yellowy orange color.

29
00:01:18,520 --> 00:01:19,030
There we go.

30
00:01:19,030 --> 00:01:20,020
So I'm going to click on play.

31
00:01:20,020 --> 00:01:21,910
We'll see what behavior we've got here at the moment.

32
00:01:21,910 --> 00:01:27,070
If I turn my vehicle towards the circle, drive over it or towards it, I just kind of drive past it.

33
00:01:27,070 --> 00:01:27,700
Nothing happens.

34
00:01:27,700 --> 00:01:28,540
The circle doesn't move.

35
00:01:28,540 --> 00:01:30,550
There's no bumping into going on here.

36
00:01:30,550 --> 00:01:32,620
So what do we need to do to get these things to bump?

37
00:01:32,620 --> 00:01:38,740
At the moment, all we've got is a Sprite renderer as the component on these game objects that says

38
00:01:38,740 --> 00:01:39,850
it looks a certain way.

39
00:01:39,850 --> 00:01:40,570
It's an image.

40
00:01:40,570 --> 00:01:44,020
Basically what we need to do is add to things.

41
00:01:44,020 --> 00:01:48,040
One of the things needs to go on both of these objects and the other thing needs to go and only one

42
00:01:48,040 --> 00:01:48,430
object.

43
00:01:48,430 --> 00:01:49,630
Okay, so what are these two things?

44
00:01:49,630 --> 00:01:52,240
First of all, we need to add colliders.

45
00:01:52,240 --> 00:01:58,330
So we go on to our player, click on our player, then find down the bottom in the inspector, add component.

46
00:01:58,330 --> 00:02:02,470
I'm going to search for Collider and we've got a whole bunch of options in here.

47
00:02:02,470 --> 00:02:07,750
Now, pay close attention to the fact that some of them say 2D and some of them don't say 2D.

48
00:02:07,750 --> 00:02:12,250
The ones that don't say 2D is when we're working with a three dimensional object.

49
00:02:12,520 --> 00:02:14,380
In other words, something that's not a Sprite.

50
00:02:14,380 --> 00:02:19,270
But when we're working with Sprite, as we are here, we want to be adding things that say 2D down the

51
00:02:19,270 --> 00:02:19,750
end.

52
00:02:19,750 --> 00:02:20,950
So what have I got here?

53
00:02:20,950 --> 00:02:23,890
Well, it's a capsule, so I'm going to look for a capsule.

54
00:02:23,890 --> 00:02:27,400
Collider two d click on that and then scroll right in.

55
00:02:27,430 --> 00:02:28,660
So you can see this.

56
00:02:28,660 --> 00:02:31,870
You can see we've got our capsule Collider 2D component.

57
00:02:31,870 --> 00:02:36,370
And what I'll do just for a moment is to turn off my Sprite renderer so that we don't have our image

58
00:02:36,370 --> 00:02:36,730
anymore.

59
00:02:36,730 --> 00:02:39,550
And you can see we've got this green line around the outside.

60
00:02:39,550 --> 00:02:44,470
Now, the player never sees the green line, but Unity knows that this green line is collision.

61
00:02:44,470 --> 00:02:47,230
It's an invisible force field that we can have things touch.

62
00:02:47,230 --> 00:02:49,480
And when they touch, we can say what the behavior will be.

63
00:02:49,480 --> 00:02:54,400
For example, they they bump off and go in a different direction or they can't pass through.

64
00:02:54,400 --> 00:02:56,890
So this is when we have a collider.

65
00:02:56,890 --> 00:02:59,440
We're also going to talk about triggers in the next lecture.

66
00:02:59,440 --> 00:03:04,480
That's something slightly different where you say some things can pass through this collision area,

67
00:03:04,480 --> 00:03:06,430
but when it does, we want to send a message.

68
00:03:06,430 --> 00:03:08,230
We want to know that it passed through it.

69
00:03:08,230 --> 00:03:12,400
You know, you've you've entered the force field and something happens or you've exited the force field

70
00:03:12,400 --> 00:03:14,110
or you're still inside the force field.

71
00:03:14,110 --> 00:03:16,480
So there's a lot of things we can do in relation to this collision.

72
00:03:16,480 --> 00:03:21,250
So I'll turn Sprite Renderer back on and then we'll jump over to our circle and we'll do the same thing.

73
00:03:21,250 --> 00:03:23,320
So little micro challenge for you again.

74
00:03:23,320 --> 00:03:29,560
Pause the video and add the appropriate sort of collider to your circle or to your square.

75
00:03:29,560 --> 00:03:31,450
If you added a square instead of a circle.

76
00:03:32,340 --> 00:03:32,570
Okay.

77
00:03:32,580 --> 00:03:35,120
So we click on Circle Add component.

78
00:03:35,130 --> 00:03:37,350
My search that I put in before is still there.

79
00:03:37,350 --> 00:03:37,560
So.

80
00:03:37,560 --> 00:03:41,250
Collider And because it's a circle, I'm going to add a circle collider to DX.

81
00:03:41,280 --> 00:03:44,990
If you added a square, then you would add a box collider to DX.

82
00:03:45,000 --> 00:03:50,490
And once again it should conform nicely to the shape of the object that we have in there, which is

83
00:03:50,490 --> 00:03:51,730
really handy.

84
00:03:51,750 --> 00:03:53,690
So you can see there we go, we've got our collision.

85
00:03:53,700 --> 00:03:56,910
Okay, so we have collision now on the outside of our objects.

86
00:03:56,910 --> 00:04:00,330
That means that unity knows, okay, what is going to bump into what.

87
00:04:00,330 --> 00:04:06,270
But there's one last step we need to do, and that is to add to at least one of these objects needs

88
00:04:06,270 --> 00:04:07,940
a rigid body.

89
00:04:07,950 --> 00:04:09,230
What is a rigid body?

90
00:04:09,240 --> 00:04:12,390
A rigid body is a way of saying this thing has substance.

91
00:04:12,390 --> 00:04:18,540
And it's the thing that the unity physics engine understands that it's going to move around and it's

92
00:04:18,540 --> 00:04:22,740
going to be able to bump into and collide and have physics interactions.

93
00:04:22,740 --> 00:04:27,150
So let's scroll down, we'll click on our player, scroll down to add component and we'll search for

94
00:04:27,150 --> 00:04:34,230
rigid body, fine rigid body 2D and add that this now says the player.

95
00:04:34,230 --> 00:04:40,830
Our cruising MC drive can interact with things using the physics system in unity and if we click on

96
00:04:40,830 --> 00:04:42,300
play, we'll see some behavior here.

97
00:04:42,300 --> 00:04:43,200
That's quite interesting.

98
00:04:43,200 --> 00:04:47,070
Our object now just automatically drops off the screen.

99
00:04:47,070 --> 00:04:47,640
Why is that?

100
00:04:47,640 --> 00:04:49,890
Well, that's because we've got gravity on.

101
00:04:49,890 --> 00:04:55,590
So if we set gravity to zero, that means that this object is not going to have any gravity applied

102
00:04:55,590 --> 00:04:57,570
to it and it's not going to drop off the screen.

103
00:04:57,570 --> 00:04:58,950
Why would we want gravity?

104
00:04:58,950 --> 00:05:02,910
Well, if we're making a side scrolling platform, we want to be able to jump and drop and jump and

105
00:05:02,910 --> 00:05:03,300
drop.

106
00:05:03,300 --> 00:05:05,700
And gravity is quite useful and quite important.

107
00:05:05,700 --> 00:05:10,920
But for us, we're making a top down driving game and there's no gravity that we can see.

108
00:05:10,920 --> 00:05:14,100
So now when I click on play, we see it doesn't go anywhere.

109
00:05:14,100 --> 00:05:20,310
And if I drive my vehicle, drive, drive, drive and bump, it bounces into this and kind of gets flung

110
00:05:20,400 --> 00:05:21,270
out of the way.

111
00:05:21,300 --> 00:05:23,130
It's not actually moving the circle.

112
00:05:23,130 --> 00:05:24,330
We'll get to that in a second.

113
00:05:24,330 --> 00:05:28,470
But I can actually have this work with the collision.

114
00:05:28,470 --> 00:05:32,880
But it's a bit odd because I bounce into this thing and it's just staying there and not moving.

115
00:05:32,880 --> 00:05:38,280
That could be valid if you've got, say, a car and then you've got like a street post or a building

116
00:05:38,280 --> 00:05:39,900
or something that you don't want to have.

117
00:05:39,900 --> 00:05:42,810
Move like a car is not going to go and move a building.

118
00:05:43,170 --> 00:05:46,290
The building is going to stand there and say, No, you didn't hurt me at all.

119
00:05:46,290 --> 00:05:49,140
But what if we wanted to move this object?

120
00:05:49,140 --> 00:05:51,930
When we bumped into it, we wanted to have it respond a little bit.

121
00:05:51,930 --> 00:05:54,390
Well, this is a little micro challenge for you.

122
00:05:54,390 --> 00:06:01,770
Again, what can you add to our circle so that when the car bumps into it, it moves?

123
00:06:02,590 --> 00:06:06,370
We can add a rigid body to this object to the circle.

124
00:06:06,370 --> 00:06:11,260
So add component once again rigid body to and what do we need to do?

125
00:06:11,260 --> 00:06:14,140
First, we need to change the gravity scale to zero.

126
00:06:14,140 --> 00:06:16,630
That's important so it doesn't go dropping off the screen.

127
00:06:17,020 --> 00:06:22,660
Now I'm going to click on play, turn my vehicle around and there's a little bit of a reaction.

128
00:06:22,660 --> 00:06:24,640
It's moving a little bit when we bump into it.

129
00:06:24,640 --> 00:06:27,130
It's not going flying off the screen and that's all fine.

130
00:06:27,130 --> 00:06:32,230
But I'm finding it a little bit difficult to kind of push the object because I keep sliding off because

131
00:06:32,230 --> 00:06:37,540
we've got this precise collision on the front of our objects, all very sliding, if you will.

132
00:06:37,540 --> 00:06:38,890
It's not hitting it tightly.

133
00:06:38,890 --> 00:06:46,180
What you could do, it's going to click play is click on our player, and then I'm just going to roll

134
00:06:46,180 --> 00:06:47,650
up a lot of these components.

135
00:06:47,650 --> 00:06:51,070
So if you click on the little arrow triangle thing here, I'm going to roll these up, roll out the

136
00:06:51,070 --> 00:06:51,610
Sprite render.

137
00:06:51,610 --> 00:06:52,180
It's still there.

138
00:06:52,180 --> 00:06:53,710
It's just I'm not seeing all the details.

139
00:06:53,710 --> 00:06:57,430
It's a way of hiding it and making your inspector more clean and tidy.

140
00:06:57,430 --> 00:06:58,420
So roll up all of these.

141
00:06:58,420 --> 00:06:59,140
Roll up all of these.

142
00:06:59,140 --> 00:06:59,470
There we go.

143
00:06:59,470 --> 00:07:00,310
It's a little bit tidier.

144
00:07:00,310 --> 00:07:01,330
I can see what I've got.

145
00:07:01,330 --> 00:07:07,930
And the Capsule Collider I'm going to click the little checkbox that says, Don't use this when we play

146
00:07:07,930 --> 00:07:08,410
the game.

147
00:07:08,410 --> 00:07:09,370
We're not using this anymore.

148
00:07:09,370 --> 00:07:11,230
It's still there, but we're just not using it.

149
00:07:11,230 --> 00:07:16,930
I'm going to add a component which will be a collider, see a box collider to DX.

150
00:07:16,960 --> 00:07:22,600
Now, when I look at my player, you can see that it's more of a straight edge at the front.

151
00:07:22,600 --> 00:07:31,570
So when I click on play, drive over to my circle, it's going to act a little bit more like a a thing

152
00:07:31,570 --> 00:07:34,030
that can push a bit more of a bulldozer, if you will.

153
00:07:34,030 --> 00:07:36,220
It's not going to give a very good effect when I do that.

154
00:07:36,220 --> 00:07:38,710
Like you can see it's going to look odd for the player.

155
00:07:38,710 --> 00:07:43,510
So I'm using this a little bit sparingly just for you and I now to see that that is another way that

156
00:07:43,510 --> 00:07:44,500
we could push a thing.

157
00:07:44,500 --> 00:07:48,490
Instead of having this capsule random thing at the front, we can have it as a square collider and also

158
00:07:48,490 --> 00:07:50,170
showing you that you could change the collider.

159
00:07:50,170 --> 00:07:55,960
Now what I could do is for my capsule collider, I could right click and remove component and we've

160
00:07:55,960 --> 00:07:56,620
deleted that.

161
00:07:56,620 --> 00:07:57,580
That's not there anymore.

162
00:07:57,580 --> 00:08:03,580
I've just got this box collider on my player, so it's pretty easy to add and remove colliders as your

163
00:08:03,580 --> 00:08:04,720
heart desires.

164
00:08:04,720 --> 00:08:11,290
So just to recap, we've added colliders to our to game objects so that they have the ability to understand

165
00:08:11,290 --> 00:08:14,080
when they're connected and touching and bumping into things.

166
00:08:14,080 --> 00:08:19,570
And we've also added rigid bodies to both of them so that they both have the ability to respond to our

167
00:08:19,570 --> 00:08:20,470
physics system.

168
00:08:20,470 --> 00:08:24,520
They can be moved around, they can interact, they can bump and all those kind of good things.

169
00:08:24,520 --> 00:08:25,270
So there we go.

170
00:08:25,390 --> 00:08:28,330
A quick introduction to colliders and rigid bodies.

