• 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 trouble with friction in platformer

I am having trouble implementing friction into my game using some code i found on youtube. For some reason the game is ignoring it, at least that is what seems like. i rewrote several times and got nothing, i even changed
the inputs to key_board_pressed and it only made the character stuck. Can you anyone help? thank you in advance.

STEP EVENT

Code:
//inputs
key_right = keyboard_check(vk_right) or gamepad_button_check(0, gp_padr);
key_left = keyboard_check(vk_left) or gamepad_button_check(0, gp_padl);
key_up =  keyboard_check(vk_up) or gamepad_button_check(0, gp_padu);
key_down = -keyboard_check(vk_down) or gamepad_button_check(0, gp_padd);
key_jump =  keyboard_check_pressed(ord("Z"))or gamepad_button_check_pressed(0, gp_face1);
key_run =   keyboard_check(ord("X"))or gamepad_button_check(0, gp_face2) {run=true}
key_press_left= keyboard_check_pressed(vk_left)or  gamepad_button_check_pressed(0,gp_padl);
key_press_right = keyboard_check_pressed(vk_right)or gamepad_button_check_pressed(0,gp_padr);
key_re_left= keyboard_check_released(vk_left)or  gamepad_button_check_released(0,gp_padl);
key_re_right = keyboard_check_released(vk_right)or  gamepad_button_check_released(0,gp_padr);

// react to inputs
var move = key_right - key_left;

//friction value

fr=.4


if (hsp==0 && vsp==0) idle=true;
else if (hsp!=0 || vsp!=0) idle=false;
// Horizontal movment

if ( key_right|| key_right)
{

hsp=0;
}


if !key_right&& !key_left
{
hsp-=fr*sign(hsp);
{




if  inair=false&& key_run
{
hsp = move * movespeed *2;

}
else
{

    hsp=move * movespeed;

walk=true;

}



// gravity
if (vsp < 10) vsp += grav;


// jumping
if (key_jump && place_meeting(x, y+1, obj_wall))

{
 
 vsp = -jumpspeed;
;
}
if key_jump
{
if (vsp>-jumpspeed)&&djump =true

{

vsp =  -jumpspeed;
djump=false;
}
}
CREATE EVENT
Code:
global.GP = gamepad_is_supported();

///variables
global.levelcomplete=0
rsp=room_speed
global.Health=100
lives=3
global.points=0

//powerups
 wreker=false
 crusher=false
 elctro=false
 pyro=false
 springtail=false
//movment vars
fr=friction;
vsp=0;
hsp=0;
grav=0.5;
jumpspeed=7;
movespeed=4;
run=false;
hurt=false;
djump=false;
longjump=false;
dift=0
maxspeed=2;
//aimation vars
facing=image_xscale;

inair=false;
idle=false;
walk=true;
highfall=false;
nearedge=false;
 
Apply the friction AFTER you've finished with ALL your hsp manipulation. So put this:
Code:
if  inair=false&& key_run
{
hsp = move * movespeed *2;

}
else
{

   hsp=move * movespeed;

walk=true;

}
Before the hsp -= fr bit.
 
I tried it out, it still didn't help. here is what i changed, maybe i did it wrong.


Code:
/// Movment
//inputs
key_right = keyboard_check(vk_right) or gamepad_button_check(0, gp_padr);
key_left = keyboard_check(vk_left) or gamepad_button_check(0, gp_padl);
key_up =  keyboard_check(vk_up) or gamepad_button_check(0, gp_padu);
key_down = -keyboard_check(vk_down) or gamepad_button_check(0, gp_padd);
key_jump =  keyboard_check_pressed(ord("Z"))or gamepad_button_check_pressed(0, gp_face1);
key_run =   keyboard_check(ord("X"))or gamepad_button_check(0, gp_face2) {run=true}
key_press_left= keyboard_check_pressed(vk_left)or  gamepad_button_check_pressed(0,gp_padl);
key_press_right = keyboard_check_pressed(vk_right)or gamepad_button_check_pressed(0,gp_padr);
key_re_left= keyboard_check_released(vk_left)or  gamepad_button_check_released(0,gp_padl);
key_re_right = keyboard_check_released(vk_right)or  gamepad_button_check_released(0,gp_padr);
key_grab= keyboard_check(ord("c")) or gamepad_button_check_pressed(0,gp_face3);
// react to inputs
var move = key_right - key_left;

//friction value
fr=.4;



if (hsp==0 && vsp==0) idle=true;
else if (hsp!=0 || vsp!=0) idle=false;
// Horizontal movment


if  inair=false&& key_run 
{
hsp = move * movespeed *2;

}
else
{

    hsp=move * movespeed;

walk=true;

}

if ( key_right|| key_left)
{

hsp=0;
}




if !key_right&& !key_left
{
hsp-=fr*sign(hsp);
}
 
T

Taddio

Guest
This looks suspicious, as you don't have (at least not that I can see) a variable named "friction".
Code:
//movment vars
fr=friction
Doesn't that give you a fatal error when compiling?
 

NightFrost

Member
I tried it out, it still didn't help. here is what i changed, maybe i did it wrong.
That entire code structure looks bit odd. First you are setting hsp either to run speed or walk speed, which can also be zero if move variable is zero. So far so good. Then, if either left key or right key has been pressed, you set hsp to zero. So, if you're trying to move, your speed will be zero. Finally, if neither left key or right key has been pressed, you decrement hsp by friction. But hsp will be zero at this point in any case so it will be zero minus fr times sign of zero.
 
That entire code structure looks bit odd. First you are setting hsp either to run speed or walk speed, which can also be zero if move variable is zero. So far so good. Then, if either left key or right key has been pressed, you set hsp to zero. So, if you're trying to move, your speed will be zero. Finally, if neither left key or right key has been pressed, you decrement hsp by friction. But hsp will be zero at this point in any case so it will be zero minus fr times sign of zero.
I removed if key_left or key_right =0 part. Do you know any other way to apply friction?
 
T

Taddio

Guest
It doesn't cause one believe it or not.
So you're using GM built-in friction? No need to set the value in step evvent, just set in in object properties and use friction instead of fr to tweak it if needed!
 
Top