1
00:00:03,040 --> 00:00:07,570
In this lecture, we're going to use the add torque method to rotate our player.

2
00:00:08,109 --> 00:00:11,560
And we're going to talk a little bit about linear drag and angular drag.

3
00:00:11,560 --> 00:00:12,700
So let's get started.

4
00:00:12,700 --> 00:00:15,160
We need to jump into our assets directory.

5
00:00:15,160 --> 00:00:21,490
Right click, create ourselves a new folder which we will call scripts, double click into that and

6
00:00:21,490 --> 00:00:28,420
then right click create a C-sharp script, which we will call player controller.

7
00:00:28,630 --> 00:00:30,430
Now let's just have a look at what we need to do here.

8
00:00:30,430 --> 00:00:32,650
Here's Barry clicked at the game object.

9
00:00:32,650 --> 00:00:34,480
Sorry, the parent game object level.

10
00:00:34,480 --> 00:00:37,960
If I click on a then I can rotate variance.

11
00:00:37,960 --> 00:00:39,460
What I want to do, I want to be rotating.

12
00:00:39,460 --> 00:00:42,790
And you can see I mentioned before that it's rotating around the inner pivot point.

13
00:00:42,790 --> 00:00:43,990
We can finesse that later on.

14
00:00:43,990 --> 00:00:48,280
If you want to feel like he's spinning more around his knees or his feet, then we can do that.

15
00:00:48,280 --> 00:00:51,130
But at the moment he's kind of spinning in the middle and that's okay for the moment.

16
00:00:51,130 --> 00:00:52,900
So you can see we want to apply this.

17
00:00:52,900 --> 00:00:56,770
And in the past what we've played around with is just changing the transform rotation.

18
00:00:56,770 --> 00:01:03,160
So just say make the rotation higher or lower on the Z axis or positive or negative, and that will

19
00:01:03,160 --> 00:01:04,090
have it move around.

20
00:01:04,090 --> 00:01:10,420
But that's a little bit clumsy because if we're saying, hey, move the rotation, move the transform

21
00:01:10,420 --> 00:01:16,300
rotation, but he's touching the world, we can have weird artifacts where it doesn't quite work properly.

22
00:01:16,300 --> 00:01:22,000
We've got the physics engine not playing nicely with us, just directly manipulating our game object

23
00:01:22,000 --> 00:01:22,540
like this.

24
00:01:22,540 --> 00:01:24,880
So I'm going to pop that back to zero.

25
00:01:24,880 --> 00:01:26,740
Sorry for spinning you around, Barry.

26
00:01:26,740 --> 00:01:29,470
What we need to do instead is apply a force.

27
00:01:29,470 --> 00:01:35,620
So to use Unity's physics engine to apply a force and the force that is used for rotation is called

28
00:01:35,620 --> 00:01:36,400
torque.

29
00:01:36,400 --> 00:01:39,820
So that's the name of the sort of force that will have our guys spin around.

30
00:01:39,820 --> 00:01:42,220
So let's open up our player controller script.

31
00:01:42,220 --> 00:01:49,840
And in here we're going to add some talk to the character when the player pushes a key on the keyboard.

32
00:01:49,840 --> 00:01:53,860
And the very first thing we need to think about is what is the torque being applied to?

33
00:01:54,010 --> 00:01:58,510
Well, just back into unity again, I know I've nearly jumped into this script, but back into here,

34
00:01:58,510 --> 00:02:01,900
we're applying the talk to the rigid body.

35
00:02:01,900 --> 00:02:06,880
Rigid bodies are the things that make objects move and respond to physics in unity.

36
00:02:06,880 --> 00:02:10,210
So first of all, we need to tap into this rigid body component.

37
00:02:10,210 --> 00:02:14,440
And the way we do that within our start method in here, we're going to use our jet component.

38
00:02:14,440 --> 00:02:18,880
If you're a jet component and we're going to get what sort of component, it'll be the rigid body,

39
00:02:18,880 --> 00:02:22,660
make sure it's 2D and then our parentheses and semicolon.

40
00:02:22,660 --> 00:02:25,240
So we're getting that, but getting it doesn't do much.

41
00:02:25,240 --> 00:02:29,590
We need to store it in a variable so we can use it in another place.

42
00:02:29,590 --> 00:02:36,610
So we will add a little space in here and we will store it in something called r b, so rigid body two

43
00:02:36,610 --> 00:02:39,310
D and then equals.

44
00:02:39,310 --> 00:02:42,880
And this is going to our red squiggly line because it's saying we haven't actually defined this as a

45
00:02:42,880 --> 00:02:43,540
variable yet.

46
00:02:43,540 --> 00:02:46,480
Now what I could do in START is to say, what is this?

47
00:02:46,480 --> 00:02:53,410
This is of type rigid body 2D and I could declare my variable in the start method just here.

48
00:02:53,410 --> 00:02:56,650
And our variable is called rigid body 2d.

49
00:02:56,680 --> 00:03:02,230
However, I'm wanting to use this in update because in update I want to say if you push left, make

50
00:03:02,230 --> 00:03:06,400
the rigid body apply torque in a left direction, if you push the right, apply in the right direction.

51
00:03:06,400 --> 00:03:11,620
So if I start to type R by 2D in here, you can see we've got our red squiggly line.

52
00:03:11,620 --> 00:03:12,460
And why is that?

53
00:03:12,460 --> 00:03:16,630
Well, we're going to talk about this a lot more throughout this course, but this is a first taste

54
00:03:16,630 --> 00:03:21,790
of the fact that certain things can be used in certain areas and not in others.

55
00:03:21,790 --> 00:03:24,130
And it's a way for us to protect our code.

56
00:03:24,130 --> 00:03:27,310
So here I can I can define a variable.

57
00:03:27,310 --> 00:03:33,550
And if it's between these curly braces within the start method, that means I can only ever use this

58
00:03:33,550 --> 00:03:40,390
and call this and modify this within my start method so it's local to within these curly braces.

59
00:03:40,390 --> 00:03:45,220
And so when I try to use it down here within a totally different method, it's saying I don't know what

60
00:03:45,220 --> 00:03:47,950
that is, doesn't exist in the current context.

61
00:03:47,950 --> 00:03:53,920
So what we're doing within START is we are encapsulating this variable, so it only lives within the

62
00:03:53,920 --> 00:03:54,700
START method.

63
00:03:54,700 --> 00:03:56,110
So what do we do in this instance?

64
00:03:56,110 --> 00:03:59,650
Well, you can see these curly prices up here, top and bottom.

65
00:03:59,650 --> 00:04:03,430
This relates to all of the content contained within our class.

66
00:04:03,430 --> 00:04:08,380
So if we want to make a class variable variable or a member variable, we could call it.

67
00:04:08,380 --> 00:04:16,450
And up at the very top of our class we can say rigid body to DX and then we will call it RB to DX and

68
00:04:16,450 --> 00:04:20,410
then semicolon and it's going to want to autocomplete semicolon.

69
00:04:20,560 --> 00:04:22,000
I'm going to get rid of this one.

70
00:04:22,000 --> 00:04:24,430
I've got down in update because we don't need it down there.

71
00:04:24,430 --> 00:04:27,730
And you can see we don't have a squiggly line there and we don't have a squiggly line here.

72
00:04:27,730 --> 00:04:34,450
And then I'm going to remove the type in front of my AB 2d because I don't need to define the type again.

73
00:04:34,450 --> 00:04:36,310
I'm just saying I already know what the variable is.

74
00:04:36,310 --> 00:04:42,790
The variable has been declared up to the top of our class and now I can use this anywhere in our class.

75
00:04:42,790 --> 00:04:43,960
I can use it within update.

76
00:04:43,960 --> 00:04:50,470
I can use it within a new method I create because it's a member variable and it is available to any

77
00:04:50,500 --> 00:04:53,350
of our methods within this class.

78
00:04:53,350 --> 00:04:58,090
So hopefully that explains a little bit of why we put some variables here, why we put some variables

79
00:04:58,090 --> 00:04:58,330
there.

80
00:04:58,330 --> 00:05:02,370
If you only need to use it within a method, there's no need to have it as a member variable.

81
00:05:02,700 --> 00:05:07,380
But if you want to have it accessible in many places, then you do need to have it as a member variable

82
00:05:07,380 --> 00:05:10,970
so that it is available anywhere within our class.

83
00:05:10,980 --> 00:05:15,060
In update, let's create ourselves and if statement if what are we doing in here?

84
00:05:15,060 --> 00:05:19,290
I'm going to use the old input system just because it's really quick and really easy.

85
00:05:19,290 --> 00:05:22,920
I promise you we will use the new input system at some point throughout this course.

86
00:05:22,920 --> 00:05:29,010
But all we need to say in here is if input dot get key and that's the method get key.

87
00:05:29,010 --> 00:05:32,010
So we're just using the input class and we're saying within there get key.

88
00:05:32,010 --> 00:05:35,850
We're trying to find if you've pushed a certain key on the keyboard and in parentheses here we need

89
00:05:35,850 --> 00:05:37,410
to say which key we're interested in.

90
00:05:37,410 --> 00:05:39,390
I'm going to say key code.

91
00:05:39,600 --> 00:05:42,420
We're looking for the left arrow.

92
00:05:42,420 --> 00:05:43,290
I think we'll do here.

93
00:05:43,290 --> 00:05:45,600
First of all, so left arrow.

94
00:05:45,600 --> 00:05:50,790
So this is a way of saying if we push the left key, then what do we want to do in here?

95
00:05:50,790 --> 00:05:54,360
Well, this is where we need to apply our ad talk.

96
00:05:54,360 --> 00:05:55,170
So what do we need to do?

97
00:05:55,170 --> 00:05:59,100
First of all, we need to access the rigid body two D So what should we start with here?

98
00:05:59,100 --> 00:06:06,090
Rigid body two D That means we're accessing Barry's rigid body to D dot and we can use the method in

99
00:06:06,090 --> 00:06:07,020
here ad talk.

100
00:06:07,020 --> 00:06:11,190
You can see right up here near the top, add talk and then you can see in the signature in here it's

101
00:06:11,190 --> 00:06:13,140
looking for parameter.

102
00:06:13,140 --> 00:06:17,820
Remember, parameters go within the parentheses, it's looking for a value that is a float and that

103
00:06:17,820 --> 00:06:19,950
value is going to be the amount of talk we want to apply.

104
00:06:19,950 --> 00:06:24,630
So we can add a value of, say, one in here, for example, and then put our semicolon.

105
00:06:24,630 --> 00:06:28,200
Now it's pretty bad to have a magic number that's buried within our code.

106
00:06:28,200 --> 00:06:31,830
So instead of doing one in here, let's create ourselves another member variable.

107
00:06:31,830 --> 00:06:37,110
Remember, member variable variable can be used anywhere in our code, right up at the top here above

108
00:06:37,110 --> 00:06:41,100
rigid body to DX that will be a serialized field and b of type.

109
00:06:41,100 --> 00:06:43,530
What what sort of type is this variable we're making?

110
00:06:43,530 --> 00:06:44,460
It will be a float.

111
00:06:44,460 --> 00:06:45,210
Very good.

112
00:06:45,210 --> 00:06:51,420
And we'll call this talk amount and then we'll initialize that at, I don't know, initialize it at

113
00:06:51,420 --> 00:06:56,550
one just to give it a value, a starting value else, or else it will initialize at zero.

114
00:06:56,550 --> 00:07:01,410
So instead of one in here, we'll type in talk amount and there we go.

115
00:07:01,410 --> 00:07:07,590
We've created a way for us to apply torque, which is a force that rotates whenever we push the left

116
00:07:07,590 --> 00:07:08,130
over arrow.

117
00:07:08,130 --> 00:07:10,680
So I'm going to save that back over into unity.

118
00:07:10,990 --> 00:07:13,410
There's one step we need to do for this to work.

119
00:07:13,410 --> 00:07:14,850
Do you know what step that is?

120
00:07:15,120 --> 00:07:19,860
We need to add our player controller to Barre Sung and a scroll down.

121
00:07:19,860 --> 00:07:24,240
I'm going to drag my player controller script where we get the blue line just there.

122
00:07:24,240 --> 00:07:24,930
Excellent.

123
00:07:24,930 --> 00:07:31,110
And now when I click on play, if I hit the left arrow, then we should see some sort of torque being

124
00:07:31,110 --> 00:07:31,740
applied.

125
00:07:31,740 --> 00:07:33,450
None yet it might be that.

126
00:07:33,450 --> 00:07:34,200
There we go.

127
00:07:34,230 --> 00:07:38,790
It was just that he was going down the hill and the amount of torque wasn't strong enough to counteract

128
00:07:38,790 --> 00:07:40,320
the physics through gravity.

129
00:07:40,320 --> 00:07:42,480
So I'll put the talk amount up to ten.

130
00:07:42,510 --> 00:07:43,260
We'll see how that goes.

131
00:07:43,260 --> 00:07:45,570
I have a feeling he's just going to go flip de flip.

132
00:07:46,080 --> 00:07:46,470
Yep.

133
00:07:47,070 --> 00:07:47,680
Whoa.

134
00:07:47,700 --> 00:07:48,450
There he.

135
00:07:48,650 --> 00:07:52,740
Okay, well, that could be a game right there, but we've successfully applied torque.

136
00:07:52,740 --> 00:07:57,330
When you push left on the left arrow, there's a lot of settings for us to tweak in here.

137
00:07:57,360 --> 00:08:01,560
And let's put this back to three for now, I think I'm not exactly sure what we want in there, but

138
00:08:01,800 --> 00:08:04,440
we're not yet at the stage where we want to tune it just yet.

139
00:08:04,440 --> 00:08:06,600
But I've got a challenge for you at this stage.

140
00:08:06,630 --> 00:08:10,950
The challenge is to finish our rotation when the player pushes the right arrow, make the character

141
00:08:10,950 --> 00:08:13,170
rotate in the other direction.

142
00:08:13,170 --> 00:08:13,920
So there you go.

143
00:08:13,920 --> 00:08:14,880
A quick little challenge.

144
00:08:14,880 --> 00:08:15,480
Pause the video.

145
00:08:15,480 --> 00:08:16,380
Take that on now.

146
00:08:17,970 --> 00:08:18,230
Okay.

147
00:08:18,270 --> 00:08:19,650
So this one's pretty straightforward.

148
00:08:19,650 --> 00:08:23,550
I'm going to copy my if statement that we've already put in there.

149
00:08:23,550 --> 00:08:24,840
Pop that down below.

150
00:08:24,870 --> 00:08:29,190
I'm going to change from left arrow to right arrow.

151
00:08:29,250 --> 00:08:32,940
And instead of torque amount, this is the one tricky bit in the whole area.

152
00:08:32,940 --> 00:08:34,049
Did you figure this out?

153
00:08:34,049 --> 00:08:36,570
We're going to add negative torque amount.

154
00:08:36,570 --> 00:08:38,070
We're going to have it go in the other direction.

155
00:08:38,070 --> 00:08:43,169
You could have gone and said float talk about is this and you know rotate one direction is this the

156
00:08:43,169 --> 00:08:47,040
other direction is that particularly if you want him to do a front flip quicker than a back flip, but

157
00:08:47,040 --> 00:08:48,180
I want it to be the same.

158
00:08:48,180 --> 00:08:51,180
I don't mind that it's going to say negative torque amount.

159
00:08:51,180 --> 00:08:53,820
What I'm also going to do in here, we've got two if statements.

160
00:08:53,820 --> 00:08:55,290
So if you're doing this, great.

161
00:08:55,290 --> 00:08:58,830
If you're doing that, what happens if you're pushing both of the arrows?

162
00:08:59,070 --> 00:09:00,450
That's going to be a bit of a conflict.

163
00:09:00,450 --> 00:09:02,820
So let's do something that says you can't do that.

164
00:09:02,820 --> 00:09:05,760
And the way we're going to do that is to add an else.

165
00:09:05,790 --> 00:09:09,000
If so, if you're pushing this, do that else.

166
00:09:09,000 --> 00:09:14,280
If so, in other words, if you're not already pushing this first if statement the first left key,

167
00:09:14,280 --> 00:09:16,950
that's what else means ls means otherwise.

168
00:09:16,950 --> 00:09:19,650
Then if you're pushing the right arrow, do this.

169
00:09:19,650 --> 00:09:23,670
That just is a way for us to make sure we're not accidentally doing both arrows at the same time and

170
00:09:23,670 --> 00:09:25,170
getting some weird sort of conflict.

171
00:09:25,170 --> 00:09:27,360
So I'm going to save click on play.

172
00:09:27,360 --> 00:09:28,200
We'll see what happens here.

173
00:09:28,200 --> 00:09:32,310
When I push left, arrow goes rotate, the left right arrow rotates to the right.

174
00:09:32,310 --> 00:09:32,940
Excellent.

175
00:09:32,940 --> 00:09:37,950
Although if I push left arrow and then let go, ooh, he's doing lots and lots of spinning.

176
00:09:37,950 --> 00:09:39,780
So let's look at that briefly what's going on here?

177
00:09:39,780 --> 00:09:43,800
If you look at Barre's rigid body, you'll see we've got a couple of values in here.

178
00:09:43,800 --> 00:09:47,790
Mass linear drag, angular drag and gravity scale.

179
00:09:47,790 --> 00:09:54,660
Now, the angular drag is going to relate to how much friction there is when we spin around and the

180
00:09:54,660 --> 00:09:58,950
linear drag is the same information when we're going forwards and backwards.

181
00:09:58,950 --> 00:10:01,500
So I have a little micro challenge for you here.

182
00:10:01,500 --> 00:10:08,040
Tune the linear drag and angular drag of your character until it feels good, until it's not just spinning

183
00:10:08,040 --> 00:10:11,490
out of control and getting into the death spin, I call it.

184
00:10:11,490 --> 00:10:15,750
So you can see here, if I just hold down lift and get him spinning.

185
00:10:16,680 --> 00:10:18,840
Okay, that's a little bit too quick for a snowboarder.

186
00:10:18,840 --> 00:10:19,560
I think so.

187
00:10:19,560 --> 00:10:24,570
Let's not have the death spin and let's have it that when you let go, he doesn't just keep spinning

188
00:10:24,570 --> 00:10:26,010
and spinning and spinning like that.

189
00:10:26,010 --> 00:10:31,770
So pause the video and play around with the tuning values of the linear drag and the angular drag and

190
00:10:31,770 --> 00:10:32,700
see how you go.

191
00:10:34,910 --> 00:10:35,360
Okay.

192
00:10:35,360 --> 00:10:35,860
Welcome back.

193
00:10:35,870 --> 00:10:37,940
Now, there's a really tricky thing going in here.

194
00:10:37,940 --> 00:10:42,320
Our angular drag and our torque amount or the speed we're trying to make it turn.

195
00:10:42,320 --> 00:10:43,760
Those two relate together.

196
00:10:43,760 --> 00:10:48,620
So I'm going to start with my talk amount at say, eight, and then I'm going to jump up to my angular

197
00:10:48,620 --> 00:10:49,820
drag and make that 15.

198
00:10:49,820 --> 00:10:51,320
Now, how do I know those are the numbers?

199
00:10:51,320 --> 00:10:55,670
Will I just put them in and play around with it and play around with it and just to see how it went?

200
00:10:55,670 --> 00:10:58,610
They're not perfect, but they're are they're better than they were before.

201
00:10:58,610 --> 00:11:05,720
And my linear drag I'm going to put at 0.5 for now, but that one will need to be balanced in conjunction

202
00:11:05,720 --> 00:11:09,560
with our level sprite shape, speed on the effect.

203
00:11:09,650 --> 00:11:16,790
So if the speed is ten, if we put this to say 20 back over onto Barre, if we put the linear drag at

204
00:11:16,790 --> 00:11:18,530
a higher amount, those two are going to relate.

205
00:11:18,530 --> 00:11:23,090
So if you have a much higher linear drag, you might need to have a higher speed on your effective.

206
00:11:23,090 --> 00:11:25,760
But let's see how my tuning values are going now.

207
00:11:25,760 --> 00:11:27,230
I've got those things set up.

208
00:11:27,230 --> 00:11:28,010
I'm going down the hill.

209
00:11:28,010 --> 00:11:28,670
Not too bad.

210
00:11:29,330 --> 00:11:31,400
That speeds a little bit too slow.

211
00:11:31,400 --> 00:11:33,350
But I'm not getting into the death spin.

212
00:11:33,350 --> 00:11:37,100
I'm spinning at a fairly consistent amount, which is what I like.

213
00:11:37,100 --> 00:11:39,620
I just don't want it to speed up or slow down.

214
00:11:39,620 --> 00:11:43,340
I want it to be just a nice, steady, consistent spin.

215
00:11:43,340 --> 00:11:46,400
Okay, so I think I need to make him spin a little bit quicker.

216
00:11:46,400 --> 00:11:50,150
So I'm going to put this up to ten and see if that's just a little bit nicer.

217
00:11:50,950 --> 00:11:55,000
When it comes to spinning and not super out of control looks pretty quick.

218
00:11:55,300 --> 00:11:56,020
Go up the hill.

219
00:11:57,070 --> 00:11:57,730
That's pretty good.

220
00:11:57,730 --> 00:12:00,580
That's roughly what I'm looking for here with my spin.

221
00:12:00,580 --> 00:12:01,090
So there we go.

222
00:12:01,090 --> 00:12:02,770
We've got some general values.

223
00:12:02,770 --> 00:12:03,670
We're in the ballpark.

224
00:12:03,670 --> 00:12:09,250
And you've learned a little bit about the angular drag and linear drag and the torque amount we've set

225
00:12:09,250 --> 00:12:13,390
ourselves up so that we're using the add torque method to spin around our snowboarder.

226
00:12:13,390 --> 00:12:13,900
Great stuff.

227
00:12:13,900 --> 00:12:15,310
I'll see you in the next lecture.

