Double jump. What to use?

Hello,
I’m doing my first 3d game in unity, it’s a parkour game and I’m trying to make the player (a simple capsule) being able to double jump. I’m currently using “ForceMode.VelocityChange” , it’s working very well for the first jump, but for the second jump, depending of if the player is going up or down, it’s going really high or it just give a little impulsion. I want to know if there is an alternative script to use, so the jumps are constant.
Hope I’ve been clear!
thank you

VelocityChange is just that, a velocity change. That is an addition to the actual velocity. The resulting velocity is the sum of the actual velocity and the AddForce parameter. Achieving a specific velocity requires you to account for the actual velocity. For example (pseudocode):

AddForce(desiredVelocity - currentVelocity, ForceMode.VelocityChange)

thank you, sorry, I’m a beginner, this is my line of code currently:

rigidBody.AddForce(Vector3.up * jumpSpeed, ForceMode.VelocityChange);

“jumpSpeed” is the velocity that I want, what should I write?

thank you

try the following:

float yVelocity = Mathf.Clamp(rigidBody.velocity.y, rigidBody.velocity.y, 0);
rigidBody.AddForce(Vector3.up * (jumpSpeed - yVelocity), ForceMode.VelocityChange);

As Edy already explained, you’re just adding velocity. If your rigidbody is on its way down, adding upward velocity will just make it fall slower. But if you also account for the current y-velocity you can make sure, that the y-velocity of your character is zero so you can guarantee a certain y-velocity after you pressed jump. It’s just condensed to one line, but that’s conceptually what happens.

The clamp in the first line serves to avoid that when you double jump while your character is still on its way up, it flies way higher than expected. You can also just remove the clamp and replace by float yVelocity = rigidbody.velocity.y

1 Like