# GMS 2I can't seem to get my character to work on slopes :/

#### LunarTaqq

##### Member
Hey! This is my first post here and I'm kind of a beginner so I might not know exactly what I'm doing.

I'm attempting to make a platform game, and I am trying to be able to have my character run up slopes. The only kind of slope I want in the game is a 45 degree one, so I know I don't need to do anything too super fancy. I've looked up a couple tutorials and they are all very complicated. Any tips?

Current collision and movement code:

//Player Input
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space) || keyboard_check_pressed(ord("W"));

//Hori Movement
var move = key_right - key_left;
hsp = move * walksp;

//Vert Movement
vsp += grv;

if (place_meeting(x, y + 1, obj_wall)) && (key_jump)
{
vsp = -4.5;
}

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

hsp = 0;
}

x += hsp;

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

Anything will help. Thanks!

-LunarTaqq

#### Attachments

• 55.6 KB Views: 10

#### Yal

##### š§ *penguin noises*
GMC Elder
Here's what I would do.
1. Check if you're on ground. If so, set a loop-start value to 1. Otherwise set it to 0.
2. Loop from the start value to -1, inclusive, in steps of 1.
3. For each iteration in the loop, check if there's a collision 1 movespeed units to the left/right (direction you're moving in), and as many units in the y direction, multiplied with the loop counter.
4. If there's no collision, add movespeed to x, add movespeed*loopvalue to y, and abort the loop.
Or in less programmery terms, check diagonally below you first (but only when you're on the ground! Because when you're jumping, you'll always have free space below you.), then directly to the side, and diagonally upwards last. If any spot is free, move there. You don't need to use a loop for this, but it makes the code less messy.

So why do we do this? Using gravity for downwards slopes looks ugly and is prone to errors, you want to follow the ground smoothly when you're on the ground. Since there's almost always tons of free space above the ground, checking for uphills first would cause lots of false positives, making you levitate diagonally upwards each step. Same thing with not checking for downhills when in mid-air, because it would suck you downwards diagonally.

#### LunarTaqq

##### Member
Thanks for the response, I really appreciate it! I think I'm understanding it for the most part, though when I attempt this to the best of my ability I get a syntax error? Each of the red exclamation points read "unexpected syntax error". Please check the image below. Thanks,

- LunarTaqq

#### LunarTaqq

##### Member
Thanks for the response, I really appreciate it! I think I'm understanding it for the most part, though when I attempt this to the best of my ability I get a syntax error? Each of the red exclamation points read "unexpected syntax error". Please check the image below. Thanks,

- LunarTaqq
nvm I figured it out haha