Godot Version
4.6
Question
Hello, I have a problem changing states after an animation is over. I’m trying to make a fighting game and I’m working on the character animations using an AnimationTree. I’m using an State Machine based on Nodes to change between animations and now i’m working on a dash, but I have no clue on how to change states since there is no input involved with it nor a physics process, the only thing dictating the end of the state would be the end of the animation.
I can’t use a timer since in fighting games they use frame count for the duration of the animations, hitboxes and hurtboxes, ect. I am not sure how to check when the dash animation in the animation tree ends (it’s not a loop) so that the character keeps moving until the animation finishes and it returns to the Idle state
I would appreciate your help a lot
I just use a “method call” animation track that triggers at the end or just before the end. The receiving object sets the state and also resets when the animation starts.
While @Aviator has given you one option, there are a few others.
Using the AnimationStateMachine
In an AnimationStateMachine you can literally tell an animation where to go using a transition at the end of the animation. take this small one as an example:
Start immediately switches to Idle, hence the green arrow. The transition to Jump switches immediately, but requires the switch to be enabled through code. That’s why the arrow is white instead of green. The switch from the Jump animation to the Fall animation happens automatically (green), at the end of the Jump animation (arrow and line). This is the only way to exit the Jump animation.
Using the animation_finished signal
You can look for the animation_finished signal in your code. Assuming you animation name is “Dash”:
@onready var animation_tree: AnimationTree = $AnimationTree
func _ready() -> void:
animation_tree.animation_finished.connect(_on_animation_finished)
func _on_animation_finished(animation_name: StringName) -> void:
match(animation_name):
"Dash":
#Put the thing you want to do when Dash is over here.
Adding a method_call to Your Animation Track
You can edit the animation, and then call a function at a certain time in your code to execute the next thing. This is useful if your use case is like @Aviator’s where you want something to happen before the animation completes.
Combination
You can also use these in any combination. For example you want the animation to switch to the Idle animation on its own, and you want the player to switch back to Idle state at the same time. Each can handle their own thing separately.
3 Likes
thanks a lot! the animation_finished signal worked,
1 Like