I am studing Godot Engine and I searched about sprite animations, and I couldn't do anything. Is there somthing like in Unity? And after I create the animations, how to change the animations state of a sprite?
Godot - Using 2D Sprite Animation
3.7k views Asked by Teodor Cristian AtThere are 4 answers
There's two ways to do animations in Godot (2d), AnimatedSprites or AnimationPlayer. Animation docs: http://docs.godotengine.org/uk/latest/classes/class_animation.html?highlight=animation
USING ANIMATED SPRITES NODE
Basically AnimatedSprites works by creating a SpriteFrames containing the animations, following this way you'll need to use separated sprite frames. In a short answer, you just need to drag'n drop the image frames to the SpriteFrame animation.
How to use it: https://www.youtube.com/watch?v=dew1JdR7TuM
USING ANIMATION PLAYER NODE
This node allows you to animate everything in Godot (not just character frames, but sounds, opacity, modulate colors and well, everything. This way requires keyframes (which are more similar to Unity's way). Also, you can use a entire SpriteSheet here and just set the properties on the inspector to split them. You can give a look about there: https://www.youtube.com/watch?v=Z00BfPs0ImM
ABOUT CHANGE ANIMATIONS
Currently, you can basically call play("animation_name") into AnimatedSprite or AnimationPlayer node, but if you're using this in _process(delta) or _physics_process(delta), you're probably going to get your animation to play only the first frame, because it calls the animation a lot of times and it didn't let it to play until the last frame. One way to avoid this is to have 2 variables: animation and old_animation, for example, and make a check like
if (animation != old_animation):
animation_node.play(animation)
old_animation = animation
of course there's another ways to do it, it's just one solution.
Unfortunately, these videos are in Portuguese, but if you pay some attention you can get it. There's a lot of good videos about it in english too which you can found in youtube.
I hope my explanation have a good didactic, and good luck on Godot studies!
I assume you can create an AnimatedSprite within a Node2D space. From there you will need to add images. These can be added separately from your Godot project folder, by click on Frames and selecting New SpriteFrames. The Kidscancode.org site, as mentioned, has got an excellent tutorial to step you through this.
One thing I would like to add. With one AnimatedSprite, you'll create several animations that you'll call via $AnimatedSprite.play("animation-name"), as referenced above. These animations can also be manipulated in the AnimationPlayer, which can control the objects within the scene. This will become handy later, and while a bit more complicated can access named animations within AnimatedSprite objects and allow you to use the built in Animation State Machine. All the examples I could find online showed the AnimationPlayer manipulating a Sprite, but it works for the AnimatedSprite object as well.
You can either use the built in animation of godot, there is a tons of tutorial out there or make your own and programatically change your sprite's frame index :
I built my own system because the godot ui threw me off.
If you do that you're gonna need this function to get the elapsed time :
And calculate from that how much time passed to check if you're on the next frame or not.