Legacy GM [SOLVED] Built-in speed/state change interfering with slopes...?

Hola!

I'm having a crack at an endless runner variation, where instead of objects coming towards the player, the player is constantly moving forward. I've tried modifying an existing movement script so that this happens, and it works, but every time I change into the attack state on a slope (going up) the player object 'sinks' into the slope.

I think/suspect it may have something to do with using speed as a variable for checks as opposed to x [ie. in the Player Create Event, I put speed = 1 and spd = 1, the idea being that spd is the variable that will increase, with speed remaining constant as a control ie. movement = speed x spd. I don't know if this is unnecessary or not, but it seems to make sense to me!].

The thing is, because I'm not using any buttons to move forward, I'm not sure how to tweak the movement code so that it uses x += 1 instead (for example). [This is for a one button game jam - (W) attacks with a quick press, or jumps when held for a moment then released, which both work fine, too]

Alternatively, maybe it's how I've set up the attack change state? Maybe this needs an extra condition? The sprite mask is the same as the player and I have no issues changing states/with collisions on a flat/horizontal surface.

Here's the code! Gracias for any suggestions!
------------------------------
Code:
//React to inputs
move = speed;
hsp = move * spd;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_wall))
{
    on_ground = false;
    vsp = key_up * -jumpspeed
}

else if (!place_meeting(x,y+2,obj_wall))
{
    on_ground = true;
}

//Horizontal Collision - the number in <= abs (4*hspd) affects how steep a slope the player can go up
// lower numbers = very slight slopes, higher numbers = very steep, with 1 being typical.
//Horizontal Collision
if place_meeting(x+hsp,y,obj_wall)
{
    yplus = 0;
    while (place_meeting(x+hsp,y-yplus,obj_wall) && yplus <=abs (1*hsp)) yplus += 1;
    if place_meeting(x+hsp,y-yplus,obj_wall)
    {
       while (!place_meeting(x+sign(hsp),y,obj_wall))
       {
          x += sign(hsp);
       }
       hsp = 0;
    }
    else
    {
       y -= yplus
    }
}
x += hsp;

//Down slopes
if !place_meeting(x,y,obj_wall) && vsp >= 0 && place_meeting(x,y+2+abs(hsp),obj_wall)
{while(!place_meeting(x,y+1,obj_wall)) {y += 1;}}

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
   while(!place_meeting(x,y+sign(vsp),obj_wall))
   {
       y += sign(vsp);
   }
   vsp = 0;
}
y += vsp;

// check if on the ground or not after collision
on_ground = place_meeting(x, y+1, obj_wall);

//Change states
if (keyboard_check_pressed(ord("W")))
    {
    state = "Attack";
    image_index = 0;
    }
[The on_ground variable is used for animation states, which I've left out here because there's no issue with those]
 

TheouAegis

Member
since you are using speed, gamemaker is going to update your players x coordinate no matter what is in front of him. Your codesince y stops your custom speed variables. It does nothing to stop the built-in speed variable. Perhaps instead of checking if x+sign(hsp) is free, check if x+sign(hsp)+speed is free.
 
Thanks for the reply, Theou!

I actually figured it out a little while ago, and the answer was a lot easier than I imagined. Simply, I set the hsp to a constant, then added a spd variable in the player object that can be altered, meaning the 'original' collision code functioned as normal, like thus (so if this chunk of code helps anyone else, have at it!*):

[also, this code allows the player to use the key_move to do a nifty boost, then jump!]

Code:
///scr_move_state_endless_runner
scr_get_input();
if state == "Move"
{
move = key_move
hsp = move * spd;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_wall))
{
    on_ground = false;
    vsp = key_up * -jumpspeed
}

else if (!place_meeting(x,y+2,obj_wall))
{
    on_ground = true;
}

//Friction
if(!key_move)
{
    hsp = 2   //((abs(hsp)- fric) * sign(hsp));
}
else
{
    hsp = move * spd;
   
}

//Horizontal Collision - the number in <= abs (4*hspd) affects how steep a slope the player can go up
// lower numbers = very slight slopes, higher numbers = very steep, with 1 being typical.
//Horizontal Collision
if place_meeting(x+hsp,y,obj_wall)
{
    yplus = 0;
    while (place_meeting(x+hsp,y-yplus,obj_wall) && yplus <=abs (1*hsp)) yplus += 1;
    if place_meeting(x+hsp,y-yplus,obj_wall)
    {
       while (!place_meeting(x+sign(hsp),y,obj_wall))
       {
          x += sign(hsp);
       }
       hsp = 0;
    }
    else
    {
       y -= yplus
    }
}
x += hsp;

//Down slopes
if !place_meeting(x,y,obj_wall) && vsp >= 0 && place_meeting(x,y+2+abs(hsp),obj_wall)
{while(!place_meeting(x,y+1,obj_wall)) {y += 1;}}

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
   while(!place_meeting(x,y+sign(vsp),obj_wall))
   {
       y += sign(vsp);
   }
   vsp = 0;
}
y += vsp;
*again, the on_ground variable refers to animation/certain effects [not included here], not actual movement.
 
Top