title: How to make player stop infinite jump in unity 2d
body: <p>So I'm new to unity only 2 days, and I'm having this problem of my player jumping in air. I can keep clicking the spacebar key and my player will keep jumping and not stop, I'm trying make it so the player can only jump while on the ground not the air. I been trying a lot of tutorials  and most of them just made my game freeze up and not work anymore. I also been trying to find out how to use ground check but I'm still not sure on how to do that.</p>
  <p>This is my code that I have right know.</p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PM : MonoBehaviour
  {
      public float moveSpeed;
      public float jumpHeight;
      Rigidbody2D rb;
  
  
      void Start()
      {
          rb = GetComponent&lt;Rigidbody2D&gt;();
      }
  
      void Update() {
      float moveDir = Input.GetAxis(&quot;Horizontal&quot;) * moveSpeed;
      rb.velocity = new Vector2(moveDir, rb.velocity.y);
  
      // Your jump code:
      if (Input.GetKeyDown(KeyCode.Space))
      {
          rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
      }
     }
  }
  </code></pre>