1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

[SOLVED] getting stuck while walking on slopes

Discussion in 'Programming' started by HaloElite, Dec 3, 2019.

Tags:
  1. HaloElite

    HaloElite Member

    Joined:
    Nov 25, 2019
    Posts:
    5
    I'm using this part of code to walk up slopes:

    Code:
    if(keyboard_check(vk_left)){
        hsp = -4;
    }else if(keyboard_check(vk_right)){
        hsp = 4;
    }else {
        hsp = 0;
    }
    
    // Slow down opposite x-movement when jumping
    if(sign(hsp) == 1 && moveL || sign(hsp) == -1 && moveR) hsp /= 2;
    if(sliding == true) hsp *= 2;
    
    // When hitting a slope
    if(place_meeting(x + hsp, y, solids)){
      
        yplus = 0;
        // Checks height of slope
        while(place_meeting(x+hsp, y-yplus, solids) && yplus <= abs(1*hsp)) yplus += 1;
      
        // Walks
        if(place_meeting(x+hsp, y-yplus, solids)){
            // Walks x-wise
            while(!place_meeting(x+sign(hsp), y, solids)) x += sign(hsp);
            hsp = 0;
        }else{
            // Walks y-wise
            y -= yplus;
        }
    }
    x + hsp;
    
    It works fine but when I'm changing the direction while TOUCHING the slope the player is getting stuck and can't move in the changed direction.
    e.g: While touching the slope and walking to the right and pressing vk_left and then vk_right again it's stuck instead of walking.

    The error just occurs when changing direction like: walking right - changing to left - trying to walk right again - stuck.

    Best regards,
    me
     
  2. HaloElite

    HaloElite Member

    Joined:
    Nov 25, 2019
    Posts:
    5
    Okay, I solved it by adding just a little line to the beginning of the code:

    Code:
    if(place_meeting(x, y + 1, solids)) y -= 1;    
    
     
  3. Raff

    Raff Member

    Joined:
    Feb 28, 2018
    Posts:
    50
    Hello,

    Don't know if it will fix the problem but it's better to move first in the x axis and then move in the y axis. Specially with slopes, i think.
     
    HaloElite likes this.
  4. Raff

    Raff Member

    Joined:
    Feb 28, 2018
    Posts:
    50
    Sorry, ignore it, then.
     
  5. HaloElite

    HaloElite Member

    Joined:
    Nov 25, 2019
    Posts:
    5
    All right, thanks for the hint, I'll try it out too!
     

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