AnimationPlayer animation getting stuck

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By OgGhostJelly

I have a player character, and everytime I press “z” or “space” it should do a sword attack animation. but the sword attack animation stops midway through the slash.

I’m pretty new to Godot so the answer is probably going to be pretty stupid and obvious so please don’t damage my ego.

extends KinematicBody2D


export var speed: = 17000.0

onready var player_sprite: AnimatedSprite = $PlayerSprite
onready var sword_sprite: Sprite = $SwordSprite
onready var animation_player: AnimationPlayer = $AnimationPlayer

var attacking = false


func _physics_process(delta: float) -> void:
	var velocity = move(delta)
	
	animation(velocity)

func move(delta: float) -> Vector2:
	var direction = Vector2(
		Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
		Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
	)
	
	direction = direction.normalized() * delta
		
	return move_and_slide(direction * speed)

func animation(velocity) -> void:
	if velocity:
		player_sprite.play("walk")
		animation_player.play("walk")
	elif not velocity:
		player_sprite.play("idle")
		animation_player.stop()
	
	if velocity.x > 0:
		player_sprite.flip_h = false
		sword_sprite.position.x = abs(sword_sprite.position.x)
		sword_sprite.flip_h = false
	elif velocity.x < 0:
		player_sprite.flip_h = true
		sword_sprite.position.x = -abs(sword_sprite.position.x)
		sword_sprite.flip_h = true
	
	if Input.is_action_just_pressed("attack"):
		animation_player.play("attack")

scene tree:

:bust_in_silhouette: Reply From: Ertain

Maybe check for whether the animation is playing before playing it?

func animation(velocity) -> void:
    if velocity and not player_sprite.is_playing() and not animation_player.is_playing():
        player_sprite.play("walk")
        animation_player.play("walk")
   else:
        player_sprite.play("idle")
        animation_player.stop()

Since these functions are in the _physics_process() function, they’ll be called constantly.

yup, it was pretty stupid and obvious. the animations work perfectly now thank you!

OgGhostJelly | 2022-10-16 02:51