Player Jump in Unity
Make player able to jump
Objective: make the player jump when the space key is pressed.
In the player script I created a new variable called _jumpHeight which is the jump force for the player. In the update method we check if the player is grounded and if the space key is pressed, if it is true then we add the jump height to the velocity vector.
So now when we try the feature in play mode we can see a weird behavior, that is because immediately in the next frame the value of the velocity vector in the Y axis is reseted to 0. To fix this we have to create an auxiliary variable to store the value of the calculation in the if statement and add it to the velocity vector after the calculations.
And that’s it!!! With that change we have the correct behavior for the player jump.