I have created a script for a Enemy and a Space Ship and a bullet I instantiate the bullet with a movement going up and both the enemy and the bullet have collisions. I connect the on_body_entered() signal to the enemy and used groups to check if the bullet is in the enemy but does not seem to detect the bullet.
All are scenes that are put in the main scene called “main”
if Input.is_action_just_pressed("shoot"):
spawn(Vector2(position.x - 14.5 , position.y - 16))
spawn(Vector2(position.x + 13.5, position.y - 16))
func spawn(pos):
var instance = defaultShot.instantiate()
instance.position = pos
get_parent().add_child(instance) # Add to the current scene or a parent node
instance.add_to_group("bullet")
func _on_body_entered(body:Node2D) -> void:
if body.is_in_group("bullet"):
print("Collision has happened")
Hi, @pacinka. Could you include some screenshots of your node types for the enemy and bullet scenes maybe? Edit your original post to include them so they don’t get lost below my comment!
I’ve had problems with this myself. I won’t likely be able to solve your problem, but if you include the scene trees in question, other people might be able to.
I’ve worked around this in the past by just detecting collisions through using move_and_collide(). I don’t find the area2d signals very intuitive.
Have you set your layers and collisions correctly? Your bullet should be in a layer called ‘bullets’, say, and your enemy should be in a layer called ‘enemies’ for instance. If your collision detection code is in your enemy script, to detect collisions with the bullet then your enemies collision layers should be set to detect ‘bullets’.