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

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

Question

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.

lightning_2.gd

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:
		queue_free()

hurtbox.gd

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
collision.call_deferred(“set”,“disabled”,true)
disableTimer.start()
1: #HitOnce
pass
2: #DisableHitBox
if area.has_method(“tempdisable”):
area.tempdisable()
var damage = area.damage
emit_signal(“hurt”,damage)
if area.has_method(“enemy_hit”):
area.enemy_hit(1)

func _on_disable_timer_timeout():
collision.call_deferred(“set”,“disabled”,false)

enemy.gd

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
move_and_slide()

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

	$AnimatedSprite2D.play("mushroom_2_walk")
	
	if(player.position.x - position.x) < 0:
		$AnimatedSprite2D.flip_h = true
	else:
		$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
print(hp)
if hp <= 0:
queue_free()

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