Jump not working properly sometimes.

Hello,
I’ve made my jump so that the jump power increases the longer you keep pressed the space button until the release. The thing is that sometimes when I let the key go it seems that the Input.GetKeyUp event is not captured and the player stays on the ground. Other times the event triggers itself while the button is pressed.

Does anyone know where could the problem be?

...
	public float jumpPower;
...

void FixedUpdate()
	{
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");
		Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
		//We increase the power over time
		if (Input.GetKey ("space")) {
			if (jumpPower <= 100.0f){
				jumpPower += 30.0f * Time.deltaTime;
			}else{
				jumpPower = 100.0f;
			}
		}
		//If on ground the ball can jump and moves quicker
		if (isGrounded ()) {
			if (Input.GetKeyUp("space")){
				movement.y = jumpPower;
				rigidbody.AddForce (movement * speed * Time.deltaTime);
				jumpPower = 20.0f;
			}else{
				rigidbody.AddForce (movement * speed * Time.deltaTime);
			}

		} else {
			rigidbody.AddForce (movement * airSpeed * Time.deltaTime);
			if (Input.GetKeyUp("space")){
				jumpPower = 20.0f;
			}
		}

	}

...

	bool isGrounded()
	{
		RaycastHit hit;
		return Physics.Raycast(transform.position, -Vector3.up, out hit, (float)(distToGround + rayMargin));
	}

As I said, it usually works fine, but it bugs me when it fails and I can’t figure out what’s the problem.

Input.GetKeyUp / Input.GetKeyDown are not meant to be used in FixedUpdate(). You have to use them in Update().

FixedUpdate(), as I understand it, waits for your computer to process before operating, if your KeyUp happens during this wait, it will be missed.

You could have simply answered your own question by looking up “GetKeyUp” in the API reference: Unity - Scripting API: Input.GetKeyUp