Enemy not receiving damage and print not working

Godot Version

Godot $

Hello!

I am very new to the whole game developpement scene, i have a problem with this code right here, i want my enemy character to receive damages when it detects a signal from the “hitArea” of my player character, and with some researches, i managed to build a script that i think shoudl work on paper, but it doesn’t. And i can not find how to solve this. I attached the signal to the hurtBox of my enemy Character. If someone could give me a clue i would be forever grateful. Here is the code of my enemy :

extends CharacterBody3D

@onready var navAgent = $NavigationAgent3D

var SPEED = 3.0
var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)
var enemyHealth = 100

func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity * delta

var currentLocation = global_transform.origin
var nextLocation = navAgent.get_next_path_position()
var newVelocity = (nextLocation - currentLocation).normalized() * SPEED

velocity = newVelocity
move_and_slide()

func updateTargetLocation(target_location):
navAgent.set_target_position(target_location)

######RECEIVE DAMAGE########
func _on_hurt_box_body_shape_entered(playerHitArea):
var Player = get_node(“/root/Player/playerHitArea”)
if Player:
var playerDamage = Player.playerDamage
enemyHealth -= playerDamage
print(“Enemy received damage: %d, Remaining health: %d” % [playerDamage, enemyHealth]) # Print for debugging
else:
print(“Player node not found”)

I assume none of the print statements are working? That would mean that function is not being called, so it’s possible you forgot to connect the “body_shape_entered” signal to the function (which seems unlikely given the function name)

Either that or that signal is not triggered by what you think it is. Have a look at the area_shape_entered signal documentation. By your function’s arguments i guess you meant to use either the “area_entered” or “body_entered” signals instead of “area_shape_entered”

If this doesn’t solve your problem, please provide information about your “hurt_box” node and your “playerHitArea” code as well

By the way, whenever you post code please use preformatted text by surrounding it by ``` lines

```
like this
```

So it looks like this:

func updateTargetLocation(target_location):
    navAgent.set_target_position(target_location)
    [...]
1 Like

Hello JoemanJ!

Thanks a lot for the tip, it was in fact due to the wrong signal, once i changed it it worked! Also thanks for the coding format tip, i will be sure to preformat it for my next topics!

Have a great day

2 Likes