Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.