Answer the question
In order to leave comments, you need to log in
How to make a character touch the ground in Unity 2D?
I'm making an android game using Unity 2D. I have character jump code:
public void Jump(bool isJump)
{
isJump = groundCheck;
if (groundCheck)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "ground")
{
groundCheck = true;
}
}
Answer the question
In order to leave comments, you need to log in
It does not work, most likely, because initially the variable groundCheck
is equal false
in the declaration, you need to set it immediately true
, because initially the character is already on the ground:
And after the jump, immediately change to :
bool groundCheck = true;
if (groundCheck)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
groundCheck = false;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question