Check if there is no collision

So I check if the player is standing on the ground before he can perform a jump. To do this I set a bool to see if he is colliding with ground tag

void OnCollisionEnter2D(Collision2D coll) 
		if (coll.gameObject.tag == "Ground")
			this.onGround = true;

When the player jumps I set onGround to false. When he lands this script above will set it to true again, everything is fine here.

My problem is when the player falls off a cliff, without performing a jump. The bool wont be set to true then, and the player is able to do a mid-air jump. What to do?

EDIT: isn’t there a way to check for collision in the frame where I want to jump? So check whenever “space” is pressed. This is probably what I should do with raycast, but is there another way to do it?

EDIT: Finally got it to work, by running the following script when player wants to jump. But I also had to put the Player-object into the Ignore Raycast layer.

public bool CheckOnGround()
		bool result = false;
		RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 0.35f);

		if(hit.collider != null)
			Debug.Log (;
			result = true;
		return result;

Instead use a raycast downward, if the ray hits, you are grounded else you are not.

I may be missing the compxelity of the problem here. But if you intend to use OnCollisionEnter why not just pair it up with an OnCollisionExit that sets the bool back to false?

This was the only way I could get it to work. Physics2d.Raycast always returned null. Hopefully you find this useful.

RaycastHit hit = new RaycastHit();
Ray ray = new Ray ();
ray.origin = transform.position;
ray.direction = Vector3.down;
if(Physics.Raycast(ray, out hit))
	if(hit.collider.tag == "Ground")
         //We are currently standing above the ground