1 min read

[12/50] Controlling sprite sheet animations with the flipbook node in Unreal

Stable Diffusion returns this image for the prompt: An artwork showing an animation sprite sheet.
Stable Diffusion returns this image for the prompt: An artwork showing an animation sprite sheet.

Sprite sheets allow for an efficient way to render 2D animations and we use a bunch of these in our environment and UI. Being able to control the animation of the sprite sheet within Blueprint or C++ gives us the ability to trigger these sprite animations in response to specific events within the game-play or player interactions.

0:00
/
Screengrab of using a material paramter to control the state of sprite animation.

Below is our material setup. The specific example is for a material with User Interface as the domain since that was our requirement.

Unreal material graph showing the Flipbook node setup
Unreal material graph showing the Flipbook node setup

The Texture node is the sprite sheet to be animated whereas the scalar parameters Rows and Columns allow the artist to specify the grid of the sprite sheet. Next is the scalar parameter Speed which controls the speed of the animation. The Phase parameter is where the fun happens and it is the one we tweak to control the animation state. A value of 0 for Phase parameter implies the first cell of the sprite sheet and a value of 1 implies the last cell.

Once the material is setup, we can control the Phase parameter in Blueprints or C++ by converting to dynamic material instance and using the Set Scalar Parameter Value node.

Screenshot showing how to control the material parameters from Blueprint
Screenshot showing how to control the material parameters from Blueprint

The number for the field Value in the right-most node above can come from any source, for example, a Timeline node.