Answer the question
In order to leave comments, you need to log in
What to do if the character jumps from the walls?
Hello! I ran into this problem: when the character is on the ground - IsGrounded works and he can jump, but he can also jump when he touches the wall (just jumps up the wall until you stop pressing the space bar). It is necessary that IsGrounded work only when in contact with the ground.
public float JumpForce = 1f;
public bool IsGrounded = false;
public float checkGroundOffsetY = -1.8f;
public float checkGroundRadius = 0.3f;
void Jump()
{
if (IsGrounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(transform.up * JumpForce, ForceMode2D.Impulse);
}
}
void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(new Vector2(transform.position.x, transform.position.y + checkGroundOffsetY), checkGroundRadius);
if (colliders.Length > 1)
{
IsGrounded = true;
}
else
{
IsGrounded = false;
}
}
private void FixedUpdate()
{
CheckGround();
}
private void Update()
{
Jump()
}
Answer the question
In order to leave comments, you need to log in
try checking the ground against a tag, a component, or whatever you can tell it from a wall. The check should be during the jump.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question