Question by
fabian_rensch · Dec 27, 2015 at 09:46 PM ·
c#velocityplatformerdouble jumpzero
[Solved] Zero out Velocity before double jumping?
Hey there. I'm working on a platformer and I try to implement a double jump. It all works fine but when I double tap the jump button quickly the character jumps way higher than it should. I guess it's because the velocity of the double jump is added to the normal jump. That's why I want to zero out the velocity right before the double jump. My code looks like this:
if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.W)) {
Vector2 velo = myRigidbody.velocity;
if (isGrounded) {
velo = new Vector2(velo.x, 0);
myRigidbody.AddForce(new Vector2(0, jumpForce));
canDoublejump = true;
} else {
if (canDoublejump) {
canDoublejump = false;
velo = new Vector2(velo.x, 0);
myRigidbody.AddForce(new Vector2(0, jumpForce));
}
}
}
But this won't work. Should I zero out the physics instead? Or is there another easy approach?
Thanks.
UPDATE
Solved it by myself:
Instead of
Vector2 velo = myRigidbody.velocity;
velo = new Vector2(velo.x, 0);
I wrote
Vector3 vel = myRigidbody.velocity;
vel.y = 0;
myRigidbody.velocity = vel;
Comment