• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML What is wrong with my jump code?

L

Littlesticks

Guest
I have a jump code currently that works okay except with a few glaring and important issues. The first issue is that I can continuously spam the jump button to "jump" again and if I do it fast enough I can stay in the air. Another issue I spotted is that the player object might fall much faster than expected when I spam the jump a lot. So apparently it's stacking the values?

I've been trying to fix it for a few hours and I'm not sure what I'm doing wrong.

Code:
if (!place_meeting(x,y+1,obj_floor)){
    y += jump_grav;
    jump_grav += 1.5;
}

if (jump_grav > jump_grav_cap){
    jump_grav = 8;
}

if (jump){
    y -= jump_speed;
    if (jump_grav = 8) {
        jump_speed -= 1.5;
    }
    else {
        jump_speed = 16;
    }
}

if place_meeting(x,y+1,obj_floor){
    jump_grav = 1.5;
    jump_speed = 16;
}
Please help (and teach me) what my mistakes are, ty
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
I have a jump code currently that works okay except with a few glaring and important issues. The first issue is that I can continuously spam the jump button to "jump" again and if I do it fast enough I can stay in the air. Another issue I spotted is that the player object might fall much faster than expected when I spam the jump a lot. So apparently it's stacking the values?

I've been trying to fix it for a few hours and I'm not sure what I'm doing wrong.

Code:
if (!place_meeting(x,y+1,obj_floor)){
    y += jump_grav;
    jump_grav += 1.5;
}

if (jump_grav > jump_grav_cap){
    jump_grav = 8;
}

if (jump){
    y -= jump_speed;
    if (jump_grav = 8) {
        jump_speed -= 1.5;
    }
    else {
        jump_speed = 16;
    }
}

if place_meeting(x,y+1,obj_floor){
    jump_grav = 1.5;
    jump_speed = 16;
}
Please help (and teach me) what my mistakes are, ty
You don't want to be changing the gravity value.
Imagine the player in the real world... our gravity on earth is 9.8m/s^2 and it NEVER changes!!

[CREATE EVENT]
Code:
// The higher the value the higher you jump
jump_spd = 10;
// The higher the value the faster you fall
grav = 2;
// This is the cap for how fast you fall
max_vspd = 4;

[STEP EVENT]
Code:
// Here we check if we are on the floor
var floored = place_meeting(x,y+1, obj_floor)

// If we are
if (floored)
{
    // No need to apply vertical speed
    vspd = 0;
    // We can check for jump key pressed
    if (/*your key press check here*/)
    {
        // Apply the jumping force
        vspd -= jump_spd;
    }
}
// You are in the air (jumping or falling)
else
{
    // Apply the gravity
    vspd += grav;
}

// Here you can cap the velocity
vspd = min(vspd, max_vspd);

// Apply the speed to your position
y += vspd;
 
Last edited:
T

TimothyAllen

Guest
Code:
// Here you can cap the velocity
vspd = max(vspd, max_vspd);
Code:
// The higher the value the higher you jump
jump_spd = 10;
// The higher the value the faster you fall
grav = 2;
// This is the cap for how fast you fall
max_vspd = 4;
Think you flubbed up here. you are making vspd a MINIMUM of max_vspd. So it will never be less than 4... this will cause all kinds of issues.
 
L

Littlesticks

Guest
You don't want to be changing the gravity value.
Imagine the player in the real world... our gravity on earth is 9.8m/s^2 and it NEVER changes!!

[CREATE EVENT]
Code:
// The higher the value the higher you jump
jump_spd = 10;
// The higher the value the faster you fall
grav = 2;
// This is the cap for how fast you fall
max_vspd = 4;

[STEP EVENT]
Code:
// Here we check if we are on the floor
var floored = place_meeting(x,y+1, obj_floor)

// If we are
if (floored)
{
    // No need to apply vertical speed
    vspd = 0;
    // We can check for jump key pressed
    if (/*your key press check here*/)
    {
        // Apply the jumping force
        vspd -= jump_spd;
    }
}
// You are in the air (jumping or falling)
else
{
    // Apply the gravity
    vspd += grav;
}

// Here you can cap the velocity
vspd = min(vspd, max_vspd);

// Apply the speed to your position
y += vspd;
So basically I should be forcing the object to go either up or down and make jump code according to that?
Also, how come y += vspd; is placed at the end of the code, shouldn't it be at the beginning? Or is it that the position doesn't matter for Coding?

EDIT: What is vspd = min(vspd, max_vspd); doing for the object? Is it meant to force the vspd to the minimum value everytime vspd changes? Why are we doing this?

(Sorry for all the questions, I'm new to coding)
 
Last edited by a moderator:

xDGameStudios

GameMaker Staff
GameMaker Dev.
So basically I should be forcing the object to go either up or down and make jump code according to that?
I don't think I understand what you want to ask with that question. The object is either falling or jumping it can't be both (and you want it to jump only when you are on the floor). So first you check if player is on the floor if it is you can jump and you reset vertical speed (vspd) to zero (Imagine your self you are standing in your room.. your vertical speed is zero). In this case you don't have to apply gravity as you are not moving in the vertical axes.

Also, how come y += vspd; is placed at the end of the code, shouldn't it be at the beginning?
it really shouldn't matter. But it is easier to understand what you are doing.. you are calculating the correct velocity to apply to the object and then AFTER applying it, it makes sense, no?

What is vspd = min(vspd, max_vspd); doing for the object? Is it meant to force the vspd to the minimum value everytime vspd changes?
translating that line of code: "vertical speed should have the minimum value between vertical speed and max vertical speed":

1) if vertical speed is less than max vertical speed then vertical speed stays the same.

2) if vertical speed is greater than max vertical speed then vertical speed is capped and given the value of max vertical speed.

This is not necessary but imaging a scenario you don't want your player to fall faster than a given amount after applying the gravity you have to limit the speed.
 
Top