GML Need Help With Object Collisions

K

Krano Network Squirrell

Guest
So I am trying to make a platformer game where the you can jump off the ground and do movement stuff, problem being that I cannot fix my code and when bug testing the character would not move unless he was off the object. This is my code so far, and I need help:

Code:
///actions
//Get the player's input
key_right = keyboard_check(ord("D")) || (gamepad_axis_value(0,gp_axislh) > 0);
key_left = keyboard_check(ord("A")) || (gamepad_axis_value(0,gp_axislh) < 0);
key_jump = keyboard_check_pressed(vk_space) || (gamepad_button_check_pressed(0,gp_face1));

//react to imputs
var move = key_right - key_left;

hsp = move * movespeed

//calculate movement

vsp = vsp + grav;

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


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

//V Collision
if (place_meeting(x,y+vsp,obj_wall))
    {
        while(!place_meeting(y+sign(vsp),y,obj_wall))
        {
            y = y + sign(vsp);
        }
        vsp = 0;
    }

y = y + vsp;
Code:
/// Create verables
grav = 0.2;
hsp = 0;
vsp = 0;
movespeed = 6;

gamepad_set_axis_deadzone(0,0.1)
 

Simon Gust

Member
Your collision code is a bit messed up.
The while() part in the V collision has the y + sign(vsp) where x should be and only y where y + sign(vsp) should be.
Code:
//V Collision
if (place_meeting(x,y+vsp,obj_wall))
{
       while(!place_meeting(x, y+sign(vsp),obj_wall))
       {
           y = y + sign(vsp);
       }
       vsp = 0;
}
y = y + vsp;
 
K

Krano Network Squirrell

Guest
Your collision code is a bit messed up.
The while() part in the V collision has the y + sign(vsp) where x should be and only y where y + sign(vsp) should be.
Code:
//V Collision
if (place_meeting(x,y+vsp,obj_wall))
{
       while(!place_meeting(x, y+sign(vsp),obj_wall))
       {
           y = y + sign(vsp);
       }
       vsp = 0;
}
y = y + vsp;
Thanks, I'm new to this so I have a lot to learn...
 
Top