The Secret of Wall Jump- Game Dev Series 84
Objective: create wall jumping function.
Wall jump, a stylish skill that you need to be talented to do it well in the first time of playing platformer game. It is also a movement type that requires some understanding to create.
In this article, we will create the wall jump ability to Player.
The core of wall jumping
To start wall jumping, we need to figure out what should make our Player jumping on the wall.
The basic of wall jumping is when Player touched the wall and jumped, it should move towards the opposite of the wall. In 3D, it should be the direction of normal on that jumping face.
We use CharacterController to do all the movement in this game, wall jumping would be one of the functions from it, too.
To do wall jumping, we will use a method called OnControllerColliderHit().
We can create a simple debug test to understand how this method works.
Select the walls in your game scene, and set their tag to “Wall”. You might need to create one if there is no Wall tag.
Then create OnContollerColliderHit() method and add one line to it.
We use this line of code to draw a ray from position on Player to wall surface. And this is how it looks like when play it.
There is a tiny blue line. This is the normal of wall, and we will use this as the standard to make Player finish wall jumping.
The limit of wall jumping
To jump on normal of wall, we need a variable to storage the vector3 of it.
Inside OnControllerColliderHit(), set the reference to hit.normal.
Now we can use this variable as the direction to change our velocity. However, if we pressed “space” to jump, it would only do the double jump. We need to make Player can only do wall jumping only when it touched the wall, while the double jump is disable.
This sounds like a bool to me, we can create a bool to control it.
It is time to understand when should enable and disable this bool. The answer of when to enable it should be easy. When Player touched wall in the air, it should be able to “wall jump”, which is “true.
Let’s add this condition and result to the if statement.
Next, when should disable wall jump? That is when Player is grounded. Set this bool to false in the if statement.
The preparation is ready. Let’s do some wall jumping!
Velocity on normal
Inside Update(), when Player is not grounded and pressed “space”, it should be double jumping or wall jumping. Add these lines into that if statement.
Quick explanation, if Player has not double jump yet and could not do the wall jump(if statement), it is not touched any walls and ready to do the double jumping.
On the other hand, if Player can do the wall jumping, the velocity would be the direction of normal(which should be 1 depends on direction) multiply the speed value.
All done! Now your Player should be ready to do a perfect wall jump!