Player Jump in Unity

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.