我正在尝试在GameMaker Studio 2中实现在斜坡上移动。它大部分时间都在工作,但是有时候当我试图向上移动时,我会陷在斜坡和地面之间。
代码是:
// 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;
在我的移动脚本中:
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);
}
}
我还在这里用我所有的代码创建了一个帖子:https://forum.yoyogames.com/index.php?threads/diagonal-moving-on-slopes.69667/
取自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
.........