• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Caracterter doesn't move left or right when on the ground [fixed]

D

danel

Guest
I'm new to GMS 2 and watched a couple of tutorials. I've been trying to make a simple platformer ( a red square moving on a line of black squares).

Here's the code i've used for the movement of the player.

Code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump  = keyboard_check_pressed(ord("W"));

if(place_meeting(x,y+1,oWall))
on_ground=1;
else
on_ground=0;

//left or right

var move  =  key_right - key_left;

hspeed = move * walksp;

vspeed = vspeed + grv;

if(on_ground && (key_jump))
{
    vspeed = -10;
}

//horizontal

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

x = x + hspeed;

//vertical

if(place_meeting(x,y+vspeed,oWall))
{
    while(!place_meeting(x,y+sign(vspeed),oWall))
        {
            y = y + sign(vspeed);
        }
          
    vspeed = 0;
}
y = y + vspeed;
As I said in the title, the square only moves when i jump, but when i stand on the ground it stays still.
(grv is set to 0.5 and walksp to 4)
 
Top