title: C# 2D platformer movement code
body: <p>This code will keep on jumping even though it is not on the ground how do you stop this (using Unity).</p>
  
  <p>The Code:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class PlayerController : MonoBehaviour 
  {
  
      //Movement
      public float speed;
      public float jump;
      float moveVelocity;
  
      //Grounded Vars
      bool grounded = true;
  
      void Update () 
      {
          //Jumping
          if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W)) 
          {
              if(grounded)
              {
                  GetComponent&lt;Rigidbody2D&gt; ().velocity = new Vector2 (GetComponent&lt;Rigidbody2D&gt; ().velocity.x, jump);
              }
          }
  
          moveVelocity = 0;
  
          //Left Right Movement
          if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) 
          {
              moveVelocity = -speed;
          }
          if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) 
          {
              moveVelocity = speed;
          }
  
          GetComponent&lt;Rigidbody2D&gt; ().velocity = new Vector2 (moveVelocity, GetComponent&lt;Rigidbody2D&gt; ().velocity.y);
  
      }
      //Check if Grounded
      void OnTriggerEnter2D()
      {
          grounded = true;
      }
      void OnTriggerExit2D()
      {
          grounded = false;
      }
  }
  </code></pre>