Making An Object Face The Way It's Moving

There are two main ways of doing this. One is very memory consuming and does not allow animated sprite rotations. The other does allow animated sprites to rotate and takes up much less memory (it also looks better). However, the latter can only be done in Registered or Pro copies of Game Maker. This small tutorial shows both methods.

Pro Only

Using this simple code, the image will simply angle itself to face in the direction it's moving. As a precautionary note, make sure the object's sprite is facing right. If it is not, rotate it so it is. Also, set the origin of the sprite so it is in the centre of the sprite (half its width as x, and half its length as y).

Rotating object: Step event

image_angle=direction

This simply turns around the image until it is facing direction.


Pro or Lite

Using this Lite-friendly code, the sprite will actually be an animation of itself rotating. It will then pick the right sub-image to match direction.

Rotating object: Sprite properties

Firstly, you will need to ensure that the sprite you want to rotate to face direction is initially facing right. If not, go to Transform > Rotate until it is (resize the canvas in the Transform tab first if the sprite's length and width don't match). If you haven't already, set the origin of the sprite so it is in the centre of the sprite (half its width as x, and half its length as y).

Go into Sprite Editor, then click Animation > Set Length. Set the number to whatever. A larger number will mean a smoother animation, but also more memory consumption. Ideally, the number should be divisible by 4, as this will give a "clean" image when facing directly up/down/left/right. A good number to use is 32.

Now, you should have a 32-frame animation. All the frames should be identical. At this stage, go to Animation > Rotation Sequence > Counter Clockwise. The number of frames (all of them) and rotation (360) should be set already, so click OK.

The sprite should now consist of your sprite, which starts facing right, then rotates anti-clockwise until it reaches the start again. Once you've done this, you can get ready to set it up.

Rotating object: Step event

image_index=direction/(360/image_number)

This will work out what direction you're facing, and assign the correct subimage to it.

Written by NAL

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License