Godot Version
godot version 4
Question
Having an issue with a enemy animation transition.
When the enemy dies while in attack state the enemy finishes the attack animation and then disappears instead of playing the death animation
I’ve tried some things but decided to try playback.travel to force the animation transition but I get the error " cannot call method ‘travel’ on null value"
Enemy code:
#things to do:
#1
#Skeleton dead animation should interrupt attack animation
#currently it dosent and skeleton disappears after attack animation is finished
#2
#if player enters "start area attack shape" in the last frames when enemy isn't
#damaging the player, the skeleton won't attack again and will keep walking forward
extends CharacterBody2D
#-------------------------------------------
const speed = 50
#-------------------------------------------
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var chase = false
var is_attacking = false
var playback : AnimationNodeStateMachinePlayback
#-------------------------------------------
@onready var player = get_node("../../Player/Player")
@onready var skeleton = $"."
@onready var animation_tree: AnimationTree = $AnimationTree
@onready var health = $Damageable_skeleton.Skeleton_health
#-------------------------------------------
func _on_ready():
add_to_group("enemy")
animation_tree.active = true
func _process(_delta):
update_animation_parameters()
if is_attacking == false:
$Attack_detection.monitoring = false
#adds skeleton to enemy group
if $".".is_in_group("enemy") != true:
add_to_group("enemy")
print(animation_tree["parameters/conditions/Dead"])
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
move_and_slide()
var direction = (player.position - self.global_position).normalized()
if chase == true:
velocity.x = direction.x * speed
if direction.x < 0:
get_node("AnimatedSprite2D").flip_h = true
$Attack_detection.scale.x = -1
$Start_attack_area.scale.x = -1
else:
get_node("AnimatedSprite2D").flip_h = false
$Attack_detection.scale.x = 1
$Start_attack_area.scale.x = 1
#-------------------------------------------
func _on_detection_area_body_entered(body):
if body.is_in_group("Player_body"):
chase = true
func _on_detection_area_body_exited(body):
if body.is_in_group("Player_body"):
chase = false
velocity.x = 0
func update_animation_parameters():
if(velocity.x == 0):
animation_tree["parameters/conditions/Idle"] = true
animation_tree["parameters/conditions/is_moving"] = false
elif(velocity.x != 0):
animation_tree["parameters/conditions/Idle"] = false
animation_tree["parameters/conditions/is_moving"] = true
if $Damageable_skeleton.Skeleton_health == 0:
#issue is here
playback.travel("Death")
animation_tree["parameters/conditions/is_attacking"] = false
animation_tree["parameters/conditions/Dead"] = true
if is_attacking == true and $Attack_detection.monitoring == true:
Game.playerHP -= 3
while animation_tree["parameters/conditions/is_attacking"] == true:
velocity.x = 0
#if skeleton isn't dead finish animation then set condition to false
if animation_tree["parameters/conditions/Dead"] != true:
await animation_tree.animation_finished
animation_tree["parameters/conditions/is_attacking"] = false
func _on_start_attack_area_body_entered(body):
if body.is_in_group("Player_body"):
is_attacking = true
animation_tree["parameters/conditions/is_attacking"] = true
while animation_tree["parameters/conditions/Dead"] != true:
await animation_tree.animation_finished
animation_tree["parameters/conditions/is_attacking"] = false
func _on_start_attack_area_body_exited(_body):
is_attacking = false
Taking damage and dying script is under a different node
Animation tree setup: