S
S
Shizun2021-11-15 17:23:56
Unity
Shizun, 2021-11-15 17:23:56

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

2 answer(s)
N
Nekit Medvedev, 2021-11-16
@NIKROTOS

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.

L
LittleBob, 2021-11-16
@LittleBob

https://youtu.be/mjaoJ1rDoaQ
Here's a nice video. There is a check through the tags, you can choose for yourself what the earth is and what is not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question