Godot Version
4.4.1
Question
Hello to all!
Still making my vampire survivor clone and i’m trying to add a projectile that explodes on contact or when the projectile has travelled a certain distance.
I used 2 Area2D for this, the 1st one being the hit detector, the 2nd one being the explosion and is a child of the first, this one is disabled by default and is enabled when it is time to explode.
Here comes the issue, when the projectile reach max range and explodes, it deals damage as expected when an enemy enters the explosion radius; but when the projectile hits an enemy and explodes, it does not deal any damage even when other enemies enter the explosion radius.
I think it’s because i’m using the “area_entered” to damage my enemy but i am confused as to why it must reach max range to deal any damage at all
anyway here is the relevant code of my projectile’s hit detector
const EXPLOSION_DURATION := 0.5
const SPEED := 1000
const RANGE := 500
func _physics_process(delta: float) -> void:
var direction = Vector2.RIGHT.rotated(rotation)
position += direction * SPEED * delta
travelled_distance += SPEED * delta
if travelled_distance > RANGE:
attack()
func attack() -> void:
set_physics_process(false)
hit_detector.disabled = true
hit_box.disabled = false
explosion_timer.wait_time = EXPLOSION_DURATION
explosion_timer.start()
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("enemy"):
attack()
here is the intitial code that handles the hit detection in my enemy:
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("attack"):
var damage = area.get_damage()
stats.health -= damage
if stats.health < 0:
queue_free()
enemy_death.emit(stats, position)
i tried moving the hit detection in my “_process” function and using the overlapping areas like this but nothing changed:
if hurt_box.has_overlapping_areas():
var overlaps = hurt_box.get_overlapping_areas()
for area in overlaps:
if area.is_in_group("attack"):
var damage = area.get_damage()
stats.health -= damage
if stats.health < 0:
queue_free()
enemy_death.emit(stats, position)
(edit) here is a screenshot of my fireball scene
and here is a video of my fireball in action