Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Implementing a jump mechanic | Unity

A quick guide about how to implement a jump mechanic in Unity

Objective: Implement a jump mechanic for a 2D player using a Rigidbody2D.

In the last post I covered how to implement movement with the new Unity’s input system. Now, it’s time to implement a jump mechanic for our player with a Rigidbody2D component within a 2D environment.

Creating the jump action

To begin, let’s create the jump action within our input actions asset. Let’s keep the action type as a Button as we’re going to implement a jump mechanic that gets triggered:

Note: If you don’t know how to start using the new input system in Unity, take a look at my last post.

In the last post I created an action to control the movement of the player.

Then, let’s select the default binding and indicate that the space key from the keyboard will bind with the jump action:

Creating the ground layer

Next, in order to be able to identify when the player is grounded, and therefore, allow the jump mechanic to be performed, let’s select the respective ground tilemap and change its layer:

Implementing the jump mechanic

Variable declaration

And now, in order to implement the jump mechanic, let’s open the player script and declare the next variables:

  • Jump power

This variable will determine the initial velocity to apply when jumping.

  • Jump fall gravity multiplier

This variable will determine the gravity scale applied to the Rigidbody when the player is falling after performing a jump.

  • Ground check height

This variable will determine the height of the box (OvelapBox method) that we’ll be using below the player to determine if the it’s on the ground or not.

  • Ground mask

This variable will allow us to check for colliders that are within the ground layer when using the OverlapBox method.

  • Disable ground check time

This variable will determine how much time does the ground check gets disabled when jumping in order to avoid resetting the jumping bool value.

  • Box center

This variable will indicate the central coordinate of the box that checks if the player is on the ground or not.

  • Box size

This variable will indicate the size (width, height) of the same box.

  • Jumping

This variable will indicate if the player is currently jumping.

  • Initial gravity scale

This variable will store the initial gravity scale value of the Rigidbody.

  • Ground check enabled

This variable will indicate if the ground check is enabled or not.

  • Wait

This variable will be used to wait within a coroutine before enabling the ground check again.

  • Rigidbody

This variable will hold a reference to the Rigidbody2D of our player.

  • Player actions

This variable will hold a reference to the input action asset that contains the player actions like moving or jumping.

  • Collider

This variable will hold a reference to the Box Collider 2D of our player.

Variable initialization

Then, in the Awake method, let’s initialize the variables and the references we need to implement the jump mechanic. The last line subscribes the Jump_performed method to the performed event of the Jump action from the input action asset.

Also, let’s make sure to enable and disable the respective action map (to receive the input from the user) within the OnEnable and OnDisable method as good practices:

Implementing the jump

Now, in order to implement the jump, let’s use the next methods and coroutines:

Jump_performed

This method will be called each time that the player performs the Jump action (which is bound to the space key). First, we’ll check if the player is on the ground by calling the IsGrounded method. If that’s the case, we’ll add velocity to the Y axis (according to the jump power of the player) and set the jumping bool value to true. Finally, we’ll start a coroutine to disable and enable the ground check to avoid resetting the jumping bool value while starting the jump.

IsGrounded

This method will return a bool value to indicate if the player is on the ground or not. To check this we’ll need to:

  • Calculate the central coordinate of the box by adding up: the central coordinate of the collider, the half of the height of the collider (negative) and the half of the height from the new box (negative) that checks if the player is on the ground.
  • Set the size of the new box within a Vector2 to have the same width of the collider and the height desired.
  • Use the OverlapBox method by sending as parameters the center coordinate of the new box, the size of the new box, the angle of the new box and the respective ground layer mask.
  • If the collider returned isn’t null, the player is on the ground, otherwise, it isn’t on the ground.

EnableGroundCheckAfterJump

This coroutine will change the bool value that indicates that the ground check should be “disabled” or “enabled”. This is intended to disable the ground check for some time (indicated by the user) after each jump so that the bool value that indicates when the player is jumping doesn’t change when the player is still on the ground for the initial frames.

Polishing the jump

And now, in order to improve the jump execution, let’s create a new method that will be called in the FixedUpdate method to determine when the player isn’t jumping anymore and to change the gravity scale when it’s falling respectively:

Then, let’s add it into the FixedUpdate method after the lines that apply the movement:

Visualizing the ground check

Next, in order to visualize the box that checks if there’s ground below the player, let’s use the Gizmos.DrawWireCube method with the respective coordinate and size inside the OnDrawGizmos method:

Note: This will be visible only in the Scene window of the Unity Editor.

Customizing properties

Finally, to start using the jump mechanic, let’s modify the values of the jump and ground check properties through the inspector:

If we run the game with Unity, we’ll see that the ground check works and more gravity gets applied after reaching the apex of the jump:

And that’s it, we implemented a reliable jump mechanic for our player with Unity! :D. I’ll see you in the next post, where I’ll be showing how to implement more mechanics for our player with Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

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
Fernando Alcantara Santana

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).