- Home /
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! :)
Answer by iwaldrop · Apr 02, 2013 at 09:20 PM
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.
The vector 3 I had : x was 0 y was -1000 z was 0 I changed y to -1 And now it works, but the jumping doesn't. :(
When I play a game I expect to be able to smash a button and be able to hold it while jumping, but that's not going to stop it from working...
What might be wrong is the space being lowercase? I use c#, and is cs land you use a keycode for getkey. You might try using $$anonymous$$eycode.Space or "Space" ins$$anonymous$$d of "space".
Answer by AronChan · Nov 06, 2013 at 07:24 PM
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
Your answer
Follow this Question
Related Questions
Top Down car/vehicle movement 1 Answer
Jumping with rigid body3d 1 Answer
AddForce to a randomly selected GameObject with a rigidbody 0 Answers
Why is my rigidbody jump height different every time, despite being in FixedUpdate. 1 Answer
Why wont my character jump? (using rigidbody and addForce) 1 Answer