skarasuko Thanks for show me the code!
It's broken in a different way, like not playing most animations at all.
The problem lies in the condition of your if statements. With that code, I think it would mean that set_animation
would be called every frame while accepting left or right input. This would cause the animation in the specified AnimationState track to be set over and over again, and only the pose at frame 0 would be repeated all the time. And as a result, your skeleton will look like it is not animated at all.
The code in our example project sets the animation as follows:
func _process(_delta):
if Input.is_action_just_pressed("ui_left"):
get_animation_state().set_animation("run", true, 0)
is_action_just_pressed
returns true when the user has started pressing the action event in the current frame or physics tick. So even if the player keeps pressing the "ui_left" button, setting the run animation will not be repeated.
There are several patterns for writing code to avoid setting the same animation repeatedly when the animation is already set. For example, it can be written as follows (This is not complete code, so please consider it a kind of pseudo code):
func _physics_process(delta: float) -> void:
# Set the current state of Player
set_player_state()
# Set an animation that match the current state of Player
set_skeleton_animation()
func set_player_state() -> void:
# If Player is on the floor and velocity is zero, set "current_player_state" to "idle"
if is_on_floor() and is_zero_approx(_velocity.x):
current_player_state = "idle"
# If Player is on the floor but velocity is not zero, set "current_player_state" to "run"
elif is_on_floor() and not is_zero_approx(_velocity.x):
current_player_state = "run"
func set_skeleton_animation() -> void:
# Get the current animation playing on track 0
current_animation = spine_sprite_anim_state.get_current(0).get_animation().get_name()
if current_player_state == current_animation:
pass
elif current_player_state == "run" and current_animation != "run":
spine_sprite_anim_state.set_animation("run", true, 0)
elif current_player_state == "idle" and current_animation != "idle":
spine_sprite_anim_state.set_animation("idle", true, 0)
This is an example of code that uses get_current(track_id).get_animation().get_name()
to check the name of the animation playing on a track, setting the appropriate animation if it does not match the player's state, and nothing if it does.
Review your code to ensure that animations are only set once, when needed.