In my game, the player is a zombie trying to infect a certain number of humans, so when they Attack an npc they will become a zombie and then increase the “score” or Zombie Count until they reach the goal and win the game. I have hitboxes for the attacks and hurtboxes for the NPCs, and I have some code set up to perform a function when they connect but I’m not getting the debug message showing that the attack connects. Any advice on troubleshooting this or finding a solution?
Additionally, if there is any advice on organizing the code based on the examples given, I’ll happily take that too! If there are any further questions of what my code is like or if more code examples are needed, please let me know.
(Example of how picking the facing sprite works, has all four directions with animState going from 0 to 3)
if Input.is_action_pressed("ui_down"):
_animated_sprite.play("face_down")
animState = 2
(Example of picking the attack sprite)
if animState == 2:
_animated_sprite.play("attack_down")
print("Attacked downward")
(Example of hitbox collision producing this outcome)
func _on_attack_down_hitbox_area_entered(area):
if area.is_in_group("hurtbox"):
print("Down attack connects!")
area.become_zombie()
#Need to make "hurtbox" for NPCs
#Need to make become_zombie()
print("Down attack connects!")
It’s hard to tell without more info on your scene setup, but here are some common issues:
If the hitbox is actually a PhysicsBody2D (rather than an area), then you need to use the _on_attack_down_hitbox_body_entered(body) handler
Is the hitbox always part of the player scene, or do you instantiate it when the player attacks? In the latter case, make sure you’re actually calling add_child on the hitbox
Is the hurtbox the same Node as the enemy, or does the enemy contain a hurtbox as a child? Does anything happen, for example, if you comment out the if area.is_in_group("hurtbox") line?
Happy to look into this more, but without more info it’s just a guessing game from my end.
Ryan is right, we need more information to help. But I made this list of common collision issues so maybe you can take a look and see if your issue is on this list.
For the player the relationship is AnimatedSprite 2D > Area2D > CollisionShape2D. For npcs, it’s CharacterBody2D > Area2D > CollisionShape2D. Everything is currently under Collision Layer 1 and Collision Mask 1.
After commenting out the if statement on one of the functions like Ryan suggested, I immediately got the message printed “Left attack connects!” as soon as I ran the program. So I guess that means immediately detecting an area entered.
If I need to show anything else, let me know!