1
00:00:03,080 --> 00:00:03,710
Hello again.

2
00:00:03,710 --> 00:00:08,930
In this lecture we are having a very high level overview of what methods are and we're just going to

3
00:00:08,930 --> 00:00:14,290
make a capsule spin around by using the transform rotate method.

4
00:00:14,300 --> 00:00:15,440
Okay, let's get started.

5
00:00:15,470 --> 00:00:16,140
Hello and welcome.

6
00:00:16,140 --> 00:00:19,310
We're going to start this lecture off with a challenge right from the get go.

7
00:00:19,310 --> 00:00:22,460
I want you to start a new 2D project in Unity.

8
00:00:22,460 --> 00:00:25,010
So open up Unity Hub, start yourself a new project.

9
00:00:25,010 --> 00:00:26,060
Call it whatever you like.

10
00:00:26,060 --> 00:00:29,270
Mine will be called delivery driver, but you can call it whatever you like.

11
00:00:29,270 --> 00:00:34,970
And then once the project is open, want you to add a capsule sprite, and that's going to be our car.

12
00:00:34,970 --> 00:00:40,370
And then I'd like you to create a C-sharp script or call driver, and then I'd like for you to add the

13
00:00:40,370 --> 00:00:43,340
script to your capsule sprite as a component.

14
00:00:43,340 --> 00:00:48,020
So if you're not exactly sure how to do that, we covered that in the very first intro section, but

15
00:00:48,020 --> 00:00:49,880
it's a case of adding that to your Sprite.

16
00:00:49,880 --> 00:00:54,890
And then last step, rename the capsule sprite to give you a car, a nifty name.

17
00:00:54,890 --> 00:00:56,600
Mine's going to be called Cruisy MC Drive.

18
00:00:56,630 --> 00:00:58,370
You can call yours whatever you like.

19
00:00:58,370 --> 00:00:59,540
There's your challenge.

20
00:00:59,540 --> 00:01:03,770
Pause the video and then start the video again once you've completed those steps.

21
00:01:06,310 --> 00:01:06,580
Hi.

22
00:01:06,580 --> 00:01:07,240
Okeydokey.

23
00:01:07,240 --> 00:01:08,560
So I'm in Unity Hub.

24
00:01:08,560 --> 00:01:09,760
I'm going to click on New.

25
00:01:09,760 --> 00:01:18,910
I'm using 20, 21.1, etc. I'm going to click on to DX, rename my project to be called Delivery Driver

26
00:01:18,910 --> 00:01:20,920
and Repos Unity two DX.

27
00:01:20,920 --> 00:01:22,240
Yep, that's exactly where I want it.

28
00:01:22,240 --> 00:01:23,560
Click the create button.

29
00:01:23,560 --> 00:01:25,660
Wait a moment, make myself a cup of tea.

30
00:01:25,690 --> 00:01:31,180
Go for a walk around the block and when I get back, unity hopefully will be good and ready to go.

31
00:01:31,930 --> 00:01:32,410
Okay.

32
00:01:32,410 --> 00:01:32,970
Welcome back.

33
00:01:32,980 --> 00:01:36,790
I had a nice little walk around the neighborhood, did some push ups, and here I am ready to roll a

34
00:01:36,790 --> 00:01:37,690
little bit out of breath.

35
00:01:37,690 --> 00:01:38,860
But good for action.

36
00:01:38,860 --> 00:01:46,420
I'm going to jump over into my hierarchy right click and select 2D object sprites and find a capsule.

37
00:01:46,420 --> 00:01:48,670
I'm going to rename it right now.

38
00:01:48,670 --> 00:01:50,560
Actually, I'm going to do the challenge a little bit.

39
00:01:50,560 --> 00:01:52,090
Out of order like a rebel.

40
00:01:52,090 --> 00:01:52,660
What was he called?

41
00:01:52,660 --> 00:01:54,730
Cruisy Muck drive.

42
00:01:54,760 --> 00:01:57,880
Kind of makes me want to eat fast food, but hopefully I'm not going to get sued.

43
00:01:57,880 --> 00:02:02,890
That's probably far enough away from copyright, I think, to be able to call it MC Drive.

44
00:02:02,890 --> 00:02:03,580
What are we doing next?

45
00:02:03,580 --> 00:02:09,490
We're creating ourselves a script, so right click down in the assets area, create C-sharp script.

46
00:02:09,490 --> 00:02:12,010
We will call this driver.

47
00:02:12,040 --> 00:02:16,510
Now the last step was to click on Cruisy MC Drive and then drag over driver.

48
00:02:16,510 --> 00:02:21,160
I could have also clicked on add component and searched for driver in there and that would have done

49
00:02:21,160 --> 00:02:22,480
the same job.

50
00:02:22,480 --> 00:02:24,160
Now it's time for a little bit of theory.

51
00:02:24,160 --> 00:02:24,370
Okay.

52
00:02:24,370 --> 00:02:25,540
We're going to talk about methods.

53
00:02:25,540 --> 00:02:26,260
What are methods?

54
00:02:26,260 --> 00:02:28,390
Well, methods are also called functions.

55
00:02:28,390 --> 00:02:30,880
So you might hear a lot of people referring to them as functions.

56
00:02:30,880 --> 00:02:35,350
Technically, technically, they're methods, but they're kind of pretty much interchangeable.

57
00:02:35,350 --> 00:02:40,960
Those two words when it comes to the way that we're using it in unity and C sharp methods execute blocks

58
00:02:40,960 --> 00:02:43,330
of code that makes our game do things.

59
00:02:43,330 --> 00:02:51,340
So methods do stuff essentially and we can use the methods already available in unity that some hardworking

60
00:02:51,340 --> 00:02:55,030
programmer in Unity headquarters has created for us to use.

61
00:02:55,030 --> 00:03:00,910
Or we can create our own methods and we'll be doing plenty of both of these throughout the entire course.

62
00:03:00,910 --> 00:03:06,400
The other thing I want you to know in this very, very quick intro to methods is that we do creating

63
00:03:06,400 --> 00:03:07,120
and calling.

64
00:03:07,120 --> 00:03:12,640
So when we create a method, we're giving it a name and saying all the things that it should do.

65
00:03:12,640 --> 00:03:17,020
So a method might be make the thing move forward, make the thing turn around.

66
00:03:17,020 --> 00:03:23,380
And we also call a method, and that is when we are saying, do the things now, please, let's jump

67
00:03:23,380 --> 00:03:28,000
back into our game and I'm going to open up my driver script, double click on that.

68
00:03:28,000 --> 00:03:30,250
That should open up Visual Studio code.

69
00:03:30,280 --> 00:03:32,350
Just going to tidy things up a little bit here.

70
00:03:32,920 --> 00:03:35,770
Let's see, I'm going to full screen this actually and make it a little bit bigger.

71
00:03:35,770 --> 00:03:40,360
So while we're learning things, we're taking up the entire screen just for a moment.

72
00:03:40,360 --> 00:03:44,950
And you can see right at the very start, we've got our using these are namespaces.

73
00:03:44,950 --> 00:03:46,180
We'll get to that later on.

74
00:03:46,180 --> 00:03:50,560
But basically it's saying, hey, you can use all of the things that are contained within here that

75
00:03:50,560 --> 00:03:53,470
the the unity engineers have given for us.

76
00:03:53,470 --> 00:03:57,670
We've got public class classes as a way of grouping similar things together.

77
00:03:57,670 --> 00:04:02,110
And within particular classes we have particular methods we can execute.

78
00:04:02,110 --> 00:04:06,460
And then we get to the stars of the show, start and update.

79
00:04:06,460 --> 00:04:10,060
We also have in front of START we have some comments in here.

80
00:04:10,060 --> 00:04:14,470
You can tell it's a comment because it's in green, but also it has a double forward slashes at the

81
00:04:14,470 --> 00:04:15,010
start.

82
00:04:15,010 --> 00:04:19,450
You can do any old comments, you want to hit double fours slash and say this is staff.

83
00:04:19,450 --> 00:04:20,010
Yay.

84
00:04:20,019 --> 00:04:22,089
So that's a way to create comments.

85
00:04:22,089 --> 00:04:23,590
Pretty handy in this case.

86
00:04:23,590 --> 00:04:28,420
We don't need these particular comments, so I'm just going to delete them for the moment just so we

87
00:04:28,420 --> 00:04:30,160
can keep this as clean as possible.

88
00:04:30,160 --> 00:04:31,540
But in general, I don't bother.

89
00:04:31,540 --> 00:04:32,800
I just leave them in there.

90
00:04:32,800 --> 00:04:34,870
Okay, so start and update.

91
00:04:34,870 --> 00:04:38,470
These are methods in particular, they're callbacks.

92
00:04:38,470 --> 00:04:42,790
Now, we don't need to dig into the depth of what callbacks are, but what I'd like you to understand

93
00:04:42,790 --> 00:04:44,920
at the moment is unity.

94
00:04:45,130 --> 00:04:52,330
The unity engine and the unity system of how it executes the game is going to call, start and update

95
00:04:52,330 --> 00:04:53,800
a little bit in the black box.

96
00:04:53,800 --> 00:04:59,710
So somewhere Unity is saying, hey, do all the things in start and it's saying do all the things in

97
00:04:59,710 --> 00:05:00,250
update.

98
00:05:00,250 --> 00:05:01,780
Now when does it call that?

99
00:05:01,780 --> 00:05:03,220
When does it say you do all the things that start?

100
00:05:03,220 --> 00:05:09,070
It's just when we click on the play button, we click on Play Boink, and then anything that's in start

101
00:05:09,070 --> 00:05:15,040
on any scripts that we have in our scene, it will execute, it will run and say, Do these things now

102
00:05:15,040 --> 00:05:16,300
and then we have an update.

103
00:05:16,300 --> 00:05:21,340
Update is called or update is happening or update is doing its stuff, if you will.

104
00:05:21,340 --> 00:05:27,040
Every single frame that our game is running and we have lots of frames that are running every single

105
00:05:27,040 --> 00:05:27,490
second.

106
00:05:27,490 --> 00:05:30,700
So let's jump back into Unity and have a look at what we'd like to do here.

107
00:05:30,730 --> 00:05:35,320
Now I'm going to click on Cruzi hit W to move Cruzi to the side because I don't like that he's sitting

108
00:05:35,320 --> 00:05:36,730
right underneath the camera here.

109
00:05:36,730 --> 00:05:41,350
And what are we going to do in this lecture is rotate cruzi using code.

110
00:05:41,350 --> 00:05:45,370
And if you have a look, we've played around this before, we've got our position up, down, left,

111
00:05:45,370 --> 00:05:47,560
right, and we've also got our rotation.

112
00:05:47,560 --> 00:05:50,080
We want to be rotating on the Z or the Z axis.

113
00:05:50,080 --> 00:05:50,800
How do I know that?

114
00:05:50,800 --> 00:05:52,120
Well, I went to X and played around.

115
00:05:52,120 --> 00:05:56,050
I went to Y and played around and went to Z and I'm going to call it Z and played around.

116
00:05:56,050 --> 00:05:57,820
And Z was the one who who did it.

117
00:05:57,820 --> 00:06:03,160
The other way you can look at it is say, well, if we are rotating around this green, it's going to

118
00:06:03,160 --> 00:06:04,480
rotate around that way.

119
00:06:04,480 --> 00:06:09,970
If we're rotating around this red or rotate that way, Z is going into the world and it's going to rotate

120
00:06:09,970 --> 00:06:10,510
around Z.

121
00:06:10,540 --> 00:06:11,920
How do I know it goes into the world?

122
00:06:11,920 --> 00:06:12,760
How can we see that?

123
00:06:12,760 --> 00:06:14,890
Well, a little sneaky trick for you here.

124
00:06:14,920 --> 00:06:17,380
Up in our scene window, you see a 2D button.

125
00:06:17,380 --> 00:06:21,010
If you click on that, it will toggle from 2D to 3D.

126
00:06:21,040 --> 00:06:25,090
I'm going to hold down old and my left mouse button, which will allow me to rotate around.

127
00:06:25,090 --> 00:06:30,700
And you can see that we have this Z or Z axis that's going into our world.

128
00:06:30,730 --> 00:06:32,800
And if we rotate around that.

129
00:06:32,800 --> 00:06:34,690
So I'm going to hit my E key.

130
00:06:34,700 --> 00:06:40,450
You can see I'm rotating on the blue which rotates around the Z and also just a little sneakiness that

131
00:06:40,900 --> 00:06:46,690
in Unity two DX games are basically just 3D games where we lock the camera by clicking this 2D button

132
00:06:46,690 --> 00:06:51,250
here and we are only moving things around on the X and the Y axis.

133
00:06:51,280 --> 00:06:51,550
Okay.

134
00:06:51,550 --> 00:06:56,920
The next thing I'll show you is at the moment, if I was to rotate, cruisy, rotate like this, my

135
00:06:56,920 --> 00:07:03,030
widget is staying at the worlds y up direction and the world's x direction.

136
00:07:03,040 --> 00:07:06,580
I'm going to undo that move if I come up to the top here and see where it says global.

137
00:07:06,580 --> 00:07:14,740
If I click from global to local now as I rotate, you'll see that the Green Arrow is staying forwards

138
00:07:14,740 --> 00:07:18,850
relative to our object because we've rotated the object around.

139
00:07:18,850 --> 00:07:22,060
The object says, Well, my forwards is now this direction.

140
00:07:22,060 --> 00:07:25,600
If we rotate around a bit further says, Oh, my forward is that direction.

141
00:07:25,600 --> 00:07:30,880
That's kind of handy concept for us to have just so we can see which way our car is facing as we drive

142
00:07:30,880 --> 00:07:31,720
things around.

143
00:07:31,720 --> 00:07:36,190
So you can see I'm rotating things around here just by clicking and dragging, changing the number in

144
00:07:36,190 --> 00:07:37,180
here if you will change it.

145
00:07:37,180 --> 00:07:39,910
Say 45 bump puts on 45 degree angle.

146
00:07:39,910 --> 00:07:41,890
How do we do that in code?

147
00:07:41,890 --> 00:07:43,180
Let's return it to zero.

148
00:07:43,180 --> 00:07:44,890
Jump back over into our script.

149
00:07:45,040 --> 00:07:49,540
And in here I'm going to do this in code by using a method.

150
00:07:49,540 --> 00:07:49,900
Okay.

151
00:07:49,900 --> 00:07:50,470
Which method?

152
00:07:50,470 --> 00:07:55,810
Well, this method is going to relate to the transform component.

153
00:07:55,810 --> 00:08:02,050
So I'm going to start by typing in transform with a lowercase t dot.

154
00:08:02,050 --> 00:08:07,930
And now you see Visual Studio is saying to us, Hey, here's a whole bunch of things that you get if

155
00:08:07,930 --> 00:08:11,200
you're trying to use something within the transform class.

156
00:08:11,200 --> 00:08:13,750
So this is a class and we'll talk about classes more later on.

157
00:08:13,750 --> 00:08:15,280
But we're using the transform class.

158
00:08:15,280 --> 00:08:18,580
It's also relating to this transform component just here.

159
00:08:18,580 --> 00:08:22,870
So in transform component, we can do things to position things for rotation, things to scale.

160
00:08:22,870 --> 00:08:25,090
We want to do something to the rotation.

161
00:08:25,090 --> 00:08:29,980
And there's a method that comes with unity already, which is called rotate.

162
00:08:30,400 --> 00:08:35,320
So every time transform, rotate and methods need parentheses at the end of them.

163
00:08:35,320 --> 00:08:38,230
So these brackets, these round brackets are called parentheses.

164
00:08:38,230 --> 00:08:43,330
So transform, rotate, and then at the end is a semicolon.

165
00:08:43,330 --> 00:08:47,950
Now that's pretty good, but it's missing something because what it needs within these parentheses is

166
00:08:47,950 --> 00:08:51,460
it's saying, Hey, how do you want me to rotate?

167
00:08:51,460 --> 00:08:52,750
What do you want me to do?

168
00:08:52,750 --> 00:08:56,740
So this particular method is asking for some arguments.

169
00:08:56,740 --> 00:09:03,550
It wants to know how much you rotating and it wants to know how much we're rotating on the X, the Y

170
00:09:03,550 --> 00:09:04,420
and the Z.

171
00:09:04,420 --> 00:09:09,520
So in here we'll say, Well, for example, let's rotate nothing on the X, let's rotate nothing on

172
00:09:09,520 --> 00:09:14,470
the Y and let's rotate, say 45 on the Z.

173
00:09:14,470 --> 00:09:20,170
So that's us calling the rotate method that exists already in unity and putting that with inside the

174
00:09:20,170 --> 00:09:23,380
start method, which unity is calling all on its very own.

175
00:09:23,380 --> 00:09:25,300
It's saying, I'll do start when you push the play button.

176
00:09:25,300 --> 00:09:26,260
Rick No worries.

177
00:09:26,260 --> 00:09:27,640
And so let's save this up.

178
00:09:27,640 --> 00:09:29,230
Make sure you save you don't have a dot up here.

179
00:09:29,230 --> 00:09:31,150
You've got the X back into unity.

180
00:09:31,150 --> 00:09:35,410
And to see this more clearly, I'm going to grab my game window, hold my left mouse, button down,

181
00:09:35,410 --> 00:09:40,270
drag it over to the side so that we have the game window and the scene window open at the same time.

182
00:09:40,270 --> 00:09:44,530
That'll just allow us to see more things, just realign it a little bit, move the cameras around a

183
00:09:44,530 --> 00:09:44,830
little bit.

184
00:09:44,830 --> 00:09:45,400
All good.

185
00:09:45,400 --> 00:09:48,070
Now, when I click on play, what do we expect to have happen?

186
00:09:48,130 --> 00:09:53,410
Look, it's rotated 45 degrees and you can see our value up here is 45.

187
00:09:53,410 --> 00:09:56,410
So I'm going to click play to stop proceedings.

188
00:09:56,410 --> 00:09:57,910
So now we've done that in START.

189
00:09:57,910 --> 00:09:58,480
That's cool.

190
00:09:58,480 --> 00:10:02,830
The other thing I want to show you is we can put this exact same thing into update.

191
00:10:02,830 --> 00:10:07,300
An update is getting cold, like I said, every single frame.

192
00:10:07,300 --> 00:10:10,270
So let's highlight our transform rotate.

193
00:10:10,270 --> 00:10:15,760
I'm going to control X to cut that, I'm going to jump down into update, I'm going to control V to

194
00:10:15,760 --> 00:10:16,450
paste that.

195
00:10:16,450 --> 00:10:24,670
And this now will be rotating 45 degrees every frame that our computer is able to calculate per second.

196
00:10:24,670 --> 00:10:27,520
Let's save that back over into unity.

197
00:10:27,640 --> 00:10:29,740
Now, there's a little thing I want to show you here.

198
00:10:29,740 --> 00:10:34,990
It might be a little bit hidden on your game window, but if you drag your game window in and find stats,

199
00:10:34,990 --> 00:10:35,920
just click on stats.

200
00:10:35,920 --> 00:10:39,220
You'll see at the moment we're getting 743 frames per second.

201
00:10:39,220 --> 00:10:42,910
Well, you know, I've got a pretty quick computer, but the computer's not really doing anything at

202
00:10:42,910 --> 00:10:44,590
the moment, so don't celebrate too much.

203
00:10:44,590 --> 00:10:47,050
Let's click on play and we'll see what happens.

204
00:10:47,050 --> 00:10:47,590
Okay?

205
00:10:47,590 --> 00:10:49,870
Whoa, it's out of control at spinning.

206
00:10:49,870 --> 00:10:50,860
Really quick.

207
00:10:50,860 --> 00:10:51,520
We have.

208
00:10:51,520 --> 00:10:52,480
I'll just pause.

209
00:10:52,480 --> 00:10:58,810
You can pause by the little pause button next to the play button 337 frames per second that we are moving

210
00:10:58,810 --> 00:11:03,970
45 degrees, which is obviously really, really quick and a bit quicker than we want to.

211
00:11:03,970 --> 00:11:11,470
So let's jump back over into our script and we'll change from 45 to 0.1.

212
00:11:11,470 --> 00:11:17,710
And because we've got a decimal place in here and because this is a float, we need to put F at the

213
00:11:17,710 --> 00:11:17,920
end.

214
00:11:17,920 --> 00:11:21,820
So Unity knows, oh, it's a float and it doesn't get it confused with something else.

215
00:11:21,820 --> 00:11:24,460
Now let's save back over into unity.

216
00:11:24,460 --> 00:11:28,900
Click on the play button and now our object is moving at a more sensible pace.

217
00:11:28,900 --> 00:11:30,550
But we're using.

218
00:11:30,700 --> 00:11:35,350
A method to make our object rotate around, which is super cool.

219
00:11:35,350 --> 00:11:36,400
I'm going to play.

220
00:11:36,430 --> 00:11:40,290
I'm going to click on stats to get rid of that, put my window back in a sensible position.

221
00:11:40,300 --> 00:11:44,020
Just a reminder here, we've played around with START, we've played around with Update.

222
00:11:44,020 --> 00:11:49,240
Both of these are methods that are being called as callbacks by unity, and we ourselves are calling

223
00:11:49,240 --> 00:11:54,880
the rotate method and we're adding in these particular parameters to make our thing rotate around.

224
00:11:54,880 --> 00:11:55,470
So there we go.

225
00:11:55,480 --> 00:11:57,460
There's a quick introduction to methods.

226
00:11:57,460 --> 00:11:59,050
I will see you in the next lecture.

