GML Slopes and Platformer Movement for Beginners

JeanSwamp

Member
@Bentley I'm playing around a bit with this, trying to set an acel/dec with down/up slopes, but seems quite hard the way the scripts work, and I'm not a fan of your suggestion of creating some sort of climb power based on dy.

Any suggestions?
 

Bentley

Member
@Bentley I'm playing around a bit with this, trying to set an acel/dec with down/up slopes, but seems quite hard the way the scripts work, and I'm not a fan of your suggestion of creating some sort of climb power based on dy.

Any suggestions?
Hey Jean, I had something a while ago when I was working on a Kirby's Adventure clone (I use the word "clone" very loosely). Basically, if he was going uphill, I would get the direction from Kirby's feet to the slope. I'd then use lengthdir_x(hspd, dir_of_slope) and move the player that distance. So his speed is decreased uphill.

This tutorial was specifically for slopes on destroyable terrain. If your slopes are pre-defined (you have sprites for slopes that aren't created in-game), you can just get the direction by looking at the slope. For example, if the sprite is a diagonal at 45 degrees, you'd input that:

// Pseudo code
hspd = lengthdir_x(hspd_max, 45);

You do the opposite downhill. I'll elaborate / write some code if you reply.
 
Last edited:
Hey Jean, I had something a while ago when I was working on a Kirby's Adventure clone (I use the word "clone" very loosely). Basically, if he was going uphill, I would get the direction from Kirby's feet to the slope. I'd then use lengthdir_x(hspd, dir_of_slope) and move the player that distance. So his speed is decreased uphill.

This tutorial was specifically for slopes on destroyable terrain. If your slopes are pre-defined (you have sprites for slopes that aren't created in-game), you can just get the direction by looking at the slope. For example, if the sprite is a diagonal at 45 degrees, you'd input that:

// Pseudo code
hspd = lengthdir_x(hspd_max, 45);

You do the opposite downhill. I'll elaborate / write some code if you reply.
I felt the need to post just to say how simple yet brilliant this is.
 

JeanSwamp

Member
Hey Jean, I had something a while ago when I was working on a Kirby's Adventure clone (I use the word "clone" very loosely). Basically, if he was going uphill, I would get the direction from Kirby's feet to the slope. I'd then use lengthdir_x(hspd, dir_of_slope) and move the player that distance. So his speed is decreased uphill.

This tutorial was specifically for slopes on destroyable terrain. If your slopes are pre-defined (you have sprites for slopes that aren't created in-game), you can just get the direction by looking at the slope. For example, if the sprite is a diagonal at 45 degrees, you'd input that:

// Pseudo code
hspd = lengthdir_x(hspd_max, 45);

You do the opposite downhill. I'll elaborate / write some code if you reply.
I calculate the angle of the slope I'm about to collide to set the sprite rotation accordingly, so I guess is similar approach to your lengthdir_x(hspd, dir_of_slope) concept. I only move forward as I'm using this for some sort of minecart mini game.

When you say, do the opposite for downhill, what do you mean? Can't really figure it out
 

Bentley

Member
@JeanSwamp You could move at hspd_max and add additional movement based on the slope
Code:
hspd_extra = hspd_max - lengthdir_x(hspd_max, 45);
hspd += hspd_extra;
Something like that. You just find out how much hspd you didn't move b/c of the slope and add that to your current speed.

Edit: and obviously this doesn't account for what you need to do when you are floating above the slope. I'd just use a while loop and move him to the ground.
 
Last edited:

SoMalFra

Member
Hey, so I'm really sorry for necroposting on this thread but you know, there is always hope!
I was using this code for a simple platformer, however I've come into a bug. If the player is touching a wall and facing it, when he jumps and look to the other side, two things can happen: 1:The player totally stop moving up and just fall, like the jump speed didn't matter, or 2:, It can just get stuck in the wall. It's not a problem with collision mask nor the sprite origin. If you can help me, I'd really appreciate.
Again, sorry for bothering and necroposting.
It has already happened in this threat and you answered, so I guess ir's no harm trying.
Thank you for the help, though!
 

Bentley

Member
@SoMalFra Hey, I'm happy to try and help. My first thought would be a mask but you said that it's not that. Just to double check, when the sprite changes is the same collision masked used with the same origin? I ask because if it's slightly wider, you'll get stuck/weird things will happen. You could use a few "show_message" calls when you're in the circumstance you described to see if a collision is occurring when it shouldn't be. If that doesn't work, let me know.
 
Top