The player in my game can attack in 4 sides. It essentially has 4 animations. I was able to successfully implement the damage mechanics in the right attack, but I am now struggling to do the same for the other attacks.
func _ready():
...
%SwordHitRight.set_deferred("monitoring", false) # extra safety
%SwordHitLeft.set_deferred("monitoring", false) # extra safety
%SwordHitTop.set_deferred("monitoring", false) # extra safety
%SwordHitBottom.set_deferred("monitoring", false) # extra safety
...
if angle_in_degrees >= 315 or angle_in_degrees < 45:
%SwordHitRight.monitoring = true
%Bluwee.play_attack() # Mouse is above the player
elif angle_in_degrees >= 45 and angle_in_degrees < 135:
%SwordHitBottom.monitoring = true
%Bluwee.play_down_attack() # Mouse is to the right of the player
elif angle_in_degrees >= 225 and angle_in_degrees < 315:
%SwordHitTop.monitoring = true
%Bluwee.play_up_attack() # Mouse is below the player
elif angle_in_degrees >= 135 and angle_in_degrees < 225:
%SwordHitBottom.monitoring = true
%Bluwee.play_left_attack()
...
func handle_attack_animation():
...
%SwordHitRight.monitoring = false
%SwordHitLeft.monitoring = false
%SwordHitTop.monitoring = false
%SwordHitBottom.monitoring = false
...
func _on_attack_hit():
var current_anim = %AnimationPlayer.current_animation
print("Attack Hit during: ", current_anim) # Debug which animation fired
for area in %SwordHitRight.get_overlapping_areas(): # Check ALL current overlaps
if area.is_in_group("hurtbox"):
var target = area.get_parent()
if target.has_method("take_damage"):
target.take_damage(GlobalVariables.player_attack_damage / 2.0) # Half damage per swing
print("RIGHT hit ", target.name, " | Frame: ", $AnimationPlayer.current_animation_position)
else:
print("RIGHT: target missing take_damage() method")
for area in %SwordHitLeft.get_overlapping_areas():
if area.is_in_group("hurtbox"):
var target = area.get_parent()
if target.has_method("take_damage"):
target.take_damage(GlobalVariables.player_attack_damage / 2.0)
print("LEFT hit ", target.name, " | Frame: ", $AnimationPlayer.current_animation_position)
else:
print("LEFT: target missing take_damage() method")
Sheep.gd code:
extends CharacterBody2D
var health = 50.0
func _ready():
var sheep_anim = $AnimationPlayer
sheep_anim.play("idle")
$Hurtbox.add_to_group("hurtbox")
func take_damage(amount):
health -= amount
print("Sheep took ", amount, " damage, health remaining: ", health)
%ProgressBar.value = health
if health <= 0:
die()
func die():
print("Sheep has died.")
# Spawn the Dead scene at sheep's position
var dead_scene = load("res://dead.tscn")
var dead_instance = dead_scene.instantiate()
dead_instance.position = position
get_parent().add_child(dead_instance)
queue_free() # Remove the sheep from the scene
I have no idea why it isn’t damaging the sheep. Someone please help me out.