How to Create 2D Animation in Godot Using AnimatedSprite

In game development, animations play a crucial role in creating an engaging and immersive experience for players. Adding 2D animations to your Godot game can bring your characters and environments to life, making them more visually appealing and interactive.

Fortunately, Godot provides powerful tools and features to create and control 2D animations easily.

4

Setting Up the Godot Game

To begin, set up a basic 2D game scene inthe Godot game engine. Create a new scene and add aKinematicBody2Dnode as the player character. Inside theKinematicBody2D, add aCollisionShape2Dwith a rectangle shape that represents the player’s collision boundaries.

The code used in this article is available in thisGitHub repositoryand is free for you to use under the MIT license.

Spark Mail app in window 11.

Additionally, add anAnimatedSpritenode to handle the player’s animations. Furthermore, make sure that you map the following input actions in yourInput Map:

Next, write the GDScript code to control the player’s movement. Attach the following script to theKinematicBody2Dnode:

netflix logo with popcorn and film board

This script sets up a constant speed for the player and allows them to move left, right, up, and down using the arrow keys or WASD.

Adding SpriteSheet in AnimatedSprite

Now, configure theAnimatedSpriteto use a sprite sheet for animations. Select theAnimatedSpritenode and navigate to theFramessection in the node’s properties tab. Here, click on theNew SpriteFramesbutton.

Switch to theSpriteFramestab located at the bottom of the Godot editor. In theSpriteFramestab, click on theNew Animationbutton. Create animations such aswalkandidleby adding appropriate frames to each animation.

Several laptops side by side with different Linux distributions.

Additionally, you have the option to create other animations, such as shooting, jumping, and climbingfor a platformer game. After that, click on theAdd Frames from SpriteSheetbutton to automatically extract individual frames from the sprite sheet.

Controlling Animations Using GDScript

Now that you have your animations set up, you could control them programmatically using GDScript.

Playing and Stopping the Animation

Controlling the playback of animations is essential to provide dynamic and interactive experiences in your game. TheAnimatedSpritenode in Godot offers methods to play and stop animations as per your game logic.

Extend theKinematicBody2Dnode and handle the animation control within the_physics_processfunction. it’s possible to use theplay_animationandstop_animationinputs to trigger the corresponding animation actions.

man animating objects in a pc

By mapping the appropriate input actions, you can provide players with control over the animation playback in your game.

For example, you might bind theplay_animationaction to a button press or a specific event in your game, allowing the player to trigger an animation sequence at a desired moment. Additionally, you canfind copyright-free musicto play while the animation is running.

Similarly, you can trigger thestop_animationactions to completely stop the animation.

By incorporating these animation control mechanisms, you can add depth and interactivity to your game’s animations, creating more engaging and immersive experiences for your players.

Rotating the Animation

Rotating the animation can add visual interest and variety to your game. You can programmatically rotate theAnimatedSpritenode to change the orientation of the animation. You can apply the rotation in degrees using therotate()method.

When you press the buttons associated with therotate_animation(it’s possible to define this action in your input map), therotate()method is called on theAnimatedSpritenode. It rotates the node by 45 degrees in a clockwise direction usingdeg2rad()to convert the degrees to radians.

Keep in mind that it will apply rotation to the entireAnimatedSpritenode, including all frames of the animation. Therefore, if you want to rotate only specific frames, you may need to split them into separateAnimatedSpritenodes or use other techniques such as flipping individual frames.

Flipping the Animation

Flipping the animation horizontally or vertically can be useful for reflecting changes in character direction. In Godot, theAnimatedSpritenode provides properties to control flipping.

To flip the animation horizontally, set theflip_hproperty of theAnimatedSpritetotrue. This will mirror the animation along the horizontal axis. Similarly, setting theflip_vproperty totruewill mirror the animation along the vertical axis.

If the player presses theflip_animationinput action then set theflip_hproperty of theAnimatedSpritetotrue. This will flip the animation horizontally.

Utilizing Signals in AnimatedSprite

In addition to controlling animations programmatically, Godot provides a powerful event system called signals. Signals allow you to respond to specific events or changes that occur during the execution of your game.

In the case ofAnimatedSprite, there are two important signals that you can use:animation_finished()andframe_changed().

1. animation_finished() Signal

Theanimation_finished()signal is emitted when the animation reaches the last frame, either during a single playback or when it loops. This signal is useful when you want to perform actions or trigger events when an animation completes.

Connect theanimation_finished()signal of theAnimatedSpriteto the_on_animation_finished()method in the same script using theconnect()function.

When the animation finishes playing, you can execute custom logic or trigger other functionalities using the_on_animation_finished()method.

2. frame_changed() Signal

Theframe_changed()signal is emitted whenever the current frame of the animation changes. This can occur when the animation is playing or when you programmatically modify the frame. You can use this signal to detect frame changes and react accordingly.

Connect theframe_changed()signal of theAnimatedSpriteto the_on_frame_changed()method in the same script. When the frame changes, you can access the current frame using the_on_frame_changed()method and perform actions or logic based on the frame’s value.

By utilizing signals, you could respond to animation events such as completion or frame changes and incorporate dynamic behaviors or trigger specific actions in your game.

Make Godot Games More Engaging Using Animations

Adding 2D animations to your Godot games can greatly enhance the overall player experience. Animations bring characters to life, making their movements and actions more visually appealing. By incorporating animations for various actions such as walking, running, attacking, and jumping, you can create a dynamic and immersive gameplay environment.

Moreover, you can also use animations to provide visual feedback to the player. This feedback helps to make the game more engaging and responsive, enhancing the player’s sense of control and involvement.

Need an open source tool for game development? Here are 10 reasons why Godot Engine might be just what you’re look for.

Every squeak is your PC’s way of crying for help.

You don’t need to fork out for expensive hardware to run an AI on your PC.

I found my TV was always listening—so I shut it down.

Sometimes the smallest cleaning habit makes the biggest mess.

Obsidian finally feels complete.

Technology Explained

PC & Mobile