[Un-Solved] Platformer Going up Slopes

W

Willowwisp001

Guest
What i have so far

Code:
//Get the player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_enter);
key_jump_held = keyboard_check(vk_enter);
key_down = keyboard_check(vk_down);


//React to inputs
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

//Jumps
if (place_meeting(x,y+1,obj_wall))
{
    jumps = jumpsmax;
}
else
{
    if (jumps == jumpsmax) jumps -=1;
}

if (key_jump) && (jumps > 0)
{
    jumps -= 1;
    vsp = -jumpspeed;
}

if (vsp < 0) && (!key_jump_held) vsp = max(vsp,-jumpspeed/2)


var hsp_final = hsp + hsp_carry;
hsp_carry = 0;

//Horizontal Collision
if (place_meeting(x+hsp_final,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp_final),y,obj_wall))
    {
        x += sign(hsp_final);
    }
    hsp_final = 0;
    hsp = 0;
}
x += hsp_final;

//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;

//Animate
if (move!=0) image_xscale = move;
if (place_meeting(x,y+1,obj_wall))
{
    if (move!=0)
    {
        sprite_index = spr_player_run;
        image_speed = 1;
    }
    else sprite_index = spr_player_idle; image_speed = 0.5;
}
else
{
    if (vsp < 0) sprite_index = spr_player_jump; else sprite_index = spr_player_fall;
}
 

sp202

Member
You'd move the player up vertically and move it back down a pixel at a time in a loop while checking for collision.

Something like:
Code:
if place_meeting(x+1,y,obj_slope)=true
{
player.x+=2 //whatever speed you want
player.y-=5 //arbitrary value, depends on the max steepness of climbable slopes you want
while place_meeting(x,y+1,obj_slope)=false
{
player.y+=1
}
}
 
W

Willowwisp001

Guest
Oh nvm it worked thanks, he kinda just hovers up before entering the slope
 

sp202

Member
The code should make him hover before bringing him back down to meet the slope, but it should all happen in one step such that the hovering is not visible. Is the loop working as it should be?
 

Zerb Games

Member
If you are looking for a way to have varying slopes with jagged features, or steepness I would recommend going with a better method such as this:
Code:
repeat (abs(hsp))
{
    
    // Move up the slope
    if (place_meeting(x + sign(hsp), y, obj_slope) && place_meeting(x + sign(hsp), y - 1, obj_slope) && !place_meeting(x + sign(hsp), y - 2, obj_slope))
    {
        y -= 2
    } else {
     if (place_meeting(x + sign(hsp), y, obj_slope) && !place_meeting(x + sign(hsp), y - 1, obj_slope))
        y-=1
     }
   
    // Move down the slope
    if (!place_meeting(x + sign(hsp), y, obj_slope) && !place_meeting(x + sign(hsp), y + 1, obj_slope) && !place_meeting(x + sign(hsp), y + 2, obj_slope) && place_meeting(x + sign(hsp), y + 3, obj_slope))
    {
        y += 2
    } else {
     if (!place_meeting(x + sign(hsp), y, obj_slope) && !place_meeting(x + sign(hsp), y + 1, obj_slope) && place_meeting(x + sign(hsp), y + 2, obj_slope))
        y+= 1
    }
    
    // Move in general
    if (!place_meeting(x + sign(xspeed), y, obj_solid)) {
    x += sign(xspeed);
    } else {
//      move_contact_solid(point_direction(x, y, x + xspeed, y), xspeed);
      xspeed = 0;
      break;
    }
}
This should be able to handle more interesting shapes.
 
W

Willowwisp001

Guest
If you are looking for a way to have varying slopes with jagged features, or steepness I would recommend going with a better method such as this:
Code:
repeat (abs(hsp))
{
  
    // Move up the slope
    if (place_meeting(x + sign(hsp), y, obj_slope) && place_meeting(x + sign(hsp), y - 1, obj_slope) && !place_meeting(x + sign(hsp), y - 2, obj_slope))
    {
        y -= 2
    } else {
     if (place_meeting(x + sign(hsp), y, obj_slope) && !place_meeting(x + sign(hsp), y - 1, obj_slope))
        y-=1
     }
 
    // Move down the slope
    if (!place_meeting(x + sign(hsp), y, obj_slope) && !place_meeting(x + sign(hsp), y + 1, obj_slope) && !place_meeting(x + sign(hsp), y + 2, obj_slope) && place_meeting(x + sign(hsp), y + 3, obj_slope))
    {
        y += 2
    } else {
     if (!place_meeting(x + sign(hsp), y, obj_slope) && !place_meeting(x + sign(hsp), y + 1, obj_slope) && place_meeting(x + sign(hsp), y + 2, obj_slope))
        y+= 1
    }
  
    // Move in general
    if (!place_meeting(x + sign(xspeed), y, obj_solid)) {
    x += sign(xspeed);
    } else {
//      move_contact_solid(point_direction(x, y, x + xspeed, y), xspeed);
      xspeed = 0;
      break;
    }
}
This should be able to handle more interesting shapes.
uh a bit buggy? :0
############################################################################################
FATAL ERROR in
action number 2
of Step Event0
for object obj_player:

Variable obj_player.xspeed(100019, -2147483648) not set before reading it.
at gml_Object_obj_player_StepNormalEvent_2 (line 23) - if (!place_meeting(x + sign(xspeed), y, obj_wall)) {
############################################################################################
 

Zerb Games

Member
uh a bit buggy? :0
############################################################################################
FATAL ERROR in
action number 2
of Step Event0
for object obj_player:

Variable obj_player.xspeed(100019, -2147483648) not set before reading it.
at gml_Object_obj_player_StepNormalEvent_2 (line 23) - if (!place_meeting(x + sign(xspeed), y, obj_wall)) {
############################################################################################
Ehh read the error their m8. Swap xspeed for hsp, understand what you are copy and pasting bruh.

// Move in general
if (!place_meeting(x + sign(xspeed), y, obj_solid)) {
x += sign(xspeed);
} else {
// move_contact_solid(point_direction(x, y, x + xspeed, y), xspeed);
xspeed = 0;
break;
}
}

All of this^ you don't need.
 
Top