Player can jump infinitely when under collision

I’m working on a little 2d platformer game and i can’t figure out why the player can continue jumping when he’s under a collision with the same tag as the ground.
Here is the player collision and movement code:

    private int playerSpeed = 10;
    private int playerJumpHeight = 1000;
    private int playerDoubleJumpHeight = 800;
    private float moveX;
    public bool Grounded;
    public bool doublejump = false;
    private int jumps;
    public float distancetoground = 0.9f;

	void Update () {
        PlayerMove();
	}

    void PlayerMove()
    {
        //Controls
        moveX = Input.GetAxis("Horizontal");
        if (Input.GetButtonDown("Jump") && Grounded == true && doublejump == false)
        {
            Jump();
            jumps++;
        }
        else
        {
            if (Input.GetButtonDown("Jump") && doublejump == true)
            {
                DoubleJump();
                jumps++;
            }
        }
        
        
        //Animation
        if (moveX != 0)
        {
            GetComponent<Animator>().SetBool("isRunning", true);
        }
        else
        {
            GetComponent<Animator>().SetBool("isRunning", false);
        }

        if (Grounded == false)
        {
            GetComponent<Animator>().SetBool("isJumping", true);
        }
        else
        {
            GetComponent<Animator>().SetBool("isJumping", false);
        }

        if (jumps == 2)
        {
            GetComponent<Animator>().SetBool("DoubleJump", true);
        }
        else
        {
            GetComponent<Animator>().SetBool("DoubleJump", false);
        }

        //Player direction

        if (moveX < 0.0f)
        {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        else if (moveX >0.0f)
        {
            GetComponent<SpriteRenderer>().flipX = false;
        }

        //Physics
        gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
    }

    void Jump()
    {
        //Jump code
        doublejump = true;
        Grounded = false;
        gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(gameObject.GetComponent<Rigidbody2D>().velocity.x, 0);
        GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpHeight);
    }

    void DoubleJump ()
    {
        //Double jump code
        Grounded = false;
        doublejump = false;
        gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(gameObject.GetComponent<Rigidbody2D>().velocity.x, 0);
        GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerDoubleJumpHeight);    
    }

    private void OnCollisionEnter2D(Collision2D col)
    {
        //Ground check
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down);
        if (hit.collider != null && hit.distance < distancetoground && hit.transform.tag != "Ground")
        {
            Grounded = true;
            jumps = 0;
            doublejump = false;
        }
    }
}

And here is what the infinite jumping looks like:

In both cases i mashed the spacebar after jumping.
I should also include that both the ground and the blocks have the same “Ground” tag because they’re part of the same tilemap.

When your character collider hits a collider tagged “Ground”, you reset your jumping variables. It doesn’t matter whether you hit the ground or the ceiling - to the collider all that matters is that you’re hitting a “Ground” collider.

You can solve this by having two colliders on your character - one around its feet to detect when it’s on the ground, and one for the rest of the body to collide with walls and ceilings.

EDIT

Have you tried to Debug.Log the values your Raycast returns? Depending on your setting, your raycast may be hitting the platform above your head or return distance small enough to pass your check.

Hey there,

i’d recommend changing your OnCollisionEnter function completly. This function already provides you with all the information that should be needed. There is no need for a raycast.

So perhaps you should try something along the lines of this example:

private void OnCollisionEnter2D(Collision2D col)
     {
         //Check if whatever we hit was something tagged ground and if the contact point is above our character.
		 //this assumes that your character transform is somewhere in the middle or lower part of your sprite.
         if (col.transform.CompareTag("Ground") && col.contacts[0].point.y < transform.position.y)
         {
             Grounded = true;
             jumps = 0;
             doublejump = false;
		 }
         
	}

hope this points you in the right direction. No guarantuee for instant working of this, I did not specifically test this code.
I also assume that your up direction is the y Axis.