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

Legacy GM Implementing slopes (and one-way slopes) on a smooth platformer

LazyTomato

Member
(sigh... i've really been making a lot of questions here lately)
So I've been trying to make a platformer engine with smooth movement and slopes for a future project I'll be making at some point.
So far it's worked perfectly fine, blocks and collision work okay, and i got one-way square blocks (i.e. blocks you can go through from the bottom only) to work. However I've been pulling my hair out trying to implement proper slope collision (and one-way slopes, if possible) into the game, and looking up this problem (as common as it may be) only gave tutorials for platformers with non-smooth movement (i.e. platformers where you stop instantly as soon as you let go of an arrow key), which is definitely not what I'm going for here.
So far the movement code I made up is:
Code:
///Step event, controls and movement
//The keys to use for each thing are set at the beginning of the game to make them changeable
key_left=keyboard_check_direct(global.keys[key.left])
key_right=keyboard_check_direct(global.keys[key.right])
key_up=keyboard_check_direct(global.keys[key.up])
key_down=keyboard_check_direct(global.keys[key.down])
key_attack_l=keyboard_check_pressed(global.keys[key.attack_l])
key_attack_h=keyboard_check_pressed(global.keys[key.attack_h])
key_jump=keyboard_check_pressed(global.keys[key.jump])
key_pause=keyboard_check_pressed(global.keys[key.pause])

//Movement
if (key_left) and hsp>-6
{
 if !place_meeting(x+hsp,y,par_block)
 {
  hsp-=0.2
 }
}
if (key_right) and hsp<6
{
 if !place_meeting(x+hsp,y,par_block)
 {
  hsp+=0.2
 }
}
if !(key_left)
{
 if hsp<0
 {
  hsp+=0.2
  if hsp>0
  {
   hsp=0
  }
 }
}
if !(key_right)
{
 if hsp>0
 {
  hsp-=0.2
  if hsp<0
  {
   hsp=0
  }
 }
}

//Jumping
if (key_jump) and (place_meeting(x,y+1,par_block) or (place_meeting(x,y+1,obj_block_oneway)) and !place_meeting(x,y,obj_block_oneway)) and vsp==0
{
 vsp=-8
}

//Gravity and vertical collision
if vsp<8
{
 vsp += grav;
}
if (place_meeting(x,y+vsp,par_block))
{
 if sign(vsp)>0
 {
  while !place_meeting(x,y+sign(vsp),par_block)
  {
   y+=sign(vsp)
  }
 }
 vsp=0
}
if place_meeting(x,y+vsp,obj_block_oneway) and !place_meeting(x,y,obj_block_oneway) and vsp>0
{
 if sign(vsp)>0
 {
  while (!place_meeting(x,y+sign(vsp),obj_block_oneway))
  {
   y+=sign(vsp);
  }
 }
 vsp=0
}
y += vsp;

//Slopes
if (place_meeting(x,y+1,par_block) and vsp==0) and place_meeting(x+hsp,y,par_block) and !place_meeting(x+hsp,y-abs(hsp)-2,par_block)
{
 y-=abs(hsp)
 while !place_meeting(x,y+1,par_block)
 {
  y+=1
 }
}
else if (place_meeting(x,y+1,par_block) and vsp==0) and !place_meeting(x+hsp,y+1,par_block) and place_meeting(x+hsp,y+abs(vsp)+1,par_block)
{
 y-=abs(hsp)
 while !place_meeting(x,y+1,par_block)
 {
  y+=1
 }
}

//Horizontal collision
if place_meeting(x+hsp,y,par_block) and place_meeting(x,y-abs(hsp)-2,par_block)
{
 x-=hsp
 hsp=0
}
x+=hsp
This should in theory work with slopes... but not only does my movement speed stop increasing when I walk up regular slopes (i keep my current speed but it doesn't increase any further), but I can't go up steeper slopes at all, and going down a slope just works as if I was dropping from a block multiple times.
(i've also noticed that I can get stuck on walls from time to time by jumping in very specific ways but I haven't found an accurate way to replicate that)
Here's an image of my test stage for clarity:

all the sprites are placeholders. thinner blocks are oneways. the left slope works almost perfectly but the right one is wonky and only half-works if I'm going really slowly
In short: Is there a better method for implementing slopes into this kind of engine? (actually that's a stupid question, of course there is... what can you think of?)
 
Top