• 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 [SOLVED] Limiting Jump Height

T

Treecase86

Guest
Hey all! I am in a small predicament currently. I would like to make some sort of jump cap. So you can't, y'know, fly to the heavens. I'm semi-new to GML and can't find any tutorials on this. Could any of you guys help? I've tried experimenting with clamps but to no avail. Many thanks for your help.
 

samspade

Member
Hey all! I am in a small predicament currently. I would like to make some sort of jump cap. So you can't, y'know, fly to the heavens. I'm semi-new to GML and can't find any tutorials on this. Could any of you guys help? I've tried experimenting with clamps but to no avail. Many thanks for your help.
Not really sure what you're asking here. Jump caps are normally done by apply some type of gravity. What does you're code look like currently? (I'll note that there are a number of free character movement assets on the marketplace as well.)
 
T

Treecase86

Guest
Not really sure what you're asking here. Jump caps are normally done by apply some type of gravity. What does you're code look like currently? (I'll note that there are a number of free character movement assets on the marketplace as well.)
I have added gravity but you can still jump as high as you want. My code is in my movement help thread.
 

JasonTomLee

Member
Hmmm You could try something like this!

Save the Y-Axis of the starting point of the jump (keyboard_check_pressed( *insert jump key here* ) )
Then while you're holding (keyboard_check( *jump key* ))
Clamp the jump height depending on your current Y and initial jump point.
 
Only allow jumping if there is a block beneath you:
Code:
if(position_meeting(x,y+1,block)){
    //jump code
}
Replace "block" with the actual platform object (a parent preferably). Replace "//jump code" with whatever you are using to jump, like "vspeed = -10;". And put this code in your jump key event.
 

Bentley

Member
You're trying to cap the jump height right? Off the top of my head, something like this might help:
Code:
if (onGround) && (keyJump)
{
    jumpCap = y - 200; //Tinker with this to get the height you want
    jumping = true;
}

if (jumping)
{
    if (y > jumpCap)
    {
        y -= jumpSpeed;
    }
    else
    {
        //fall
    }
}
 

JasonTomLee

Member
So it'll look something like this?

Code:
//Create Event
startY = 0;
jumpHeight = 50;

// Jump key
var kJumpP = keyboard_check_pressed( vk_space );
var kJump   = keyboard_check( vk_space );
var kJumpR = keyboard_check_released( vk_space );

// Save Starting Y-node

if (kJumpP){
startY = y;
} else if (kJump){
y = Clamp( y, 3, startY+ jumpHeight ); //3 is the jump speed
}


///Clamp( value, change, cap );

// The change can either be negative or positive
// The cap is calculated whether you wish to check the min or max value.

// Ex) Hp = Clamp( Hp, 5, 100 );
//     Hp = Clamp( Hp, -10, 0 );


var _val        = argument0;
var _change     = argument1;
var _cap        = argument2;
var SIGN        = sign(argument1);


var _val2       = _val + _change;

if (SIGN == 1){
if (_val2 > _cap)
    return _cap;
} else {
if (_val2 < _cap)
    return _cap;
}

return _val2;
 

JasonTomLee

Member
You're trying to cap the jump height right? Are you looking for something like this (I'm sure this isn't the best way to go about it):
Code:
if (onGround) && (keyJump)
{
    jumpCap = y - 200; //Tinker with this to get the height you want
    jumping = true;
}

if (jumping)
{
    if (y > jumpCap)
    {
        y -= jumpSpeed;
    }
    else
    {
        //fall
    }
}
Yea! Something like that~ sry my example was more of a demonstration & brief .. :p But you get the idea !
 
T

Treecase86

Guest
Yea! Something like that~ sry my example was more of a demonstration & brief .. :p But you get the idea !
Thanks all of ya guys! How do I implement this:p? I do it in a script, yes? Where do I execute the script?
 
T

Treecase86

Guest
So it'll look something like this?

Code:
//Create Event
startY = 0;
jumpHeight = 50;

// Jump key
var kJumpP = keyboard_check_pressed( vk_space );
var kJump   = keyboard_check( vk_space );
var kJumpR = keyboard_check_released( vk_space );

// Save Starting Y-node

if (kJumpP){
startY = y;
} else if (kJump){
y = Clamp( y, 3, startY+ jumpHeight ); //3 is the jump speed
}


///Clamp( value, change, cap );

// The change can either be negative or positive
// The cap is calculated whether you wish to check the min or max value.

// Ex) Hp = Clamp( Hp, 5, 100 );
//     Hp = Clamp( Hp, -10, 0 );


var _val        = argument0;
var _change     = argument1;
var _cap        = argument2;
var SIGN        = sign(argument1);


var _val2       = _val + _change;

if (SIGN == 1){
if (_val2 > _cap)
    return _cap;
} else {
if (_val2 < _cap)
    return _cap;
}

return _val2;
Hey, uh, slight problem. The character shoots sky high when I press space. How can I fix this?

EDIT: Fixed this. Thanks for your help
upload_2017-9-9_18-0-12.png upload_2017-9-9_18-0-54.png
 
Last edited by a moderator:
Top