title: Variable jump height in Unity jumping to random heights
body: <p>I'm trying to implement a variable jump height system in my game. I stop the jump early by multiplying the velocity by .5f whenever the player releases the jump button (space).</p>
  <p>This sometimes works, however sometimes it launches me up randomly when I lightly press space and let go as quickly as I can. It even jumps higher than the maximum jump height I get when I hold space.</p>
  <p>This has happened in a project of mine so I thought the project had something wrong with it, I ended up making a new empty project just to test this out. I have no idea whats wrong. I can also confirm it's not an issue with <code>onGround</code> because I tried multiple ground sensing methods, and I substituted <code>Input.GetKeyDown(KeyCode.Space)</code> with <code>Input.GetKey(KeyCode.Space)</code> and nothing changed.</p>
  <p>Here's the code :</p>
  <pre class="lang-cs prettyprint-override"><code>Rigidbody2D rb;
  [SerializeField] float force;
  void Start()
  {
      rb = GetComponent&lt;Rigidbody2D&gt;();
  }
  
  bool jump;
  [SerializeField] bool onGround;
  void Update()
  {
      if(Input.GetKeyDown(KeyCode.Space) &amp;&amp; onGround)
      {
          jump = true;
      }
  }
  
  private void FixedUpdate() 
  {
      if(jump)
      {
          //onGround = false;
  
          jump = false;
          rb.AddForce(Vector2.up * force, ForceMode2D.Impulse);
      }
  
      if(Input.GetKeyUp(KeyCode.Space) &amp;&amp; rb.velocity.y &gt; 0)
      {
          rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * .5f);
      }
  }
  
  private void OnCollisionEnter2D(Collision2D other) 
  {
      if(other.gameObject.CompareTag(&quot;Ground&quot;))
          onGround = true;
  }
  
  private void OnCollisionExit2D(Collision2D other) 
  {
      if(other.gameObject.CompareTag(&quot;Ground&quot;))
          onGround = false;   
  }
  </code></pre>
  <p>I have no other scripts attached. Thanks</p>