• 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!

[SOLVED] getting stuck while walking on slopes

H

HaloElite

Guest
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
 
H

HaloElite

Guest
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;
 
R

Raff

Guest
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.
 
H

HaloElite

Guest
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.
All right, thanks for the hint, I'll try it out too!
 
Top