Skip to content

Instantly share code, notes, and snippets.

@rubyr
Last active February 23, 2019 08:03
Show Gist options
  • Save rubyr/53b3a4d572c8020e522ae98627aad834 to your computer and use it in GitHub Desktop.
Save rubyr/53b3a4d572c8020e522ae98627aad834 to your computer and use it in GitHub Desktop.
simple platformer movement for gamemaker: studio (could work for other engines too, with some modifications)
/// CREATE EVENT
xx = 0; // xx and yy are velocities, for x and y respectively
yy = 0;
spd = 3; // pixels per frame; if using delta timing it will be pixels per second
impulsespd = 0.3; // play around with this
jump_impulse = -10; // negative, because up is negative on the Y
//note: GMS1 will have to define this in "define macros" under "resources"
#macro plrfriction 0.85
#macro grav 0.98
#macro terminal_velocity 16 // this should ideally be the smallest thing that you could collide with (unless you do multiple checks for collision)
/// STEP EVENT
// xxx and yyy can be thought of as acceleration
xxx = (Input.right - Input.left); // Input.left is just however you're getting your left movement input (same with right)
xx += xxx * impulsespd; // impulsespd is how fast the movement is added to xx
xx = clamp(xx, -spd, spd); // clamp will return a number that is "clamped" between the 2nd parameter (-spd here) and 3rd parameter
if (xxx == 0) xx *= plrfriction; // if no input slow player down with friction
if (abs(xx) <= 0.05) xx = 0; // if velocity is too small just make it 0
yy += grav;
if (yy > terminal_velocity) yy = terminal_velocity;
if (Input.jump) // NOTE: double jumps can be added by making a separate variable to track however many jumps the player has left,
if (is_grounded()) // left, and then checking if it is above 0 (do a separate check every frame to see if the player's grounded and
yy = jump_impulse; // if so set to 2 (or however many)). Decrease the variable here as well
// x collision here
x += xx;
// Y collisions here
y += yy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment