This code initializes the variables we need for our wall jump.
wall_direction tells us if the player is sticking to a wall, and where that wall is.
It'll be 0 when not sticking to the wall, 1 when sticking to a wall on the right, and -1 when sticking to a wall on the left.
wall_jump_force is the horizontal jumping force applied when jumping from a wall.
wall_jumping will be set to true for a brief period after jumping from a wall, and will be false at other times. This way we can know if the player has just wall-jumped or not.
Sticking Left
Open obj_player's Key Down - Left event, and add the following code at the top of the event.
This code checks for a wall on the left, and if it finds one, it sets wall_direction to -1. This is how we indicate that we're sticking to a wall on the left.
It then sets Alarm 1 to run after 10 frames, which will reset the player’s wall jumping state. (That event doesn't exist yet - we'll make it soon.)
Stop Right There
When the player has just wall jumped, we don't want them to change directions. This requires a simple change.
In the same event, there's a condition that checks if the player is in knockback, which then exits the event.
Add ‘or wall_jumping’ to the following If Expression so the event exits if the player is wall jumping.
if (in_knockback or wall_jumping)
{
exit;
}
This means the player can’t move if they're wall jumping.
Sticking Right
Now, repeat the last two steps in the Key Down - Right event, but replace -1 with 1.
Don't forget to add ‘or wall_jumping’ into the next condition.
Alarm to reset state
Create an Alarm 1 event:
And add the following code in it:
wall_direction = 0;
This will reset wall_direction to 0, which means the player isn't wall jumping, and they can move again.
Slow Down Gravity
While the player sticks to a wall, we want them to fall slower.
Next, inherit the Begin Step event.
Add the following code after event_inherited() or the Call Parent Event action:
if (wall_direction != 0)
{
if (vel_y > 3)
{
vel_y = 3;
}
}
This code reduces the player's Y velocity to 3, slowing its fall, if wall_direction is not 0 (i.e. the player is next to a wall and is pushing left or right).
If we run the game now, the player can stick to the wall, but can't jump off it yet.
This code checks if the player is in mid-air and if wall_direction equals something other than 0.
If both of these requirements are met, we know that the player is sticking to a wall, and because of the event, that they're pressing Space. It’s wall-jumpin’ time.
We set the player's x and y speed, basing the x speed on the wall_direction, multiplied by -1 so the player jumps in the opposite direction of the wall.
Then it changes the player's sprite to the jumping one, sets grounded to false, and wall_jumping to true.
wall_jumping should be reset after half a second, so we set Alarm 2 to 30. We'll create this event soon.
Finally, it exits the event, so the normal jumping logic doesn’t run.
Reset Wall Jump
Create an Alarm 2 event:
And add the following code to reset wall_jumping to false.
wall_jumping = false;
Here we go!
The player can now wall jump.
You can now make new sections in your levels that use wall jumping, and hide secrets behind clever wall-jumps.