Godot Version
4.6
Question
My hit animation for my heart won’t work it just disappears i don’t know why, is it the order in which i have put the scripts. I’m trying to have it where the animation plays before the heart disappears
extends CharacterBody2D
class_name PlayerController
@onready var animation_player: AnimationPlayer = $Animation_Controls/AnimationPlayer
@onready var currenthealth: int = max_health
@onready var particles_player: CPUParticles2D = $Particles/CPUParticles2D
# Movement Variables
@export var acceleration := 2400.0
@export var speed : float = 300.0
@export var jump_force : float = -250.0
@export var jump_time : float = 0.25
@export var coyote_time : float = 0.075
@export var gravity_multiplier : float = 3.0
@export var max_health = 3
@export var health_parent : HBoxContainer
var reset_position : Vector2
var direction = 0
var is_jumping : bool = false
var jump_timer : float = 0
var coyote_timer : float = 0
var can_control : bool = true
var can_take_damage : bool = true
var hearts_list : Array[TextureRect]
***func _ready():***
*** print(get_path())***
*** reset_position = Vector2(3.0, -63.0)***
*** var hearts_parent = health_parent***
*** for child in hearts_parent.get_children():***
*** hearts_list.append(child)***
*** print(hearts_list)***
***func update_hearts():***
*** for i in range(hearts_list.size()):***
*** hearts_list[i].visible = i < currenthealth***
func _physics_process(delta: float) -> void:
if not can_control: return
# Add the gravity.
if not is_on_floor() and not is_jumping:
velocity += get_gravity() * gravity_multiplier * delta
coyote_timer += delta
else:
coyote_timer = 0
# Handle jump.
if Input.is_action_just_pressed("Jump") and (is_on_floor() or coyote_timer < coyote_time):
particles_player.emitting = true
velocity.y = jump_force
is_jumping = true
elif Input.is_action_pressed("Jump") and is_jumping:
velocity.y = jump_force
if is_jumping and Input.is_action_pressed("Jump") and jump_timer < jump_time:
jump_timer += delta
else:
is_jumping = false
jump_timer = 0
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
direction = Input.get_axis("Left", "Right")
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
move_and_slide()
func handle_danger():
**currenthealth -= 1**
** if currenthealth == 3:**
** hearts_list[0].get_child(2).play("Hit")** -- This is where the animation plays
** update_hearts()**
if currenthealth == 0:
velocity.x = 0
print("You Died")
visible = false
can_control = false
await get_tree().create_timer(1).timeout
reset_player()
return
func reset_player() -> void:
currenthealth = 3
update_hearts()
velocity.x = 0
global_position = reset_position
visible = true
can_control = true