how to implement smooth character movement with animation that does not reset every time a button is pressed

Godot Version

4.3

Question

how to implement smooth character movement with animation that does not reset every time a button is pressed. in general, if you repeatedly press the movement button, say to the right, the character will slowly, but walk, and the animation will move, while if you do this in my game, the animation starts over each time and each time only the first frame is played

Make sure your Animation is set to loop and i would recommend implementing your movement as a state-machine: State · Design Patterns Revisited · Game Programming Patterns

1 Like

I already did this, I have a state machine script, but every time I press the movement button the animation starts again, I don’t explain it well, so you can see how it’s implemented in the titan souls game

Please always share your code using preformatted text ```
Otherwise it’s a guessing game how the logic is implemented in your game.

there is walk_state code extends NodeState

@export var player: Player
@export var animated_sprite_2d: AnimatedSprite2D
@export var speed: int = 60

func _on_physics_process(_delta: float) → void:
var direction: Vector2 = GameInputEvents.movement_input()

if direction != Vector2.ZERO:
	player.player_direction = direction
	if direction.y < 0 and direction.x == 0:
		animated_sprite_2d.play("walk_back")
	elif direction.y > 0 and direction.x == 0:
		animated_sprite_2d.play("walk_front")
	elif direction.x < 0 and direction.y == 0:
		animated_sprite_2d.play("walk_left")
	elif direction.x > 0 and direction.y == 0:
		animated_sprite_2d.play("walk_right")
	elif direction.x > 0 and direction.y < 0:
		animated_sprite_2d.play("walk_back_right")
	elif direction.x > 0 and direction.y > 0:
		animated_sprite_2d.play("walk_front_right")
	elif direction.x < 0 and direction.y < 0:
		animated_sprite_2d.play("walk_back_left")
	elif direction.x < 0 and direction.y > 0:
		animated_sprite_2d.play("walk_front_left")

	player.velocity = direction * speed
	player.move_and_slide()

func _on_next_transitions() → void:
if !GameInputEvents.is_movement_input():
transition.emit(“Idle”)

func _on_enter() → void:
pass

func _on_exit() → void:
animated_sprite_2d.stop()