Hi guys, I am making a 2D game, where I have an archer and multiple enemies overlapping each other. Everything works fine, but when the arrow is fired and the enemies are overlapping the arrow hit all of them before it disappear queue_free(). I tried to add a script to the arrow and queue_free() it immediately when it detects a collusion with the enemy hearthbox and it works, but still the collusion is detected by all enemies in the bunch. I also tried the other way around to detect the arrow from the enemies and remove it from there, which works too, but the result is the same, it takes damage to all of them. It seems that by the time the arrow is removed it was already detected by all the hearthboxes. Any workaround will be greatly appreciated.
Arrow collide with multiple enemies
Welcome to the forums @gargara!
I generally use something like this in the projectile script to avoid having multiple overlapping enemies take damage:
extends Area2D
func _ready():
connect("on_body_entered", self, "projectile_on_body_entered")
var has_damaged = false
func projectile_on_body_entered(other):
# If true, then the projectile has already hit something and we can ignore
# this collision
if (has_damaged == true):
return
# Check if the body collided with is something we can damage.
if (other.has_method("hurt")):
# Damage the body and do anything else needed here (like sound effects)
other.hurt()
# Set has_damaged to true, so we know to ignore damaging
# anything else
has_damaged = true
# free it using queue_free
queue_free
The key is just to somehow track whether the individual projectile has already hit something and if it has, to ignore further collisions from it.
get_parent().remove_child(self) ?
- Edited
Usually you want the projectile to react to colliding with something.
First it should check wether it is still inside the tree with is_inside_tree, then it should check whether the other object has health or is destructible then decreases health or destroys the other object as applicable. Finally it calls destroys itself with queue_free or something else.
Thank you all, what TwistedTwigleg suggested turns out to be working fine. Meanwhile I was able to make a workaround with get_overlapping_areas()[0], but it has some draw backs. This seems to work fine, so thanks once more for the help!