Hurtbox
Type: 3DAn Area3D that receives hits from Hitbox areas and reports them with a signal. The other half of a hitbox / hurtbox setup.
Setup
- Save the script as hurtbox.gd.
- Add an Area3D to anything that can be hit, attach the script and give it a CollisionShape3D.
- Put the Hurtbox on its own Collision Layer, for example layer 3 named hurtbox.
- Connect the hit signal to your damage logic.
Code
GDScript
class_name Hurtbox
extends Area3D
## Receives hits from Hitbox areas and reports them with a signal.
signal hit(damage: int)
func take_hit(damage: int) -> void:
hit.emit(damage)Usage example
GDScript
# enemy.gd
# Forward every hit straight to a HealthComponent.
@onready var hurtbox: Hurtbox = $Hurtbox
@onready var health: HealthComponent = $HealthComponent
func _ready() -> void:
hurtbox.hit.connect(health.damage)