GameMaker [Solved] Collision is Preventing Jump

Papa Doge

Member
I'm new to GML (and coding in general) and am having a difficult time figuring out how basic collisions work. I've watched quite a few tutorials now and the code that I'm using seems to be pretty standard.

My issue, is that while my player object does indeed stop where it's supposed to, once the player is butted up against a vertical wall (left or right of the player) I am no longer able to jump up. In fact, all the player is able to do is move in the opposite direction of the wall while on the ground. This presents a problem every time the player wants to jump over a ledge while touching said ledge.

Code:
// Collision Checking

// for horizontal movement...
if (place_meeting(x+hsp,y,obj_invisible_wall))
{
    // As long as their is NOT a collision within 1 pixel of the direction of movement...
    while (!place_meeting(x+sign(hsp),y,obj_invisible_wall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
} else {
    // Change the X coordinate to be the object's current position plus horizontal speed.
    x = x + hsp;
}

// for vertical movement...
if (place_meeting(x,y+vsp,obj_invisible_wall))
{
    // As long as there is NOT a collision within 1 pixel of the direction of movement...
    while (!place_meeting(x,y+sign(vsp),obj_invisible_wall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
} else {
    // Change the X coordinate to be the object's current position plus vertical speed.
    y = y + vsp;
}
 

Attachments

TheouAegis

Member
Hmmm...

Check you are sprites. you said this happens when moving left or right and running into a wall. Make sure your running sprite and idle sprite have the same bounding box width and that the origin is the same distance from the bounding box edges.
 

Papa Doge

Member
Hmmm...

Check you are sprites. you said this happens when moving left or right and running into a wall. Make sure your running sprite and idle sprite have the same bounding box width and that the origin is the same distance from the bounding box edges.
Bingo!

I had the collision mask for my player object set to "same as sprite". When I checked the collision mask on my jumping animation I noticed that it was wider than my idle or running sprites which was causing the player to get stuck in the wall as soon as a jump was executed.

My solution was to change the collision mask to a specific sprite — my idle player sprite.

I will do this moving forward as a best practice. Thanks so much for your help, TheouAegis. I would have spent hours trying to fix the code when the problem was elsewhere all along.
 
Top