In my game, I have an enemy that’s root is a characterbody2d that looks like this mockup here:
please take note that i already have the enemy a in group.
I got two questions: 1)
When I add the enemy to the scene through code, it automatically adds the enemy to a group called: “_root_canvas2615635083264”. 2)
when the character first enters the tree, for some reason the characterbody2d root triggers the signal body_entered(), is there a way to make an area ignore its parent?
Your help is much appreciated, and thank you in advance
You could change the collision layer / collision mask for the characterbody and the area. It’s a good idea to filter colllisions with an if statement eitherway.
by just changing the collision layers, that fixed the second issue.
I’m wondering if my first issue isn’t actually anything now.
I’m not sure if I need it for this project since I’m using groups, but could you tell me what you mean about filtering collisions with an if statement?
The body_entered signal emits what entered it, the body part of this function:
func _on_body_entered(body: Node2D) -> void:
You can use an if statement to only run code if the condition is met, sort of like a filter for what “body” runs what code. There are a number of conditions to use, I like checking the class of body with is.
func _on_body_entered(body: Node2D) -> void:
if body is Player:
body.take_damage(10)