All language subtitles for 4. Movement & Time.deltaTime

af Afrikaans
ak Akan
sq Albanian
am Amharic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1 1

[MUSIC] 2

2

In this lecture, we're gonna make our player move up, down, left, right. 3

3

We'll be introducing the concept of Time.deltaTime, so 4

4

that we can make our game frame rate independent. 5

5

Okay, let's make our player ship move. 6

6

First thing, I'm going to do is click on my player and add a player script. 7

7

So I clicked on my hierarchy on the player, game object. 8

8

And I will now in the [INAUDIBLE] click on add component. 9

9

We will call this player, I've already got it typed in there. 10

10

And then click on new script, click Create and add. 11

11

That will give us a new script. 12

12

Over in my project folder, I will. 13

13

Do I have a script done? 14

14

I don't have a script folder yet. 15

15

Okay, right click Create Folder. 16

16

We'll call this script, there were go. 17

17

Drag the player script into the scripts folder. 18

18

Double click on that and open up player. 19

19

And that should open up Visual Studio for us, if I can click on it correctly. 20

20

Sometimes unity takes a little bit of time to think. 21

21

Need that half a second where its like hang on Rick. 22

22

Just slow down Rick, calm down, hold your horses, I'm doing some stuff. 23

23

Give it a minute, and then I need to click on it again. 24

24

Here we go,okay. 25

25

Now, I'm in player.cs, excellante. 26

26

Now, we're going to do something to make the player move. 27

27

And movement needs to be happening on Update. 28

28

Cuz we want it to be listening our input every frame. 29

29

Every single frame we want to have the ability to say, move up and left and 30

30

right and down. 31

31

So I will start by creating a method, Move ();. 32

32

That doesn't exist yet, so we're getting the red squiggly line. 33

33

Let's give ourselves a little space down below, and this will be a private void, 34

34

so there's no return type. 35

35

Call it Move. 36

36

Whoops, auto-complete thought I was trying to do something else. 37

37

Move, and then open and close our squiggly braces. 38

38

Our curly braces is probably the better term. 39

39

Now what do we need to do in here? 40

40

Well first of all, we want to create 41

41

this concept that there will be a new position that we're moving to. 42

42

So I'm gonna write in here var new nexXpos, 43

43

short for position. 44

44

We can write the whole thing, 45

45

position but pos is one of those things that we know it means position. 46

46

And instead of next I'm saying new. 47

47

There we go, lots of typos there, sorry, new x pos. 48

48

And you might say, well why is this a var? 49

49

I'll get to that in a moment. 50

50

Okay, equals, transform, 51

51

dot position .x so we're doing this on the x axis at the moment. 52

52

Just the x axis and we're saying where do we want to be on the x axis? 53

53

And we're saying it is where we currently are plus something. 54

54

And that plus something is going to be some sort of change so 55

55

we'll call this deltaX. 56

56

Delta referring to change or difference, or 57

57

distance between where we currently are and where we want to be, and semicolon. 58

58

Okay, so there's our first line. 59

59

It doesn't know what deltaX is, so we need to create that as well. 60

60

I'm gonna type in var again, again, I'll get back to var in just a moment. 61

61

Have a little chat about that. 62

62

deltaX, so the change equals, this is gonna be cool here. 63

63

We're gonna use Input, .GetAxis, and 64

64

we haven't used GetAxis before, and then we wanna know which particular axis. 65

65

So waht is the axis, and where do we get it from, and why are we using it? 66

66

Let's jump back over into Unity. 67

67

If you go up to Edit, Project Settings, and find Inpu, 68

68

you'll see we have the InputManager. 69

69

It might be closed up for you, you need to open up the Chevron thingy. 70

70

And you can see a number of different elements, a number of different aspects, 71

71

axes in our InputManager. 72

72

We have the Horizontal and we have the Vertical. 73

73

So in the past, in one of our previous sections, 74

74

in here we would have said Input.GetKeyDown, for example. 75

75

That's another, GetKeyDown, 76

76

it's another method that we can use within the Input class. 77

77

And GetKeyDown, we would say specifically what particular keys. 78

78

So it might space or we might say, a, or we might say, b. 79

79

I'm gonna undo that back to get axes. 80

80

Instead of doing that, we can just go and say let's go and use the horizontal axes. 81

81

The horizontal axes relates to the x coordinates. 82

82

And a negative is going to be negative x, and a positive is going to be positive x. 83

83

So basically, that's gonna be left and right, 84

84

because we know negative x heads to the left and positive x heads to the right. 85

85

And the negative button in this instance is left on the keyboard and 86

86

the positive is right. 87

87

Also there's an alternative negative button which is AND. 88

88

So if you wanna use WASD to control your game, or the cursor arrows, 89

89

the up, down, left, right, you can do both of those within here. 90

90

A couple of other settings, 91

91

we're not gonna play around with too much at the moment, gravity, 92

92

the dead zone, sensitivity, etc. 93

93

Sometimes a little bit more relevant for keyboard. 94

94

The cool thing is if you scroll down a little bit, 95

95

you'll see down at the bottom there's another horizontal. 96

96

Which is set up for the joystick but what I reallly wanted to show you, is we can 97

97

just used one aspect that we print our code, which we're gonna call horiztonal. 98

98

And then system knows that all of these different things can be 99

99

referring to horiztonal. 100

100

And if you want to change it, 101

101

then you can change it here in your in your inspector nice and easily. 102

102

Rather than having to go into your code and find your particular spot, where 103

103

we're referring to the left key or the right key or whichever key you're using. 104

104

So I'm gonna copy horizontal, back over into my player script. 105

105

in here, where we say input.getaccess open parenthesis and do our quotation. 106

106

And then paste in horizontal, or you can type it but 107

107

pasting it means we get it We know we get it correct for sure, and then semi colon. 108

108

So what we've created here is a var delta x, and 109

109

we have a squiggly line here at the moment. 110

110

I'm not happy, I'll get to that in a second. 111

111

Input dot GetAxis("Horizontal"), and the reason we got a red, 112

112

squiggly line, it's not happy that we're using it before we've declared it. 113

113

So we're gonna put this line, 114

114

I just wanted to do it this way around from a learning point of view. 115

115

So you could see this one first, that one second, but I will now put this up above. 116

116

So we're declaring what deltaX refers to, and 117

117

then we're using that deltaX in our newXPos. 118

118

So in other words, where we want our ship to be. 119

119

So I mentioned the var within our local variable here, we sometimes use var. 120

120

Or we could type in float, that would be just fine. 121

121

Because this is of type float, if I undo it. 122

122

If you mouse over delta x, 123

123

it shows you that it's a local variable with type float. 124

124

So in instances, in situations where Unity knows, that our Visual Studio knows that 125

125

this particular variable, because what follows it must make it a float. 126

126

Then it's okay for you to say var and it knows it's a float, 127

127

it converts it automatically. 128

128

So to move, you might know this already. 129

129

If you know this, pause the video and jump in and give this a try. 130

130

Otherwise, we're going to type in transform.position, 131

131

you guessed it, equals. 132

132

Well we need to use our new keyword to create a Vector2. 133

133

We're working in Vector2 in this game. 134

134

You could type Vector3, and then it would want to know X, Y and Z. 135

135

But we're not working with Z in this section of the course. 136

136

So we're just, Using Vector2. 137

137

And then what'sthat going to be? 138

138

Well, it's looking for an x and a y. 139

139

So float x, float y. 140

140

What's our float x? 141

141

Float x is going to be the new x position. 142

142

So this new x position. 143

143

And the y for now we're going to say is 144

144

transform.position.y; to round it off. 145

145

So we're saying that we want to get the change, this is the change 146

146

based upon what the player is gonna be doing that we're calling each frame. 147

147

And then that will allow us to say, what's the new position that we want the ship to 148

148

go to, which is the current position plus the change. 149

149

And then we need to say, well your position, your actual position, 150

150

this frame should be this new Vector2. 151

151

Which is going to be the x, which is our new position and then the y, 152

152

transform.position.y, is just saying stay where you are right now on the y. 153

153

Okay, now let's save that, come back over into Unity, click on Play. 154

154

Not on Pause, Rick, click on Play. 155

155

I'm having a bit of trouble with this today, aren't I? 156

156

Click on Play, okay, and then left and right, wow, fantastic. 157

157

No, up and down, just left and right. 158

158

This is going very fast, so what do we need to do next? 159

159

Well, next, what we need to do is something that, 160

160

I'm going to introduce a new concept for you here. 161

161

And I've got some documentation to show you. 162

162

It's going to be time dot delta time. 163

163

So very quickly I've also given you a link to input, 164

164

if you want to know more about input and all the different properties and 165

165

all the different methods that the input class has. 166

166

Also into the Input Manager you can read more about that, if you'd like. 167

167

We're looking at Time.deltaTime, and I've got a couple of slides on this. 168

168

But the summary of why use Time.deltaTime is to make our game 169

169

frame-rate independent. 170

170

That means that no matter how fast or 171

171

slow your computer runs, you will get the same experience. 172

172

So let's have a look at some slides on that. 173

173

So on Update, which we know is each frame, so every frame of our game. 174

174

We move the whatever, in this case our player ship, 1 unit to the left. 175

175

That's what we're saying in this particular example. 176

176

We're saying, On Update, move 1 unit to the left. 177

177

And on a slow computer, for example, this is just a guess. 178

178

We might be getting ten frames per second. 179

179

So the computer is chugging away saying [NOISE]. 180

180

Each second it manages to calculate through ten frames worth of computer. 181

181

On a fast computer, it can calculate through a lot more frames. 182

182

It can render more frames. 183

183

Or it can process what we're trying to get it to do. 184

184

So, it might be getting 100 frames per second. 185

185

The problem varies if we saying on Update move 1 unit, then on the Slow Computer, 186

186

that's 1 unit x 10 Frames per second, the Distance per second will be 10 units. 187

187

And on the Fast Computer, it's gonna be 1 unit per frame to the left, 188

188

x 100 frames per second, which is 100 frames. 189

189

So you get a very different experinece on a slow computer than on a fast computer. 190

190

Now, what do we do about this? 191

191

Well, when we look at the duration of the frames on a slow computer, 192

192

if we're getting 10 frames per second, then each frame is 0.1s. 193

193

10 frames per second, that means each individual frame is .1 of a second. 194

194

And our example with the Fast Computer, if we're getting 100 frames per second, 195

195

then each frame is taking .01 second to run. 196

196

So every point every .01 of a second there's another frame, and 197

197

another frame, and another frame. 198

198

So the distance per second, if we were to multiply the 1 unit per frame 199

199

times by the 10 frames per second that our computer is capabale of running, 200

200

times by the duration of each frame, which is 0.1, we get a value of 1. 201

201

And that's exactly the same if you use the same calculation one frame to the left 202

202

per, so one unit per frame times by 100 frames per second, 203

203

times by 0.01 that it takes to run each frame, then we get a value of 1. 204

204

So you can see formula, gives us the same result on both computers. 205

205

So then we can use Time.deltaTime because using 206

206

Time.deltaTime Unity can tell us how long each frame took to execute. 207

207

We don't need to sit there with a stopwatch, 208

208

cuz we don't know how to do that. 209

209

It'd be impossible to measure the frames. 210

210

Unity knows exactly how long the last frame took to execute it. 211

211

Knows that it's keeping track of that so we can use that. 212

212

And so when we multiply something by Time.deltaTime it makes our game frame 213

213

rate independent. 214

214

So if I scroll back up here ,where the duration of the frame is 215

215

what we're getting by using time.delta time. 216

216

Time.delta time means how long did it take for that frame to execute? 217

217

Okay, and 218

218

so the end result is the game's behaving the same on fast and slow computers. 219

219

So back over into our game, let's go back to play.cs. 220

220

And to make this super clear, I'm just going to print, so debug.log, 221

221

Not quotation marks, thats if were doing a string, deltaX. 222

222

So we will just print this variable and see what it prints out for 223

223

us when we're moving. 224

224

So back over into Unity, you don't need to do this, this is just for 225

225

illustration purposes. 226

226

Play the game and then as I move to the left you can see the value. 227

227

I'm going to go off the screen. 228

228

If I hold down to the right, it goes all the way up to one. 229

229

Hold down to the left it goes all the way to minus one. 230

230

And there's a ramp up in between. 231

231

Cuz that's what happens, so when we jump back into our code here. 232

232

We're saying that as we're pushing this Horizontal, this value here, 233

233

this deltaX, we're getting the axis, it's going from minus 1 up to 1. 234

234

So we're gonna times this, this is where we want to be framerate independent. 235

235

We'll times this by Time.deltaTime. 236

236

Okay, that is making this framerate independent. 237

237

So I'm just going to get rid of this debug.log, don't need that anymore. 238

238

Save up, back over into Unity. 239

239

Now it's going to give us a very interesting result. 240

240

Not exactly what we want for our final game, but it's a good result. 241

241

So we'll move our ship to the left, [SOUND] very slow. 242

242

And to the right, very slow as well. 243

243

But this would be exactly the same on every computer. 244

244

This would be the exact same slowness of speed, which is great, that's a big win. 245

245

And the last step we need to do in here is to get the actual speed that we want. 246

246

We need to be able to tune it, cuz that's way too slow. 247

247

So I'm gonna create myself a new variable. 248

248

That I will call what SerializeField first of all, before I call it anything. 249

249

It's going to be a float and I'll call this moveSpeed and 250

250

we will initialize that at 10. 251

251

And it's going to be a float so we will be a f on the end of 10. 252

252

And now down here now in our delta so what is the change we say times by 253

253

Time.deltaTime times my moveSpeed, cuz we're wanting to speed it up. 254

254

Save, jump back over into Unity, 255

255

you can see if I click on my Player after it thinks, here we go. 256

256

Our move speed here is ten. 257

257

If I click on play we'll see how good that feels at the moment. 258

258

Might have got it right the first go. 259

259

Left, right, okay that's pretty good. 260

260

We can tune it a bunch when we've got more stuff in our game. 261

261

But for now that's feeling In the right ballpark. 262

262

And we can very easily increase this to 20 or decrease it to 5, 263

263

if we wanna double or half the speed of the player. 264

264

Okay, so that's us moving left and right. 265

265

We're using Time.deltaTime. 266

266

We're getting the axis, which is Horizontal. 267

267

And I have a challenge for you. 268

268

The challenge is to create vertical movement. 269

269

So allow the player to move up and down as well as left and right. 270

270

Basically, by replicating what we did just now for the horizontal axis. 271

271

Do the same thing for the vertical axis. 272

272

Okay, so jump in and take on that challenge and 273

273

I'll see you back here in a moment. 274

274

Okay, so we're gonna keep this really simple. 275

275

I'm just going to highlight my first two lines, copy, and 276

276

then paste them underneath the previous two lines. 277

277

And go through and change the reference to from X to Y, 278

278

and from our axis of Horizontal to Vertical. 279

279

And then for our new X position is actually our new Y position. 280

280

And then transform.position.y and deltaY. 281

281

And then the last step here in our transform.position, 282

282

the new Vector2 is newXPos, newY Pos, okay. 283

283

So let's give it a nice and simple, just we've got the same thing for 284

284

horizontal that we're now doing for the vertical. 285

285

Back over into Unity, click in play. 286

286

If all's gone to plan we can fly up, down, left, 287

287

right Okay, up down, left, right, cool. 288

288

So we've got movement in our game, the player can move around. 289

289

I can go off the screen, so 290

290

that's something we'll need to resolve pretty soon. 291

291

But we do have movement. 292

292

And this is framerate independent, which is very, very excellent. 293

293

It also means you can plug in a joystick, and you 294

294

could do exactly the same thing, without having to go and write additional code. 295

295

It just knows immediately, that horizontal and vertical maps to your joysticks or 296

296

your joypad. 297

297

So great work, well done and I'll see you in the next video.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.