Character Jumping

Hello, I was wondering if there was a way to do jumping with a rigidbody. When I add a rigidbody to the player. When I click play, the player flies up into infinity and beyond. I am using this for the script so far :

var JumpHeight : float = 8;

function Update(){
if(Input.GetKeyUp("space")){
rigidBody.AddForce(Vector3.up * JumpHeight);
}
}

But this won’t work because like I said before, the player flies up into the sky as soon as I play. Thanks in advance if anyone can help! :slight_smile:

It sounds like your gravity might be inverted, or the rigidbody is interpenetrating into whatever it’s sitting on, causing physics to freakout when you hit play and fire it off as if the two are colliding.

As an experiment, raise your player above ground level to see if he ‘falls’ in the correct direction. Also, you might try adding another floating rigidbody to the scene to see if the same thing happens. Also also, ensure you don’t have any box colliders enveloping your character.

Gravity is set in Edit>Project Settings>Physics as a Vector3 at the top of the list of options. If it doesn’t read -9.81 then it’s been toyed with. It can read anything you want, of course, but if it’s a positive number then that’s why your character is taking flight.

Try with this. (my code is c#)

rigidbody.AddForce(jumpHeight, ForceMode.Impulse);

where jumpHeight is a vector3 (0, 12, 0)

You can play arround with the ammount to jump on the y-axis

also i recommend that you apply all your physics logic in the fixedUpdate() method instead of the normal update()

also you should probably check it on the Input.GetButtonDown(“Jump”) and set your jump in edit > input