2.5D Game Development In Unity: Physics Based Character Controller Part 2: Jumping

In the previous article I set up the movement for the player. In this article I’ll go over setting up a jump mechanic.

To jump, the player needs to be grounded. Then I want to say if the space key is pressed the player jumps. So the following script will achieve this. This is all being handled in update for now.

Dont worry about the wall jump stuff just yet. That’ll come up a little later.

These variables help me store the info needed to accomplish jumping.

I set the yVelocity to the jumpHeight when the space key is pressed and then I set a double jump bool to true. This means I am now in the air. So now “else” if Im in the air and I press space, the player will double jump, so I’m just adding the jumpHeight to the yVelocity again.

Now I’m setting gravity. this happens in the else statement. So therefore, if the player is NOT grounded, apply gravity. The final result! Since the variables have been given the SerializeField attribute, changing the values of them i the inspector is easy. Adjust them to your liking :)

--

--

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