Godot 4.3
Hello! I’m trying to make it to where when my player gets hit, an animation appears, but it’s not working, could I get some help?
if body.is_in_group("One Hurt"):
value -= 1
print("Hurt 1")
print(value)
if body.is_in_group("Two Hurt"):
value -= 2
print("Hurt 2")
print(value)
if body.is_in_group("Three Hurt"):
value -= 3
print("Hurt 3")
print(value)
$"../Animations".play("ouch")
Please post your full code, and please also describe what you expect to happen and what happens instead. “It’s not working” isn’t a useful description as it could mean anything from “the game crashes” to “the wrong animation plays.”
The animation just won’t play, that’s about it, and I can’t figure out why
extends ProgressBar
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
max_value = 15
min_value = 0
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
if value == 0:
print("Dead")
get_tree().reload_current_scene()
func _on_hitbox_area_area_entered(area: Area2D) -> void:
if area.is_in_group("One Hurt"):
value -= 1
print("Hurt 1")
print(value)
if area.is_in_group("Two Hurt"):
value -= 2
print("Hurt 2")
print(value)
if area.is_in_group("Three Hurt"):
value -= 3
print("Hurt 3")
print(value)
$"../Animations".play("ouch")
func _on_hitbox_area_body_entered(body: Node2D) -> void:
if body.is_in_group("One Hurt"):
value -= 1
print("Hurt 1")
print(value)
if body.is_in_group("Two Hurt"):
value -= 2
print("Hurt 2")
print(value)
if body.is_in_group("Three Hurt"):
value -= 3
print("Hurt 3")
print(value)
$"../Animations".play("ouch")
May I suggest to use @onready variables or @export variables to reference nodes instead of individually referencing them
Use @onready if the node is a child node and @export if the node is node a child node. Just remember to set the node in the editor.
For the animation not working.
- Check that the groups are assigned correctly.
- Check that the animation tracks are named correctly.
- Check that the reference to the animation player is correct.
- Check that the signals are connected