1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GML trouble with friction in platformer

Discussion in 'Programming' started by Apexpredator, Mar 14, 2019.

  1. Apexpredator

    Apexpredator Member

    Joined:
    Apr 27, 2017
    Posts:
    36
    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;
    
     
  2. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,100
    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.
     
  3. Apexpredator

    Apexpredator Member

    Joined:
    Apr 27, 2017
    Posts:
    36
    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);
    }
    
    
    
    
     
  4. Apexpredator

    Apexpredator Member

    Joined:
    Apr 27, 2017
    Posts:
    36
    he still gets doesn't move
     
  5. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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?
     
  6. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,711
    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.
     
  7. Apexpredator

    Apexpredator Member

    Joined:
    Apr 27, 2017
    Posts:
    36
    It doesn't cause one believe it or not.
     
  8. Apexpredator

    Apexpredator Member

    Joined:
    Apr 27, 2017
    Posts:
    36
    I removed if key_left or key_right =0 part. Do you know any other way to apply friction?
     
  9. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice