Hi, i’m creating a siple platform game with the possibility to do a double jump but i’ve a problem. Once i’ve clicked the jump button i’ve to re-click it to double jump but, the faster i re-click, the higher is the jump.
Any suggestion?
Here is the code ( is quite simple and simile to the one in the tutorial)
I’m not a big fan of using forces in a character controller, since they never give me results that I can easily control.
What I do is set velocities instead:
// Whenever you want to jump
rigidBody.velocity += Vector3.up * jumpSpeed;
Unrelated, but worth thinking about: if there’s ever a situation in your game where you press the jump key but your character stays on the ground (e.g. low ceilings), things might break if you set jump flags.
If you want jumping to be a constant thing then I would recommend changing the following line:
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));
Change that to this:
GetComponent<Rigidbody2D> ().SetForce (new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpForce));
What this does is rather than making the jump add extra force to the player during a double jump,
a double jump and a normal jump will always go up by a specific height. By using SetForce rather than AddForce, your wont get your problem any more.
Hi, thanks for the answers, but i’ve found a way to solve my problem. I’ve just simply added this line
GetComponent().velocity.y = 0 ;
before adding a force to my character, and the jump works correctly. Now i try to tell why, in my opinion, this line is necessary. When you add a vertical force to a game object, this force start decreasing (due to opposite gravity force) until the peak of the jump, where the remaining force is 0. Right after the peak, character start falling down due to gravity force. Setting the vertical velocity to 0 is necessary because if i add a force while the y velocity of the chracter is not 0, this new force is not 700 (the value i’ve selected in my code), but 700+the residual force, which is >0 if the player is rising, <0 if the player is falling (gravity force is a negative force).