GMS 2 [SOLVED] Stuck to Ground when Jumping in Platformer

Sarena

Member
I'm creating a Platformer game and I've been having some collision issues when it comes to jumping. Whenever my character jumps and lands on a raised platform, they get stuck to it. The same happens if they start on a raised piece of ground and they jump or fall to the ground below. When they jump and land on a flat piece of ground that is level with where they started, they are fine. I'm not sure what's causing the collisions to be so finicky, but I've spent a few hours trying to debug my code.

I've been searching around a lot for answers to this question on the forums, and I haven't been able to find a solution that worked for me. I am using D&D, and just started using GameMaker about a month ago.

Here's what my code looks like currently in my objPlayer's Step Event:
(Keep in mind this is from the Live Preview since I'm using D&D)

Code:
// Key Inputs

// Assign Variable
key_left = keyboard_check(vk_left) or keyboard_check(ord("A"));
key_right = keyboard_check(vk_right) or keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);

// Movement

// Declare Temp
var move = key_right - key_left;

// Assign Variable
hsp = move * walkspeed;

// Assign Variable
vsp = vsp + grv;

// Jump Collisions

// If Object At
var l5FF2BBF4_0 = instance_place(x, y + 1, objWall);
if ((l5FF2BBF4_0 > 0))
{
    // If Variable
    if(!(key_jump == 0))
    {
        // Assign Variable
        vsp = -5;
    }
}

// Horizontal Collisions

// If Object At
var l10CBD9A7_0 = instance_place(x + hsp, y, objWall);
if ((l10CBD9A7_0 > 0))
{
    // Loop
    while(true)
    {
        // If Object At
        var l4E305BEB_0 = instance_place(x + sign(hsp), y, objWall);
        if (!(l4E305BEB_0 > 0))
        {
            // Jump To Point
            x = x + sign(hsp);
        }
    
        // Break
        break;
    }

    // Assign Variable
    hsp = 0;
}

// Jump To Point
x = x + hsp;

// Vertical Collisions

// If Object At
var l38BDDE9F_0 = instance_place(x, y + sign(vsp), objWall);
if ((l38BDDE9F_0 > 0))
{
    // Loop
    while(true)
    {
        // If Object At
        var l5FB9B04A_0 = instance_place(x, y + sign(vsp), objWall);
        if (!(l5FB9B04A_0 > 0))
        {
            // Jump To Point
            
            y = y + sign(vsp);
        }
    
        // Break
        break;
    }

    // Assign Variable
    vsp = 0;
}

// Jump To Point

y = y + vsp;
And then the objPlayer's Create Event looks like this:
Code:
// Assign Variable
hsp = 0;
vsp = 0;
grv = 0.1;
walkspeed = 4;
I'd appreciate any help I can get with this, thanks!
 

hogwater

Member
Probably have sprites of different sizes, and when you change to the landing or idle sprite you get stuck because the mask for the new sprite is already inside the ground when it is applied.

Make a sprite to use as a collision mask, and do all your collisions with that. Also make sure the sprites have even numbered dimensions like 32x32.
 

Sarena

Member
Probably have sprites of different sizes, and when you change to the landing or idle sprite you get stuck because the mask for the new sprite is already inside the ground when it is applied.

Make a sprite to use as a collision mask, and do all your collisions with that. Also make sure the sprites have even numbered dimensions like 32x32.
So far I've only created one sprite that is being used, so I don't think swapping between sprites is the issue right now. I double-checked and my sprite is 64x64 with its origin point at 32,32 in the middle.

I think you were right that it does have to do with the Collision Mask, though. When I changed the Collision mask to cover 1 pixel more downwards, my player was stuck in the ground and could not even move at all.

I did your suggestion of creating a new sprite just for the Collision Mask, but I'm still having the same issue.

Here are some screenshots of what I have set up right now:



Thanks!
 

Sarena

Member
Shout out to Minty Python on the /r/GameMaker Discord server for helping me figure out the issue!
For anyone who comes back to this post and wants the solution:

The issue is with the Vertical Collision. I simply changed:

Code:
var l38BDDE9F_0 = instance_place(x, y + sign(vsp), objWall);
to

Code:
var l38BDDE9F_0 = instance_place(x, y + vsp, objWall);
Removing the
Code:
sign()
was able to do the trick in the initial test because it was checking in both directions, so the player got stuck inside of objects for checking too far initially.
 
Top