How do i make slopes for an RPG

Raidenc04

Member
I want to create corners in my rpg but instead of being able to walk into the corner instead it pushes you from one side of the corner to the other side as if a slope is there. so basically what im saying is making invisible slope collisions that are blocking the player from actually touching the corners.

My idea was to make it sort of like in undertale when there are edges you cant want in between them but instead it pushes you.
 
I made objects called oCorner and placed them at each corner. Then in the character object, I got the distance to the nearest corner. If the player is too close, it will slide them away.

Here's the small GMS2 project:
https://drive.google.com/file/d/1jpnm_Oi3eHuqcZhSGNJb4f7AppENzJ39/view?usp=sharing

And the code if you don't want to download the project. Just replace 'oWall' with your wall object or parent. And 'oCorner' with whatever you name your corner object.

Create event of player:
Code:
sloping = false;
slope_spd = 0;
slope_dir = 0;
move_dir = 0;
move_spd = 3;
hsp = 0;
vsp = 0;
cornerThreshold = 24; //Distance to corner that triggers a slope slide (Distance is from origin of character)
slideDistance = cornerThreshold*1.6; //Change 1.6 to whatever you want (>1)
Step event of player:
Code:
if(sloping){
    //Slope sliding
    slope_spd+=.1;
    hsp = lengthdir_x(slope_spd,slope_dir);
    vsp = lengthdir_y(slope_spd,slope_dir);
    if(point_distance(x,y,nearestCorner.x,nearestCorner.y)>slideDistance){
        sloping = false;
    }
}else{
    //Movement
    left = keyboard_check(vk_left);
    right = keyboard_check(vk_right);
    up = keyboard_check(vk_up);
    down = keyboard_check(vk_down);
    move_dir = point_direction(0,0,right-left,down-up);
 
    if(left||right||up||down){
        //This allows diagonal movement at the same speed as horizontal movement
        hsp=lengthdir_x(move_spd,move_dir);
        vsp=lengthdir_y(move_spd,move_dir);
    }else{
        hsp = 0;
        vsp = 0;
    }
 
    //Corner detection
    nearestCorner = instance_nearest(x,y,oCorner);
    if(point_distance(x,y,nearestCorner.x,nearestCorner.y)<cornerThreshold){
        sloping = true;
        slope_dir = point_direction(nearestCorner.x,nearestCorner.y,x,y);
        slope_spd = .5;
    }
}

//Collisions
if(place_meeting(x+hsp,y,oWall)){
    while(!place_meeting(x+sign(hsp),y,oWall)){
        x+=sign(hsp);
    }
    hsp = 0;
}
x+=hsp;

if(place_meeting(x,y+vsp,oWall)){
    while(!place_meeting(x,y+sign(vsp),oWall)){
        y+=sign(vsp);
    }
    vsp = 0;
}
y+=vsp;
If you have any questions, let me know :)
 
Top