Rolling and running - please check if I handle animations correctly or do I overcomplicate?

Godot Version



Hey, so I’m following some tutorials trying to learn Godot and I’m trying now to make something on my own. I want to make the player character run on arrow key presses and play a rolling animation on shift press.

I got it working on my own, but I do wonder if this is the correct way to do it. My initial problem was if I just played animation of rolling on shift press, it would play one frame and revert back to running animation. I first got it somewhat working while holding shift key, but I wanted it to just play the whole animation on the single key press. So here’s my solution:

First I declare a variable:
var is_rolling = false

Then in _physics_process I handle the animations:

if is_on_floor():
if direction == 0:“idle”)
elif !is_rolling and !Input.is_action_just_pressed(“roll”):“run”)
is_rolling = true“roll”)

And finally I use my AnimatedSprite2D signal to control the boolean:

func _on_animated_sprite_2d_animation_changed():
if(animated_sprite and animated_sprite.animation != null):
if animated_sprite.animation == “roll”:
is_rolling = true
is_rolling = false

Seems to work, but I don’t know, feels kind of hacky for a lack of better word. Any tips or tricks for it?

If it works, dont bother changing it unneccessarly. If you want something more structured, you can have a look at state machines. There are a lot of tutorials on state machines on yt

1 Like

State machines are something that sounds like a solution to me, thanks a lot!

1 Like