Platformer: Wall Jumping

Intro

In this article, we will talk about how to give your player the ability to wall jump. I have a previous article that covers the setup of getting this mechanic ready, so makes sure you read that before you read this.

The first thing we need to check for is if the player is not grounded, meaning the player is jumping, and hitting the wall. It is this condition we will want to give the player the ability to perform a wall jump. We are already using the player controller.isGrounded to detect if the player is hit the ground or jumping.

In order to detect if we are hitting a wall we can wall jump off of, we will give them a tag “Wall”.

In the Player script, we can now check for both of these conditions in the On Controller Collider Hit method. When both of these conditions are met, we know the player should have the ability to wall jump, so we will create a global bool _canWallJump and set it true.

When the player is grounded, meaning the player is not jumping, we will want to set _canWallJump false since it doesn’t might both of the wall jumping conditions.

When we are wall jumping we want to disable the player’s ability to double jump, so in the ELSE of isGrounded true, we check for the space bar being pressed (jump) AND _canWallJump = false. This allows for the double jump to only happen in when we are not wall jumping.

If we are not grounded, the space key is pressed and _canWallJump is true (we set it to true when we come into contact with the tagged wall), then we want to set the player’s velocity to to the surface normal of the wall.

The surface normal is stored in the hit variable of the Collider Hit.

We will want to store this in a global variable, so we can apply it to the player’s velocity. Declare a Vector 3 variable for this.

We set this Vector3 global variable to hit.normal in the ControllerColliderHit.

Now, we are finally ready to apply the surface normal to the velocity and we will want to multiply it by the player’s speed or you will not really bounce back. This only gets the player to move in the opposite direction of the wall it is jumping off of, so we also need to give it jumping height. We do this by setting the y velocity to the jump height.

Conclusion

It is endless fun being able to bounce back-and-forth off of walls. This mechanic also allows you to add more interesting level designs. For this version of the wall jump, we disable the ability to move in mid air, which is correct physics. But, all the platformers I grew up on allowed you to move once in the air, so I will want to revisit this and add that feature. Until next time…Cheers!