- Home /
How to create a variable jump height
Hi guys,
I'm creating a platformer and am trying to figure out how to create a variable jump height. Similar to what you'd find in Yoshi games from nintendo.
Not working in Unity, the way I imagined, but some pseudo code I kind of have in my head is:
if(Input.GetKeyDown ){
//player jumps force of 1
}
if( Input.GetKey ){
//player jumps force of 1.15 (with a lerp from 1 --> 1.15)
}
After a bit of digging, I managed to nail down a normal jump. But this has a fixed height.
void Jump()
{
_rigidBody2D.AddForce (new Vector2 (0, JumpForce));
}
Any help would be appreciated. Thanks
Answer by Eno-Khaon · Apr 27, 2017 at 02:53 AM
This isn't my first time answering this question, so I'm going to shortcut this a little bit by directing you to a previous response I've given on the topic (http://answers.unity3d.com/questions/1324980/jump-higher-when-holding-button.html?childToView=1327081#answer-1327081).
To summarize here what I say there, the approach I offer provides a guaranteed maximum jump height and an implied minimum. Rather than continuously pushing the character upward, I instead opt to push the character down when the player is not holding the jump button. Then, once they have reached the apex of their jump (and, therefore, have started falling down), the character no longer needs to be pushed down in order to reduce the height of the current jump.