GameMaker Studio 2 Desktop

GameMaker Studio 2 Desktop

39 értékelés
Basic Jumping In A Topdown Game
Készítő: heroic
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.
   
Díjazás
Kedvenc
Kedvenc
Törlés
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 ♥♥♥♥♥♥ 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, image_index, 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.













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 :)
6 megjegyzés
annpersant 2024. ápr. 21., 20:26 
If anyone using this wants this to actually work with collision add "y-= zspd" to the step event and then remove "draw_sprite(sprite_index, image_index, x, y - z)" from the draw event and replace it with "draw_self()"
LeOnHaRdTh 2021. dec. 7., 7:21 
awesome!
Le_Beholder 2021. márc. 31., 9:26 
how about platforms? as in M&LSS -style platforms?
I'm thinking there's a little more to those than just setting zspd to 0.
troyleenall 2021. febr. 26., 13:51 
what would the "shooting" math look like to incorporate into firing bullets ? i am a total newb and my end goal to build a top-down RPGish game. i enjoyed your tutorial !!! ty !!!
GameSmashDash 2021. febr. 20., 2:59 
I mean this isn't a very complex concept; I've applied this concept to my own works. I get the fact the mental mapping is difficult.
Squill 2021. febr. 5., 13:23 
very cool!