Reputation:
I'm trying to implement moving on slopes in GameMaker Studio 2. It's working most of the time but sometimes I'm getting stuck between the slopes and the ground when trying to move upwards.
The code is:
// Stop gravity on slopes
// When changing this to vsp = -1 I'm not getting stuck but the player is
// currently jumping from 1 to 0 pixels...
if(place_meeting(x, y + vsp, slopes) && y > 0) vsp = 0;
And in my moving script:
repeat(abs(hsp)){
if(place_meeting(x + sign(hsp), y, slopes) && !place_meeting(x + sign(hsp), y - 1, slopes)){
y -= 2;
}
if(place_meeting(x + sign(hsp), y, slopes) && !place_meeting(x + sign(hsp), y + 1, slopes) &&
place_meeting(x + sign(hsp), y + 2, slopes)){
y += 2;
}
// Horizontal movement
x += sign(hsp);
}
}
I also created a post here with all of my code: https://forum.yoyogames.com/index.php?threads/diagonal-moving-on-slopes.69667/
Upvotes: 0
Views: 1952
Reputation: 335
Taken from https://www.youtube.com/watch?v=1r1rElIiWqw
//Horizontal Collision
if place_meeting(x+hsp,y,par_wall)
{
yplus = 0;
while (place_meeting(x+hsp,y-yplus,par_wall) && yplus <= abs(1*hsp)) yplus += 1;
if place_meeting(x+hsp,y-yplus,par_wall)
{
while (!place_meeting(x+sign(hsp),y,par_wall)) x+=sign(hsp);
hsp = 0;
}
else
{
y -= yplus
}
}
x += hsp;
// Downward slopes
if !place_meeting(x,y,par_wall) && vsp >= 0 && place_meeting(x,y+2+abs(hsp),par_wall)
{while(!place_meeting(x,y+1,par_wall)) {y += 1;}}
// Vertical Collision
.........
Upvotes: 0