Trying to get my enemy hurtbox to detect my bullet and apply damage

Godot Engine v4.2.2.stable.official.15073afe3 -
Vulkan API 1.3.277 - Forward+ - Using Vulkan Device #0: AMD - AMD Radeon RX 7900 XT


I will attach the scenes and scripts in question. I am very new to game dev/programming in general. I’ve made steady progress at putting together a simple combat system. I am struggling to figure out how to make my lightning_2 scene (bullet) interact with the hurtbox Area2D child node attached to my enemy.

extends CharacterBody2D

var speed = 2
var direction = Vector2.RIGHT

func _ready():
direction = Vector2.RIGHT.rotated(global_rotation)

func _process(delta):
velocity = direction * speed
var collision = move_and_collide(velocity)

	if collision:

extends Area2D

@export_enum(“Cooldown”,“HitOnce”,“DisableHitBox”) var HurtBoxType = 0

@onready var collision = $CollisionShape2D
@onready var disableTimer = $DisableTimer

signal hurt(damage)

func _on_area_entered(area):
if not area.get(“damage”) == null:
match HurtBoxType:
0: #Cooldown
1: #HitOnce
2: #DisableHitBox
if area.has_method(“tempdisable”):
var damage = area.damage
if area.has_method(“enemy_hit”):

func _on_disable_timer_timeout():

extends CharacterBody2D

var player_chase = false

@export var movement_speed = 20.0
@export var hp = 10
@onready var player = get_tree().get_first_node_in_group(“player”)
@onready var sprite = $AnimatedSprite2D

func _physics_process(_delta):
var direction = global_position.direction_to(player.global_position)
velocity = direction*movement_speed

if player_chase:
	position += (player.position - position)/movement_speed

	if(player.position.x - position.x) < 0:
		$AnimatedSprite2D.flip_h = true
		$AnimatedSprite2D.flip_h = false

if direction.x > 0.1:
	sprite.flip_h = false
elif direction.x < -0.1:
	sprite.flip_h = true

func _on_hurtbox_hurt(damage):
hp -= damage
if hp <= 0:

In my enemy.tscn, as mentioned before, I have it as a characterbody2d with a collision shape and animatedsprite2d. I then have an area2d node named hurtbox with a collisonshape2d and timer set to 0.5s with one shot enabled.

I added an area2d to my lightning_2.tscn, however, I don’t know how to get the hurtbox to pick this area2d up and apply damage to my enemy. Any help would be greatly appreciated!

First you would have to set collision-layer and the collision-mask of your area2D’s. For example:
collision-layer of hurtbox is set to 1
collision-mask of lightning is set to 1
then you can use the signals from area2D like "_on_area_entered(area: Area2D) to detect when something enters

I ended up completely redoing the code for this. I am way farther now than ever before. Thank you!

1 Like