Wall Jumping in Unity! (2.5D Platformer)

Marcus Ansley
3 min readJul 11, 2021

--

(This was a particularly satisfying mechanic to work on 😉)

Recently needed to implement a wall-jumping mechanic to this 2.5D platformer. As a brief explanation, whenever the player hits anything tagged “Wall” I’m using a normal vector from that surface in order to ‘bounce’ the player off when they hit the jump button (in addition to applying the usual _jumpHeight float variable to their y velocity). This then gives us the impression of being ascend by jumping from wall to wall.

I’ll include a step-by-step breakdown of this below!

1. In our Player script, we’ll need to add the OnControllerColliderHit method in order to detect and retrieve information about collisions between our CharacterController and the collider of another object. We’ll then need to check for two things:

  1. The player shouldn’t be grounded (we can check this via the CharacterController.isGrounded bool).
  2. The object we’ve collided with must be a wall (we can check this using tags).

If both of these conditions are met, then we can set a _canWallJump bool to true and store the hit.normal information in a Vector3 variable (‘_wallJumpDirection’ in this case). For testing purposes, we can also use the Debug.DrawLine method to draw a long between our hit.point (the point of collision) and our hit.normal. For some reason, setting hit.normal as the second argument in this method gave me some funky results, but I found a workaround online (just add hit.normal to our hit.point).

In the event that either of these conditions are false, then we just need to make sure _canWallJump is set to false:

2. Next, we need to make a couple of adjustments to our Update method (where we’re handling the current jumping and movement logic). The main adjustment we need to make is in our first else statement (when the player isn’t grounded). We need to check whether the jump button has been pressed and whether or not _canWallJump is true. If it isn’t, then we’ll check to see if we can double jump, but if it is, then we can implement the logic to handle the wall jump. Thankfully the logic here is surprisingly quick and easy: just two lines of code; one to give us our usual jumpHeight and the other to set our velocity according to the _wallJumpDirection Vector3 multiplied by our speed. We don’t have to multiply this by our speed, but we probably do need to go give this a little boost to ensure the player properly bounces off the wall 😉

3. With this done, all we need to do now is to ensure that a “Wall” tag exists, add it to the walls we want to have the player jump off, and ensure those walls have colliders on them (with isTrigger set to false) 😄

I’ll include the relevant code for this mechanic in the script below:

Happy wall-jumping!

--

--

Marcus Ansley
Marcus Ansley

Written by Marcus Ansley

Game Developer | Game Design and Literature Graduate

No responses yet