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.
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
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.
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.
This variable will be used to wait within a coroutine before enabling the ground check again.
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.
This variable will hold a reference to the Box Collider 2D of our player.
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:
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.
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.
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.
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.