• 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 Help With Simple Varying Jump Height

BlueBot5000

Member
For my PlatFormer i'd like my character press "K" to do a low jump but if "K" is pressed and is still being held than i would like my Character to jump at max jump height (but still fall just to be clear)

How can i do this?

here is the code in case it's needed

CREATE EVENT

Code:
/// @description Create Some PLatformer Variables

hspd = 0;
vspd = 0;

grv = 0.3;
walkspd = 4;
jumpspd = 9.4;

key_down = 0;
STEP EVENT

Code:
/// @description Core PLayler LOGIC

//Get The PLayer Inputs
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(ord("K"));
key_down = keyboard_check(vk_down);

//Calculate Movement
var _move = key_right - key_left;

hspd = _move * walkspd;

vspd = vspd + grv;

if (place_meeting(x,y+1,objSoild)) && (key_jump)
{
    vspd = -jumpspd;
}
//Horizontal Collisions
if (place_meeting(x+hspd,y,objSoild))
{
    while (!place_meeting(x+sign(hspd),y,objSoild))
    {
        x = x + sign(hspd);
    }
    hspd = 0;

}
x = x + hspd;

//Vertical Collisions
if (place_meeting(x,y+vspd,objSoild))
{
    while (!place_meeting(x,y+sign(vspd),objSoild))
    {
        y = y + sign(vspd);
    }
    vspd = 0;

}
y = y + vspd;

//Animations
if (!place_meeting(x,y+1,objSoild))
{
    sprite_index = sprMrBrick_InAirNEW;
    image_speed = 0;
    if (vspd > 0) image_index = 1; else image_index = 0;
}
else
{
    image_speed = 1.4;
    if(hspd == 0)
    {
       sprite_index = sprMrBrick_Idle;
    }
    else
    {
       sprite_index = sprMrBrick_Jogging;
    }
}

if (hspd != 0) image_xscale = sign(hspd);
 
Last edited:

Kahrabaa

Member
You need a variable that keeps track of how much force is remaining in the jump.

example force = 50;
-force decreases with 1 as long as K is held down
-If player is in air and K is NOT held down then set force to 0.
-vspd decreases (-y) as long as force is larger than 0
-force is reset to 50 when player is on floor
 

Slyddar

Member
You need to capture when the jump key is being held down, so add a check
Code:
key_jump_held = keyboard_check(ord("K"));
Then you need a variable to control the effectiveness of the release, I use jump_dampner.
Code:
jump_dampner = 4.5;
Now you can use that to set the vspd to something smaller if jump_held is not being held while moving up. We can use max to ensure it only applies once.
Code:
//enable smaller jumps
if vspd < 0 and !key_jump_held vspd = max(vspd, -jumpspd / jump_dampner);
 

Slyddar

Member
The key_jump_held goes with your other key captures.
The declaring of the jump_dampner variable goes in the create event.
The code to enable the smaller jumps goes before your horizontal collision check, after you've applied the jump.
 

BlueBot5000

Member
I am using your code but the character wouldn't even jump at all!

is it
Code:
//Get The PLayer Inputs
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(ord("K"));
key_down = keyboard_check(vk_down);

//Calculate Movement
var _move = key_right - key_left;

hspd = _move * walkspd;

vspd = vspd + grv;

if (place_meeting(x,y+1,objSoild)) && (key_jump)
{
    vspd = -jumpspd;
//enable smaller jumps
if vspd < 0 and !key_jump_held vspd = max(vspd, -jumpspd / jump_dampner);
}
or
Code:
//Get The PLayer Inputs
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(ord("K"));
key_down = keyboard_check(vk_down);

//Calculate Movement
var _move = key_right - key_left;

hspd = _move * walkspd;

vspd = vspd + grv;

if (place_meeting(x,y+1,objSoild)) && (key_jump_held)
{
//enable smaller jumps
if vspd < 0 and !key_jump_held vspd = max(vspd, -jumpspd / jump_dampner);
}
It's quite a confusing thing to me...
 

FoxyOfJungle

Kazan Games
GML:
/// @description Core PLayler LOGIC

//Get The PLayer Inputs
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_up);
key_jump_held = keyboard_check(vk_up);
key_down = keyboard_check(vk_down);

//Calculate Movement
_move = key_right - key_left;
hspd = _move * walkspd;
vspd = vspd + grv;

//Jump
if (place_meeting(x,y+1,objSoild)) && (key_jump)
{
    vspd = -jumpspd;
}

//Small Jump
if (vspd < 0 and !key_jump_held)
{
    vspd = max(vspd, -jumpspd / jump_dampner);
}



//Horizontal Collisions
if (place_meeting(x+hspd,y,objSoild))
{
    while (!place_meeting(x+sign(hspd),y,objSoild))
    {
        x += sign(hspd);
    }
    hspd = 0;
}




//Vertical Collisions
if (place_meeting(x,y+vspd,objSoild))
{
    while (!place_meeting(x,y+sign(vspd),objSoild))
    {
        y += sign(vspd);
    }
    vspd = 0;

}


x += hspd;
y += vspd;
 

FoxyOfJungle

Kazan Games
If these are done together there is a chance of getting stuck in corners. The x needs to be applied after the horizontal movement, then y after the vertical movement, like he has in his original post.
Make sense, I didn't know it could cause this, thanks!
 

BlueBot5000

Member
Thank you very much don't know what would've done if i didn't have varying jump height thanks to this effort my game get's a fresh and clean-looking start!
 
Top