How i stop the player from infinite jump if touch the wall

Im making an 2D platform game . The player works fine and have dobule jump, but when it touches a wall and press “W” i can infinite jump untill i dont touch the wall. The player have a collider and rigidbody2d and the wall have a collider2d box.
Can someone help me ?

I know that can be hard to understand my code but its the first time when im making an game.

//Moving
public float speed;
public float jumpForce;
private float moveInput;
public int Health;

public Animator animator;

private Rigidbody2D rb;

private bool facingRgiht = true;

public bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;

public bool canJump = true;
private int extraJumps;
public int extraJumpValue;

void Start()
{
    extraJumps = extraJumpValue;
    rb = GetComponent<Rigidbody2D>();

}

void FixedUpdate()
{
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

    animator.SetFloat("Speed", Mathf.Abs(moveInput));

    moveInput = Input.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

    if (facingRgiht == false && moveInput > 0)
    {
        Flip();
    }
    else if (facingRgiht == true && moveInput < 0)
    {
        Flip();
    }

}
void Die()
{
    SceneManager.LoadScene(0);
}

void Update()
{

    if (Health <= 0)
    {

        Die();
    }
    if (isGrounded == true)
    {
        extraJumps = extraJumpValue;
    }

    if (Input.GetKeyDown(KeyCode.W) && extraJumps > 0)
    {

        rb.velocity = Vector2.up * jumpForce;
        extraJumps--;
    }
    else if (Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true && canJump)
    {
        rb.velocity = Vector2.up * jumpForce;
    }

}

void Flip()
{
    facingRgiht = !facingRgiht;
    /*Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
    transform.localScale = Scaler;*/

    transform.Rotate(0f, 180f, 0f);

}

public void Damage(int dmg)
{
    gameObject.GetComponent<Animation>().Play("Player_Flash");
    Health -= dmg;
    Debug.Log("Dmg Taken");

}

public void healthPickUp(int hlt)
{
    if (Health < 4)
    {
        Health += 1;
    }
}

public IEnumerator Knockback(float knockDur, float knockBackPwr, Vector3 knockBackDir)
{
    float timer = 0;
    rb.velocity = new Vector2(rb.velocity.x, 0);

    while (knockDur > timer)
    {
        timer += Time.deltaTime;
        rb.velocity = new Vector2(0, 0);
        rb.AddForce(new Vector3(knockBackDir.x * -120, knockBackDir.y + knockBackPwr, transform.position.z));

    }
    yield return 0;
}


void OnCollisionExit(Collision coll)
{
    if (isGrounded)
    {
        isGrounded = true;
    }
}

void OnCollisionStay(Collision other)
{
    if (other.gameObject.tag == "Platform")
    {
        canJump = true;
    }
    
}

}

Looking at your code, there’s 2 things I can see as the potential problem, or perhaps they’re both causing two separate problems that manifest the same way.


  1. You never set isGrounded to false. I assume you mean to do this on OnCollisionExit, as you’re checking if it’s true there and then setting it to true, which makes no sense. If isGrounded is always true, extraJumps will be set to extraJumpsValue every frame in update, meaning you can jump every frame without limit.
  2. (less likely) The wall might be tagged as a platform, so while touching it canJump is always true.