Godot Version
godot v4.3
Question
Hello,
I tried to follow this video about making hitboxes and hurtboxes but realised that it didn’t really explain how to set up for example enemy damaging the player:
So I tried give my player character a hurtbox and set it to different mask layer so player wouldn’t damage itself, but it still does this, even tough I think they shouldn’t be able to detect eachother.
I’m unsure if I’m missing something or if I have just made a error.
Here’s the related code:
Hitbox:
class_name MyHitBox
extends Area2D
@export var damage := 10
@export var layer = 2
func _init() → void:
collision_layer = layer
collision_mask = 0
Hurtbox:
class_name MyHurtBox
extends Area2D
@export var mask = 2
func _init() → void:
collision_layer = 0
collision_mask = mask
func _ready() → void:
connect(“area_entered”, self._on_area_entered)
func _on_area_entered(hitbox: MyHitBox) → void:
if hitbox == null:
return
if owner.has_method("take_damage"):
owner.take_damage(hitbox.damage)
Also inspector setting because I thought I could change these things based on the entity if I wanted to.