Jumping with rigidbody is random

Hi

I am trying to make a rigidbody player and I can’t get the jump right. I change the velocity of the rigidbody but the jump height is sometimes different. What am I doing wrong?

void FixedUpdate()
{

    float gravity = rig.velocity.y + extraGravity * Time.fixedDeltaTime;
    rig.MovePosition(rig.position + new Vector3(moveDirection.x, gravity, moveDirection.z) * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        if (isGrounded)
        {
            rig.velocity = new Vector3(rig.velocity.x, jumpSpeed, rig.velocity.z);
        }
    }
}

Any help is appreciated.

The only thing that sticks out to me here is the fact that you read input in FixedUpdate. GetKeyDown is true during 1 Update(), 1 Update can consist of 0-n FixedUpdates depending on framerate. Move the input reading if-clause to Update. It could be something else too in case you modify the physics values somewhere else (extraGravity etc.)