How to Animate Objects in GameMaker


How to Animate Objects in GameMaker

Do you want to add cool animations to your game? You're in the right place.

In this tutorial, we'll use Sequences to turn this:

Into this:


Watch the video tutorial here:

Open Your Project

Create a new project using the Windy Woods template. You can choose either GML Code or GML Visual:

undefined

You can use your existing Windy Woods project if you already have one.

How to Create an Animation in GameMaker

From the Asset Browser, create a new Sequence asset:

undefined

Name it seq_coin.

undefined

Drag obj_coin into the track panel.

You can also drag it onto the canvas. If you do, reposition the coin to the center of the sequence if necessary.

Stretch the asset key by right clicking on the track and choosing Stretch Asset Key.

Set the sequence length to 54 frames.

undefined

The coin track will be automatically resized if you used the Stretch Asset Key option. If you adjusted the track length manually, you'll need to resize it.

Change the playback mode to Loop.

undefined

Add a position track if it doesn't already exist.

Add keyframes at 0f, 27f, and 54f.

Move the playhead to frame 27.

undefined

Select the coin and move it up a bit.

Play the animation, and the coin will come alive!

Using Curves

We're getting somewhere, but the animation is a little robotic. Let's make it realistic by using curves.

Expand your obj_coin track, and select the Position parameter.

undefined

Enable Curve Mode.

undefined

Convert your selected parameter into a curve.

undefined

Select the Y track, because we're animating the coin vertically.

undefined

Open the Curve Library undefined and choose Bezier.

undefined

This should do the job, but you can customise it ever further.


Click on a point in the curve, and then drag the end of the handles to extend them. This will make the final animation smoother than ever!

Placing in Level

From the Asset Browser, open the rm_level_1 room:

undefined

Create a new Asset Layer called "Sequences".

undefined

Drag and drop the seq_coin Sequence in the room, right next to the player.

undefined

Because we used obj_coin inside of our sequence, all of that object's logic is still present in the game.

So even though we used a sequence to animate the coins, they're still collectable!

Update Existing Coins

We finally have animating coins, but there's a small problem as well. What do we do with all the coins that we already have in the room?

You could go through every level and replace every coin with your new coin sequence.

However, we can automate this: when a coin instance is created without a Sequence, it destroys itself, and creates the Sequence immediately.

Open obj_coin's Create event.

You can find obj_coin under Objects -> Environment -> Items.

Easily search for any asset by using the keyboard shortcut CTRL/CMD T.

Add the following GML code or Visual actions:

if(!in_sequence)
{
        layer_sequence_create("Sequences", x, y, seq_coin);
        instance_destroy();
}

undefined

Now, every instance of obj_coin in your game will check whether it's part of a sequence.

If not, it'll create the seq_coin sequence in its place and destroy itself.

How to Create a Pickup Animation in GameMaker

We've got a looping, idle animation in-game.

Now, create a one-time animation that plays when a coin is collected.

Create a new sequence and name it seq_coin_collect. Instead of adding obj_coin to the track panel, add spr_coin, which is the sprite.

Play around with the Sequence editor to make your own coin collect animation!

Here's what mine looks like. In addition to animating spr_coin's position, rotation, and scale, I added the spr_coin_collect_effect at the end for some extra flair.

It's important to use the sprite here and not the object, because we don't want any of the coin object's logic in the collect animation.

Open obj_player's collision event with obj_coin, and add the following code after the instance_destroy() line or Destroy Instance action:

layer_sequence_create("Sequences", other.x, other.y, seq_coin_collect);

undefined

This code uses GameMaker's built-in functions and Actions to create a sequence at runtime.

And that's it! If we run the game and collect a coin, our new animation is there.

Summary

Here are some key takeaways from this tutorial:

Read more tutorials to build your game.

Happy GameMaking!