Animation never finish and keep going

Godot Version

4.3

Question

IDK why i have this bug but i cannot solve it.
I got enemy Hitbox, attack comp and health comp.
The problem is that animation never stop and signal anim_finish not working to, i can stop it only trough await. But i know that`s not a correct way.

extends CharacterBody2D
class_name clown

@onready var hitbox_component = $Hitbox_component as Hitbox_component
@onready var attack_component = $Attack_component as Attack_component
@onready var health_component = $Health_Component as HealthComponent 
@onready var animation_player = $AnimationPlayer
@onready var sprite_front = $SpriteUP  # Подключите ваш спрайт сюда

@export var speed = 0
var direction: Vector2 = Vector2.ZERO
var lastDirection = Vector2.ZERO
var enemy_dead = false

func _ready():
	add_to_group("Enemy")
	add_to_group("TrapEffect")
	health_component.connect("dead", Callable(self, "on_dead"))
	health_component.connect("damaged", Callable(self, "on_damaged"))

func _process(_delta: float) -> void:
	if enemy_dead:
		velocity = Vector2.ZERO
		return  # Если враг мертв, не делаем больше ничего

	var player_node = get_parent().get_node_or_null("Player")
	if player_node and enemy_dead == false:
		direction = (player_node.global_position - global_position).normalized()
		velocity = direction * speed
		move_and_slide()
		
		update_direction(direction)

func update_animation():
	if enemy_dead == false:
		if velocity.length() > 0.1:
			animation_player.play("walk")
		elif velocity.length() == 0:
			animation_player.play("idle")
	else:
		print("efefeefefe")

func update_sprite_flip():
	if lastDirection.x < 0:
		sprite_front.flip_h = true
	elif lastDirection.x > 0:
		sprite_front.flip_h = false

func update_direction(direction):
	lastDirection = direction
	update_sprite_flip()

func on_dead(killer):
	if killer.name !=null:
		speed = 0
		hitbox_component.disable_hitbox()
		health_component.call_deferred("queue_free")
		attack_component.call_deferred("queue_free")
		play_death_animation(killer)
		print("Enemy is dead, killed by:", killer.name)
	else:
		print("Enemy is dead")

func on_damaged(damager):
	if damager !=null:
		print("Enemy took damage from:", damager.name)
	else:
		print ("Enemy took damage")

func play_death_animation(killer):
		if killer.name == "Trap" and enemy_dead != true:
			animation_player.play("death_trap")
			enemy_dead = true
			print(enemy_dead, "анка сука")

func _on_animation_player_animation_finished(anim_name: StringName) -> void:
	print("Animation finished: ", anim_name)
	if anim_name == "death_trap":
		# Прекращаем обновления анимации после завершения смерти
		animation_player.stop()
		print("Death animation finished")
extends Area2D
class_name Hitbox_component

@export var health_component: HealthComponent
@export var hit_box_shape: CollisionShape2D

func _ready():
	self.connect("area_entered", Callable(self, "_on_area_entered"))
	self.connect("area_exited", Callable(self, "on_area_exited"))
	call_deferred("_set_hitbox_disabled", false)  # Исправлено

	print("Hitbox shape node:", hit_box_shape)

func _on_area_entered(other_area: Area2D):
	if other_area is Attack_component:
		var attacker: Node = other_area.get_parent()
		health_component.damage(other_area.damage, attacker)

func disable_hitbox():
	print("Before disabling, is hitbox disabled?: ", hit_box_shape.disabled)
	call_deferred("_set_hitbox_disabled", true)  # Вызов отложен

func _set_hitbox_disabled(state: bool):
	hit_box_shape.disabled = state
	print("After disabling, is hitbox disabled?: ", hit_box_shape.disabled)

and that`s a print

After disabling, is hitbox disabled?: false
Enemy took damage from:Trap
Before disabling, is hitbox disabled?: false
trueанка сука
Enemy is dead, killed by:Trap
Dead signal emitted by Clown
After disabling, is hitbox disabled?: true
type or paste code here

enemy_dead is wotking well as i understand by the print (when my clown go to trap)

extends Area2D
class_name Attack_component

@export var damage: float = 10.0
@export var attack_shape: CollisionShape2D

func _ready():
	self.area_entered.connect(on_area_entered)

func on_area_entered(other_area: Area2D):
	if other_area is Hitbox_component:
		var hitbox = other_area as Hitbox_component
		hitbox.health_component.damage(damage, get_parent())

func disable_attack():
	attack_shape.set_deferred("disabled", true)

func enable_attack():
	attack_shape.set_deferred("disabled", false)

I gues that`s due to set.defferred but idk how to solve it cause i cannot make it directly

func _on_animation_player_animation_finished not comming

Are you sure you connected the signal?

Yes. I when i try to connect throughout the ready its make. Error cause that signal already connected

Have you checked if the animations are set to loop-mode?

Yep, when u play it in animation player its playing once. But in game it makes continuously full animation then again but without finish

So the animation “death_trap” is looping in game, but not when you play it in the editor?

Yes, idk why it was, but when i changed the name of animation. Everything solved.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.