So I just got into unity when I stumbled upon this problem.
I followed Brackeys code tutorial on player movement when I realized that the player can jump as much as they want.
So I tried to make a isgrounded boolean but I can never find the correct way to implement it into my codes.
I want to make a code if (Input.GetButtonDown(“Jump”)) && isgrounded = true;
{
jump = true
isgrounded = false
}
But every time I use the &&, it said that it’s not possible or something along those lines. I know that it’s the basic fundamental to know about this but I can’t seem to figure it out.
Any help would be appreciated and sorry for my lack of English vocabulary, I am still learning so excuse me if I make any mistakes.
4822511–462497–PlayerMovement.cs (910 Bytes)
Note this is misunderstanding the C# language and not misunderstanding Unity.
Three problems:
The expression should all be within the brackets so “if (SomeExpression)”
= is used to assign a value not compare equality. You should use “==”
; (semi colon) at the end of that line isn’t supposed to be there.
So you should end up with:
if (Input.GetButtonDown("Jump") && isgrounded == true)
{
jump = true;
isgrounded = false;
}
It’s also a minor thing but you can implicitly check if something is true/not null by doing this subtle but sometimes easy-on-the-eyes code:
if (Input.GetButtonDown("Jump") && isgrounded)
{
jump = true;
isgrounded = false;
}
2 Likes
When you use Code Tag it would be easier to see your code.
You first IF statement linę is incorrect.
You need put first condition && and second condition in same brackets. For example
if ( conditionA >= 0 && isConditionBTrue )
{
// some code
}
Edit:
Sorry I writing from mobile.
1 Like
MelvMay:
Note this is misunderstanding the C# language and not misunderstanding Unity.
Three problems:
The expression should all be within the brackets so “if (SomeExpression)”
= is used to assign a value not compare equality. You should use “==”
; (semi colon) at the end of that line isn’t supposed to be there.
So you should end up with:
if (Input.GetButtonDown("Jump") && isgrounded == true)
{
jump = true;
isgrounded = false;
}
It’s also a minor thing but you can implicitly check if something is true/not null by doing this subtle but sometimes easy-on-the-eyes code:
if (Input.GetButtonDown("Jump") && isgrounded)
{
jump = true;
isgrounded = false;
}
Ty for the help, I really appreciate this!
Antypodish:
When you use Code Tag it would be easier to see your code.
You first IF statement linę is incorrect.
You need put first condition && and second condition in same brackets. For example
if ( conditionA >= 0 && isConditionBTrue )
{
// some code
}
Edit:
Sorry I writing from mobile.
So I need both conditions in a same bracket like this
if (Input.GetButtonDown(“Jump”) && isgrounded = false; )
Or am I missing the point here?
Sorry for this but I don’t get the point your are telling me…
Can you explain it a little more details?
I gave you an example of the correct code above, simply look at the difference if you don’t follow my explanation. If I explain in more detail it seems it’ll make things more confusing for you.
1 Like
MelvMay:
I gave you an example
Melv explained you best way.
You need this
if (Input.GetButtonDown("Jump") && isGrounded == true)
or its simplified form
if (Input.GetButtonDown("Jump") && isGrounded )
meaning
isGrounded is same as isGrounded == true.
Also, in comparison loops, you don’t use normally single equal sign.
PinkyP17:
isgrounded = false
This is wrong.
use double equal sign for comparison
isgrounded == false
Singe equal sign is for assignment, like a = 2.
Also, you don’t put semicolon inside if loops. This is wrong ( see ; character after false ).
Corrected with if statement is as following
if (Input.GetButtonDown("Jump") && isgrounded == false )
2 Likes