- Home /
2D Double Jump velocity affected by first jump
Hey guys, I downloaded the Sample Assets beta, which has a jump script that looks like this:
// If the player should jump...
if (grounded && jump) {
// Add a vertical force to the player.
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0f, jumpForce));
}
}
And I personally added the following to add a double jump:
else if (grounded == false && jump && doubleJumpCount == 1) {
rigidbody2D.AddForce (new Vector2 (0f, doubleJumpForce));
doubleJumpCount = 0;
}
if (grounded) {
doubleJumpCount = 1;
}
}
Now first off, I'm kind of a newbie so I'm fairly proud this worked out in general. But is this clean code or is there a better way to do this?
Now, to my actual question: Currently the second jump is affected by the first jump's velocity. So if I double jump really quick in succession, the character goes a lot higher than when i jump, wait a bit, and jump again.
How can I change this so when the double jump button is pressed, the character basically loses all the momentum he has and the double jump always has the same height? I tried adding
rigidbody2D.AddForce (new Vector2 (0f));
Into the double jump function, but got an error.
Thanks in advance!
Answer by rutter · Jul 10, 2014 at 10:27 PM
That looks mostly fine, aside from the problem you mentioned.
One important thing to understand about vectors, such as velocity: they have one component per axis (in your case, X and Y), and each axis is fully independent.
For example, if you want to assign only the player's vertical speed:
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 20f);
Or, if you want to reset horizontal speed, too:
//the following are equivalent
rigidbody2D.velocity = new Vector2(0f, 20f);
rigidbody2D.velocity = Vector2.up * 20f;
If you want, you could limit the player's double jump timing a little bit; maybe they can only jump at the "peak" of their first jump? To do that, we need to understand a little bit more about velocity.
The y-component of velocity describes vertical motion. If it's positive, the character is moving up; if it's negative, the character is moving down. And if it's zero, or near zero? The character isn't moving up or down.
So, we can tell if someone's near the peak of their jump:
If they jumped (ie: they're not grounded)
Their velocity's y-component is near zero (at the peak of a jump, they "hold still" briefly)
In code, that might look like:
float doubleJumpDifficulty = 0.2f; //max speed for double jump
bool canJump = grounded || (doubleJumpCount > 0 && Mathf.Abs(rigidbody2D.velocity.y);
bool jump = ??? //(this looks like your key press check?)
if (canJump && jump) {
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 6f);
}
If you want to be slightly more forgiving, you could allow a second jump any time the player is falling (maybe they walked off a platform?). Any mechanic you can describe mathematically is within your reach!
Thanks a lot for the thorough answer, I got exactly what i was looking for! :)
Answer by screenname_taken · Jul 10, 2014 at 10:30 PM
Try to zero out the velocity with rigidbody2D.velocity=Vector2(0,0); then jump.
Hey, thanks for the answer. It worked, but without the other comment I wouldn't have known why (vertical and horizontal velocity)
Answer by unknownsk · Jul 18, 2020 at 02:51 PM
thans guys what you need to do is set velocity to 0 when jumping for second time:
void jump () {
//secondjump
if (candublejump) {
rigidbodyplayer.volocity = new vector2 (0f, 0f);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613153746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
NullReferenceException: UnityEngine.Rigidbody2D.get_velocity() 0 Answers
error CS0019: trying to increase velocity when key is pressed twice in quick succession 1 Answer
Velocity Behavior Question 0 Answers
Projectile Trajectory Velocity, with predefined Start and End Positions 0 Answers