There are two ways to detect a collision, depending of the type of node you are using:
func _physics_process(delta):
move_and_slide()
var collision_count = get_slide_collision_count()
for i in collision_count:
var collision_info = get_slide_collision(i)
var collider = collision_info.get_collider()
if collider.has_method("take_damage"):
collider.take_damage(impact_damage)
died.emit()
queue_free()
func _physics_process(delta):
global_position += Vector2(speed, 0) * delta
func _on_area_2d_body_entered(body):
if body.is_in_group("enemy"):
if body.has_method("take_damage"):
body.take_damage(damage)
queue_free()
Object A will collide to Object B is Object A's Mask value matches Object B's Layer value.
Object A belongs to layer 2, and will collide with any object belonging to layers 1, 2 or 3
Object B belongs to layer 1, and will collide with any object belonging to layer 1