I'm very much new to this, so I've been following this tutorial. My problem is that tutorial is 6 years old, so stuff like this sometimes happens, and since I don't understand GML yet, I have to poke around to find the solution.

Basically, the player object is sliding downward constantly, when it reaches the floor, the player is frozen and unresponsive to movement commands except for jumping. It rapidly displays both jumping and idle sprites. I can't really take a picture of it since it changes every single frame.

Create Event
grv = 0.2; //gravity
hsp = 0; //current horizontal speed
vsp = 0; //current vertical speed
hspWalk = 3.5; //walk speed
vspJump = -6; //jump speed
canJump =0; //are you touching the ground?




Step Event
//Get Player Input
var _keyLeft = keyboard_check(vk_left);
var _keyRight = keyboard_check(vk_right);
var _keyJump = keyboard_check_pressed(vk_space);

//Work out where to move horizontally
hsp = (_keyRight - _keyLeft) * hspWalk;

//Work out where to move vertically
vsp = vsp + grv;

//Work out if we should jump
if (canJump -- > 0) && (_keyJump)
{
    vsp = vspJump;
    canJump = 0
}

//Collide and Move
if (place_meeting(x + hsp, y, obj_wall))
{
    while (abs(hsp) > 0.1)
    {
        hsp *= 0.5;
        if (!place_meeting(x + hsp, y, obj_wall)) x += hsp;
    }
    hsp = 0;
}
x += hsp;

if (place_meeting(x, y + vsp, obj_wall))
{
    if (vsp > 0) canJump= 7;
    while (abs(vsp) > 0.1)
    {
        vsp *= 0.5;
        if (!place_meeting(x, y + vsp, obj_wall)) y += vsp;
    }
    vsp = 0;
}
y+= vsp;

//Animation
if (!place_meeting (x,y+1,obj_wall))
{
    sprite_index = spr_player_jumping;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
    image_speed = 1;
    if (hsp == 0)
    {
        sprite_index = spr_player_idle;
    }
    else
    {
        sprite_index = spr_player_running;
    }
}

There is no definitive error here that prevents it from compiling, it's more of a bug. Not sure what to write here, I can't really try much since I don't know what I can try. I fed it to ChatGPT to see if it could explain it for me, but all I got was generic advice.

1

There are 1 answers

0
Steven On

I think it's the way it lands on solid ground, and doesn't notice correctly that it lands on the ground.

so let's see what it means:

if (place_meeting(x, y + vsp, obj_wall)) //Checks if there's solid ground between your character's current frame, and the next frame it will be.
{
    if (vsp > 0) canJump= 7; //If you're still falling, you can jump(?) 
    while (abs(vsp) > 0.1) //This loop should move your character bit by bit to the ground, until it reaches the ground, and then stop moving.
    {
        vsp *= 0.5;
        if (!place_meeting(x, y + vsp, obj_wall)) y += vsp;
    }
    vsp = 0; //stops falling.
}
y+= vsp; //adds the vertical speed to the y axis (does not move if it's 0).

So, first off, have you set your Sprite's Origin Point at Bottom-Center (or the bottom of the hitbox)? Because if it's true center, then it doesn't take half of the object's height into account.

I recommend using a variable onGround (perhaps instead of canJump) And use that variable for every place where you check if you're standing on ground. place_meeting(x,y+1,obj_wall) works on paper, but only if your Origin Point is at the very bottom of your collision hitbox. You either want to move that point downwards, or increase the y+1 more to take the height into account. I prefer having a variable for this so it only needs to be dealt with it once.

Another thing is, does Gravity also gets ignored when you're setting vsp to 0? If you're on the ground, then Gravity should not take into account.

The idea of the while loop moving the object bit by bit to the ground is correct, and the tutorial should probably still be fine. I've used a slight variation of it to actually check per-pixel rather than halving the vertical speed. Perhaps that could be a possible solution.

Platforming Collission Checks are probably the hardest part of making a platforming game. But from what I see, you're on the right track. Hope these tips may help.