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.@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?
I felt the need to post just to say how simple yet brilliant this is.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.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.
hspd_extra = hspd_max - lengthdir_x(hspd_max, 45);
hspd += hspd_extra;
var kright, kleft, kjump, xdir, onground, kjump;
kright = keyboard_check(vk_right);
kleft = keyboard_check(vk_left);
kdown = keyboard_check(vk_down);
kjump = keyboard_check_pressed(vk_up);
xdir = keyboard_check(vk_right) - keyboard_check(vk_left);
onground = place_meeting(x, y + 1, obj_wall);
//check if on ground
if( place_free( x, y+1 )){
vspd += grav;
}
if kjump{
vspd = -vspd_max
}
//set horizontal movement based on controls
if kright
{
hspd = lerp(hspd,lengthdir_x(hspd_max,angle+30),0.25); // does not work if +30 is added
}
if kleft
{
hspd = lerp(hspd,lengthdir_x(-hspd_max,angle-30),0.25); // goes faster if going to the other side witouth -30
}
if !kright && !kleft && !kdown
{
hspd = lerp(hspd,0,0.1); // worlds most basic friction
}
//Find p1
for (i=0;i<=sprite_width;i+=1){
if position_meeting(bbox_left,bbox_bottom+i,obj_wall){
p1=i;
break;
}
}
//Find p2
for (i=0;i<=sprite_width;i+=1){
if position_meeting(bbox_right,bbox_bottom+i,obj_wall){
p2=i;
break;
}
}
//Now, rotate the sprite!
angle = point_direction(bbox_left,bbox_bottom+p1,bbox_right,bbox_bottom+p2);
angle = round(angle);
// Actually move
var dy;
repeat (abs(hspd)) // Horizontal movement
{
if (onground) // Check for slopes if we are on the ground
{
if (place_meeting(x + sign(hspd), y, obj_wall)) // Up slope
{
dy = up_slope(); // Measure the slope
if (dy <= slope_max) // Climbable
{
x += sign(hspd);
y -= dy;
continue;
}
hspd = 0; // Unclimbable
break;
}
else if (!place_meeting(x + sign(hspd), y + 1, obj_wall)) // Down slope (just like we did up slopes)
{
dy = down_slope();
if (dy <= slope_max)
{
x += sign(hspd);
y += dy;
continue;
}
x += sign(hspd); // Continue the loop rather than break the loop to keep momentum
continue;
}
else x += sign(hspd); // Flat
}
else // We're in the air so we don't check for slopes
{
if (!place_meeting(x + sign(hspd), y, obj_wall)) x += sign(hspd);
else
{
hspd = 0;
break;
}
}
}
repeat (abs(vspd)) // Vertical movement
{
if (!place_meeting(x, y + sign(vspd), obj_wall)) y += sign(vspd);
else
{
vspd = 0;
break;
}
}
var angle = 315;
var hspd_extra = hspd - lengthdir_x(hspd, angle);
var hspd_total = hspd + hspd_extra;
repeat (abs(hspd_total))