GameMaker Studio 2 Desktop – Basic Jumping In A Topdown Game

GameMaker Studio 2 Desktop – Basic Jumping In A Topdown Game 1 - wpgameplay.com
GameMaker Studio 2 Desktop – Basic Jumping In A Topdown Game 1 - wpgameplay.com

Guide for GameMaker Studio 2 Desktop – Basic Jumping In A Topdown Game

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. 
z = 0; zspd = 0; grav = 0.2; jump_height = 4; //You can change these numbers to whatever works best for your game 
 

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. 
key_jump = (keyboard_check_pressed(vk_space) or keyboard_check_pressed(vk_up)); 
Once you’ve done that, you’ll want to add the following code underneath it. 
if(z > 0) zspd -= grav; else zspd = 0; z = max(0, z); is_grounded = z == 0; if(key_jump and is_grounded) { zspd += jump_height; } z += zspd; 
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. 
if(z > 0) zspd -= grav; else zspd = 0; 
This will make it so our Z position can’t go below 0. 
z = max(0, z); 
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! 
is_grounded = z == 0; if(key_jump and is_grounded) { zspd += jump_height; } 
And finally we just need to make sure the Z position is actually being changed by the z speed 
z += zspd; 
 

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 
draw_sprite(sprite_index, 0, x, y - z);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. 
GameMaker Studio 2 Desktop - Basic Jumping In A Topdown Game 
 
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 🙂 
 

By heroic

I hope you enjoy what we shared today about GameMaker Studio 2 Desktop – Basic Jumping In A Topdown Game. If there is anything, you want us to add, please let us know via comment below! See you soon! And thanks!
 


Be the first to comment

Leave a Reply

Your email address will not be published.


*