Overview
This guide will teach you how to add jumping (or essentially a fake Z axis) to a topdown game! This guide is fairly beginner-friendly, but I do recommend you learn the basics of topdown movement and collision before you try to implement this into your games.
Initialization
In your Create event, make two variables called ‘z’ and ‘zspd’ respectively. These will be used to store your Z coordinate and your speed on the Z axis. You also want to add a variable called ‘grav’ which will determine the amount of gravity on the Z, and add a variable for your jump height. They don’t have to be named the same as I did but make sure you know what they mean.
Step Event
At the top of your Step event, make a new variable and call it something like ‘key_jump’. This is what you’re going to use to store your jump input. Below is what we’ll be using.
Once you’ve done that, you’ll want to add the following code underneath it.
Before we move on, lets do a quick analysis of this code.
Here we want to decrease our Z speed when the player is above the ground to simulate gravity, and also make sure our Z speed is set to 0 when the Z position of our player is less than or equal to 0 (essentially being the “ground level”) so they don’t continue to fall once they’ve hit the ground.
This will make it so our Z position can’t go below 0.
Here we make a true/false value called ‘is_grounded’ that checks if the players z level is equal to 0. Then we check for the jump input and make sure the player is on the ground and we jump!
And finally we just need to make sure the Z position is actually being changed by the z speed
Draw Event
Now here’s where the magic happens. When you’re implementing a Z axis into your game with this method its not really a “true” Z axis. You’re essentially faking it by taking advantage of the already existing Y axis. When you do this you don’t want to change the Y axis of the actual player. This would cause a lot of collision issues and potentially other issues. Instead of actually changing the Y position of our player, we’re just going to change the Y position of the sprite.
In your Draw event, add the following code
Here we are simply drawing the player sprite, but we subtract our Z position from the Y position.
End Result
That’s it! You’ve successfully learned how to jump. Here’s an example of it in action. I’ve added a shadow to signify where the actual Y position of the player is located.
Thanks for reading. Hopefully I was able to help. Remember to never give up even when you’re feeling discouraged or burnt out. Take breaks. Take as long as you need. Come back when you feel like you’re ready to work on your game again 🙂