r/gamemaker • • 14d ago

Help! Movement of enemy GML VISUAL

Ive looked at countless tutorials but i cant find any gml visual coding for movement of an enemy in a platformer game. All i want is for my enemy to move left and when colliding with the edge of platform, switch directions with the sprite also switching and going the opposite direction. I also want to include an animation for when it’s walking too. If anyone can, please help :)

2 Upvotes

1 comment sorted by

0

u/QueasyPrior6654 14d ago

first open your walking sprite in the Sprite Editor, check that its playback speed is above 0 (say 8 to 12 FPS), and set its Origin to Middle Centre or Bottom Centre. if the origin stays at Top Left, flipping the sprite horizontally will teleport the enemy sideways by its full width and can drop it right off the ledge. after changing the origin, re-seat the enemy instance right on top of the platform in the Room Editor so it isn't floating above the floor.

then in GML Visual you only need Create and Step:

  1. Create event:

    • Assign Variable: set dir to -1 (left), walk_spd to 2, and ledge_offset to your collision mask's full pixel width (for example 32 for a full-width 32px sprite).
    • Set Sprite: pick your walking sprite (Sub Image: 0), and add Set Animation Speed set to 1 so the walk cycle loops automatically.
    • Set Instance Scale: set Horizontal to dir (use -dir if your sprite art was drawn facing left) and Vertical to 1, with Relative unchecked.
  2. Step event:

    • add Declare Temp: Name = turn, Value = 0.
    • wall check: add If Object At Place with Object set to your platform/wall object, x = dir * walk_spd, y = 0, and Relative checked. inside it, put Assign Variable turn = 1.
    • ledge check: add a second If Object At Place with Object set to your platform/wall object, x = dir * ledge_offset, y = 4, Relative checked, and Not checked (meaning there is no floor ahead and 4 pixels down). inside it, put Assign Variable turn = 1. because If Object At Place shifts the whole collision box, offsetting by the full mask width turns the enemy right as its leading foot reaches the drop-off; keep ledge_offset as a positive variable instead of sprite_width, since flipping Horizontal scale to -1 makes sprite_width negative in GameMaker.
    • flip when needed: add If Variable turn equal to 1. inside it, put Assign Variable dir = -dir, plus Set Instance Scale with Horizontal = dir (or -dir if drawn facing left) and Vertical = 1 (Relative unchecked).
    • move: at the bottom of Step, add Assign Variable with Name = x, Value = dir * walk_spd, and Relative checked.