I have a problem when I instantiate a scene in to my main scene area2d does not detect it

Godot Version

4.3

Question

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")

image

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.

Thank you for replying i have edited the post

1 Like

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’.

1 Like

I believe you need to use overlaps_body for Area2D rather than the physics callbacks…?

Could you tell me why this way wont work
thanks

yes my layers and collisions are set correclty that is not the issue