All language subtitles for 13. Code Cleanup

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

In this video we are going to clean up our code inside of our player class.

As of right now we've just finished the.

Player behavior for everything related to movement.

Now the core principle of object oriented programming is to effectively organize your code and be able

to efficiently debug your programs.

For example if we ever have anything wrong with our player movement we need an easy way to detect where

our player movement code is and fix it.

As you can see right now we're using the Update method which is our game loop and everything is being

called inside of update and we're going to be continuing to add core mechanics and features to our game

using the Update method.

What's gonna happen eventually is if we continue to just utilize the Update method we're gonna run into

a problem where it's very difficult to debug our programs.

So one of the core concepts of object oriented programming is to organize our code by doing that we

can create our own custom methods.

So for example void Update is automatically called from the model behavior on its own.

As long as it's attached to a game object this will run.

However what we can do is we can come down here and we can create our own method let's call this void

calculate movement

this method is going to be responsible for all things movement related.

I'm going to take the movement code that's inside of update.

I'm going to select it all and I'm going to right click or command see.

You can also just cut it if you'd like.

And I'm going to paste it inside of the calculate movement method.

All of our code is now inside of this method related to movement inside a method called calculate movement

if there's ever a bug or ever a problem with our movement code.

I know exactly where to look.

It's related to movement which means it has to be inside of this calculate movement method.

This method here can be called anything.

Void is just a default return type it means that the code runs from top to bottom and that's it.

Now unlike update this is only called when we call this method.

Update is automatically called once per frame or 60 frames per second.

We need to call this from update to call a function you simply say the function name and you initialize

it with parentheses.

So now an update we're calculating our movement every frame we're going to call all of this code.

However we just cleaned it up to where now our update is clean for other code that we're going to create.

And we've organized our movement code inside of here.

The last thing I'm going to do is I'm just going to clean up our code a bit and remove any of the pseudocode

that we have.

That is no longer needed.

When it comes to commenting your code you really only have to come in your code.

If you can't understand what's going on just by reading it I know that this is horizontal input vertical

input.

I don't need comments over it that tell me what that is the direction.

I know that this is calculating the direction translate.

I know that this is doing the translation.

For example here if transform not position that Y is greater than zero.

I don't need a comment anymore telling me what it's doing if I can read it in plain English what it's

doing.

So that's your rule of thumb for when to use comments.

If it's not clear at first glance what's happening.

Put in the comment OK.

So we now have our player movement here.

And one thing I want to show you guys if you're interested in clamping the movements for your y position

here like this.

There is one more optimized way to do that and it's using a math method in unity called clamping.

Right now I'm clamping the values between negative three point eight in zero and alternative method

to do that.

Other than using if l sifts here is that I could just say I could just say transform dot position equals

a new vector 3 and I'm going to pass in.

Let's say we don't care about the X so transform not position x and then I can say math f dot clamp

and math f that clamp allows us to clamp a value between a min and max the value that I want to clamp

is the transformed position about Y and the value for minimum is going to be negative three point eight

F and the max is going to be zero and then I can add a Z zero for Z with this line of code is doing

is it's going to clamp the transform position y between negative three point eight and zero eliminating

the need for this if else statement now I can't do that for the rapping because I don't want to clamp

it.

I need to make sure that I can go above it so that I can get the default or get the reverse of it.

So if I save this and hop back into unity and by all means you do not have to copy what I'm doing.

I want you guys to have your own unique implementations here.

I'm just going along and thinking of best practices that I think you should know in case in the future

you want to get more advanced you'll see here that I can now run the application.

I can still walk to the left and right.

So if I go all the way to the right I can walk to the left and if I go all the way down I can't go below

and if I go all the way up I can't go above.

So that inline piece of code there is clamping us.

All righty.

So our calculate movement is looking great.

Make sure you're calling calculate movement update and that's going to conclude everything related to

our player movement.

I'll see you guys in the next section.

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