How to only jump when grounded

So i have this basic 2D player code that can jump and move horizontally but the jump can always be initiated, even when already in air. How do i fix this? Code here:

using UnityEngine;
using System.Collections;

public class  Player : MonoBehaviour{

public float moveSpeed = 10f;
	public float jumpSpeed = 10f;

	void Update () {
	float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
		if (Input.GetButtonDown ("Jump")){
			rigidbody2D.velocity = new Vector3(0, jumpSpeed, 0);
		transform.Translate (new Vector3 (h, 0));


I usually create an enumerator to know the different states of the gameobject. Something like this:

public enum State{
public State state;

And then:

void Update(){
    if (Input.GetButtonDown ("Jump")) Jump();

void Jump(){
    //Do whatever you want to jump now
    state = State.jumping;

void OnCollisionEnter(Collision collision){
    if (collision.tag == "Ground" && state == State.jumping){
        state = State.normal;

This requires you to tag the ground as a “ground”, and of course add a collider to both your player and the ground.

Alright you will need to use the function OnCollisionStay within your code. Along with contact points (to tell if your characters “feet” are touching the ground). So you will need a boolean within the method:

  void OnCollisionStay(Collision c){

      // Visualize the contact point
	Debug.DrawRay(c.point, c.normal,;
    //check if the contact point was below an amount of the rigidbodies origin
    if(c.point < 0.5){ //you might use something else here? I would look into it.
    allowJump = true;
    } else{
    allowJump = false;

You would of course need to create the var allowJump as a global variable and then within your if(Input.GetButtonDown(“Jump”)) you would want to have if(Input.GetButtonDown(“Jump”) && allowJump == true)

Note I have not tested this out so it might need a fair amount of tweaking before it works. If you have any questions about it let me know.