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
Now that we have user input working the next challenge is going to be restraining the player through
player bounds.
So let's check this out.
I can move around freely and if I move all the way to the right let's say here that I wanted to prevent
the player from moving any further.
And if I go all the way down I can prevent the player from going down or if you want to get really fancy
let's say I do move all the way to the right I wrap and appear all the way at the left in order to do
this we need to understand what's going on in our view let's also restrain us from going above let's
say zero on the y in order to do this.
We simply need to check if the player on the x position here is let's say greater than or equal to nine.
We can't move and if we're on the x position and we're less than or equal to let's say negative nine
then we can't move any further.
Or if you're all the way past let's say 11:00 you appear at 11:00.
And likewise if you're all the way at 11:00 you appear a negative eleven.
So we create sort of wrap.
Likewise we say on the Y if the y position is greater than zero then what do we do.
Well we need to set us back to zero.
And if the y position is less then let's say negative three point eight we need to set us back to negative
three point eight so that we can't move.
So in order to do this we have to work with what's called an if statement.
The next concept of fundamental learning C sharp and again check out the unity C sharp Survival Guide
for dozens of challenges on mastering C sharp.
So to do an if statement here what we need to do is we need to first type out the logic that we're trying
to achieve here.
I've gone ahead and I've cleaned up my script a bit.
I've just removed some comments and I'm going to continue to do that throughout the course just to clean
up our code.
Now here what I have is what is the logic we're trying to do.
Let's go ahead and restraints on just the top let's say I don't want to be able to go above zero on
the Y.
What is the logic in the pseudocode for this.
I recommend you always type out your pseudocode.
It's going to help you in the end become a better software engineer to type this out in pseudocode which
is just comments were basically checking if the let's go ahead and say here if player position on the
Y is greater than zero then we need to say here y position equals zero pseudocode.
If then logic that's all we're doing here.
This is called pseudocode where you type it out in English and then we're going to just convert it to
code and if statement is very similar to if then logic basically if some condition is true then we're
gonna do some logic.
A great example of this just to quickly test it is let's say here we had two variables FLOAT A equals
five AND FLOAT B equals 10.
If I wanted to check if five is greater than 10 which it's not.
How could I do that.
Well I could type out an if statement which looks like this as the syntax and I could say here if A
is greater than B that says if a which is five is greater than B then run this code.
Now the question here is is that a true condition is a greater than B is five greater than ten.
The answer is No.
Therefore this code never runs.
However if this was switched to where this is 10 and this is five is a greater than B Again it's 10
greater than five.
Yes.
So now this statement runs we use if statements to control logic and instructions for our program.
So what I wanna do here is I'm checking the player position on the Y.
How do I get the Y position.
Well if we look here in Unity I access the transform property the position followed by the y value.
And that's going to give me the Y position at the current time.
So I'm going to check if the y position of our player which is done through transform that position
don't y.
And I'm checking to see if it's greater than or equal to zero if it's greater than equal to zero.
I need to set my entire position on the y back to zero.
Now you might be thinking I could just say transform dot position dot y equals zero.
However we can't do this in C sharp you're not allowed to do this because we're neglecting the x and
z values.
What about them.
Are they just chopped liver.
No.
We need to acknowledge what happens to the x and z values.
We want them to stay the exact same.
Watch what happens here if I type transform not position equals let's say a new position.
And the reason why I no that is because transform our position it's taking in a vector 3 and I can say
get reset.
So I'm going to set it to a new position and I'm going to say 0 0 0.
By doing this.
Watch what happens.
I can go back into unity let it compile and I'm going to run the game.
And when I run this I'm going to snap to zero and I can move around I can move left and right up and
down I can still do everything but if I go to the top whatever happens I can go down I can go up and
I'm restrained everything's working.
But what happens now if I go all the way to the right and then up I snap to the center we have a bug
it shouldn't be doing that why is it doing that.
And the reason for that is because we reset the x and y positions to zero.
When really I wanted the x value to stay whatever the current X is I just wanted the Y to be zero.
So in order to fix this we have to tell the program to use our current x value.
And how do you get the current x value.
Well you say transform dot position dot X and that's going to say use the current x position whatever
it is set the Y to Z set the Z to Z or zero by running the application again you'll see here that I
can run this and no matter where I am on the x axis we will always be restrained to zero on the Y.
And now I can move along but I can't go above.
So now let's take a look at how to add the bottom now.
In unity we have if statements.
There's also something called an L statement which means that if this is not a true condition it will
default to an else.
So for example if chance went on that position that Y is greater than equal to zero is false.
This will run.
We also have something called in.
Else if and else if statement in unity or in C sharp in general basically is checking the same condition
I'm checking here.
If the Y position is greater than 0 else if the y position is less then three point eight I want to
do the same thing we use else ifs if we're checking the exact same property which we are in this case.
So here in pseudocode we would type I'm just gonna remove that if player position on the Y is greater
than zero Y position equal zero.
Else if position on the Y is less than negative three point eight F because we're working with decimal
values then what needs to happen y pose equals negative three point eight F so to convert that to actual
code we would say here if and then else.
So if chance when opposition Y is greater than equal to zero else if transform not position not y we
only use the elses if we're checking the same condition here we check chance on our position not Y.
And here we check transient opposition not Y.
And now I'm checking if it's less than or equal to negative three point eight F and if it is then what
are we going to do.
If it is less than negative three point eight F we're simply going to take the current position and
assign it a new position.
I want to keep the current x position and then I want to assign a negative three point eight F on the
Y let's save this and lets us it out
will run the application start unity and you'll see here that I am now fully restraint from the top
of the screen.
I can't go above it and I can move freely on the X and I can't go below it.
We've now restricted our player on the vertical axis as a challenge.
I'd like you to go ahead and fully restrain the player on the left and as a bonus go ahead and make
it so that when the player is all the way past the bounds they wrap to get you started.
We'll go through the pseudocode together.
We're going to use a new if statement because we're checking a new condition.
I'm no longer checking the wire I'm now checking the X so here.
If player on the x is greater than let's say 11:00 then X pose is going to equal negative negative 11.
So if you go all the way to the right I want to appear on the left and then elusive player on the X
is less than negative 11.
Then why do we need to do X pose equals positive 11.
So good luck.
Give it a shot and I'll see you in the challenge review.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.