r/gamemaker • u/cappykartt • 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
0
u/QueasyPrior6654 14d ago
first open your walking sprite in the
Sprite Editor, check that its playback speed is above0(say8to12FPS), and set itsOrigintoMiddle CentreorBottom Centre. if the origin stays atTop 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 theRoom Editorso it isn't floating above the floor.then in
GML Visualyou only needCreateandStep:Createevent:dirto-1(left),walk_spdto2, andledge_offsetto your collision mask's full pixel width (for example32for a full-width 32px sprite).Sub Image: 0), and add Set Animation Speed set to1so the walk cycle loops automatically.Horizontaltodir(use-dirif your sprite art was drawn facing left) andVerticalto1, withRelativeunchecked.Stepevent:Name=turn,Value=0.Objectset to your platform/wall object,x=dir * walk_spd,y=0, andRelativechecked. inside it, put Assign Variableturn=1.Objectset to your platform/wall object,x=dir * ledge_offset,y=4,Relativechecked, andNotchecked (meaning there is no floor ahead and4pixels down). inside it, put Assign Variableturn=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; keepledge_offsetas a positive variable instead ofsprite_width, since flippingHorizontalscale to-1makessprite_widthnegative in GameMaker.turnequal to1. inside it, put Assign Variabledir=-dir, plus Set Instance Scale withHorizontal=dir(or-dirif drawn facing left) andVertical=1(Relativeunchecked).Step, add Assign Variable withName=x,Value=dir * walk_spd, andRelativechecked.