Skip to content

Instantly share code, notes, and snippets.

@rubyr
Last active July 21, 2023 00:24
Show Gist options
  • Save rubyr/ed867f569dd30bbfe36d9d0368cfe0e7 to your computer and use it in GitHub Desktop.
Save rubyr/ed867f569dd30bbfe36d9d0368cfe0e7 to your computer and use it in GitHub Desktop.
simple 2d topdown player movement for gamemaker: studio games (and other engines too, if you adapt it)
/// 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
//note: GMS1 will have to define this in "define macros" under "resources"
#macro plrfriction 0.85
/// STEP EVENT
// xxx and yyy can be thought of as acceleration
var 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
// all the same but with Y
yyy = (Input.down - Input.up);
yy += yyy * impulsespd;
yy = clamp(yy, -spd, spd);
if (yyy == 0) yy *= plrfriction;
if (abs(yy) <= 0.05) yy = 0;
if (xx != 0 && yy != 0) { // if the player is moving
var dir = point_direction(0, 0, xx, yy); // get the direction of the player
var mag = min(sqrt(sqr(xx) + sqr(yy)), spd); // get the magnitude of the movement vector (or if it's too big, just the normal speed)
xx = lengthdir_x(mag, dir); // set x velocity to the direction with the magnitude (thereby limiting the player speed diagonally)
yy = lengthdir_y(mag, dir);
}
// ideally you would do collisions here, but it depends on how you're doing it (objects vs. tiles)
// when you do collide however, set xx and yy to the minimum distance you can travel (which might be 0)
// X collision here
x += xx;
// Y collision here
y += yy;
// separate the collisions to avoid weird results when going 45 degrees towards a corner (don't separate them to see what I mean)
@06NinjaKid06
Copy link

input doesn't exist

@rubyr
Copy link
Author

rubyr commented Jul 11, 2023

Ah, I haven't thought about this in years. Anyways, as it mentions in the comments, "Input" should be something representing how you're getting your inputs (in my case, I had an object named Input that I'd add a "Before Step" event to, which would set whatever input variables I needed). Nowadays I'd recommend using something like Input 6, which would handle all of that for you. But then I'd also recommend not using this code at all, it's very basic and doesn't cover anything like collisions, physics, etc.

@06NinjaKid06
Copy link

already found a tutorial, but thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment