- Home /
[2D] gravity independant jumping
Sup! I have the following code:
void Update() {
if(Input.GetKey(Keycode.W))
{
OnJump();
}
}
void OnJump() {
rigidbody2D.AddForce(new Vector2(0f, 300f));
}
It works fine until i double jump, i mean, when i press W, and then when i want to jump second time while object is falling, i don't actually jump that high as if i would jump first time. It's logical because force works against the gravity. yet i have no idea how to make jumping without adding force. Do you have any ideas? :(
You should be able to get around this by removing your rigidbody's Y velocity before adding the force.
void OnJump() {
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0f);
rigidbody2D.AddForce(new Vector2(0f, 300f));
}
Answer by RescuerRus · May 15, 2014 at 10:49 PM
ah,
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
did the trick.
Your answer
Follow this Question
Related Questions
Change rigidbody's jumping speed 2 Answers
Adding impulse force cancels out gravity force on collisions. 1 Answer
moving with rigidbody without acceleration 0 Answers
Character doesn't fall back down when jumping 0 Answers
AddForce Jump Problem 3 Answers