IsGrounded does not work

Sooo, I am trying to check if my 2D “Player” is grounded or not.

Following code excerpts:

	// Use this for initialization
	void Start () {
        float distanceToGround = GetComponent<Collider2D>().bounds.extents.y;
	}

And the isGrounded function is following:

    bool isGrounded()
    {
        return Physics2D.Raycast(transform.position, -Vector2.up, distanceToGround + 0.1f);
    }

But it is telling me 24/7 that distanceToGround does not exist in the current context…

My player is using a polygon collider2d and a rigidbody 2d.
I just want to prevent my player from jumping while he is “jumping”/not grounded…

Greetings

Could you look to see if velocity is not equal to 0?

You’re declaring distanceToGround inside Start(), which means that the function isGrounded() doesn’t know about it. What you want is:

float distanceToGround;

// Use this for initialization
void Start () {
    distanceToGround = GetComponent<Collider2D>().bounds.extents.y;
}

bool isGrounded()
{
    return Physics2D.Raycast(transform.position, -Vector2.up, distanceToGround + 0.1f);
}

distanceToGround will only be accessible in the Start() function since that is where it is decleared.

Declear it at the top of your script outside of the start function.