Problems with detecting collisions

Godot 4.3

New Godot programmer here! I finished the Dodge the Creeps tutorial yesterday and have been trying to edit it so that the player periodically fires bullets that can destroy mobs. I have the bullets set up, but no collisions between the mobs and the bullets are being detected. Layering issues were brought up often in other forum posts about collisions, but I’m not sure that’s what’s happening here. I have Player set to layer 1 & mask 2, mobs set to layer 2 & mask 3, and bullets set to layer 3. What am I missing?

Node setup for mobs:

Mob (RigidBody2D)

AnimatedSprite2D
VisibleOnScreenNotifier
CollisionShape2D
Area 2D
CollisionShape2D2

Mob code for detecting collisions:

func _on_area_2d_body_entered(body: Node2D):
if body.is_in_group(“bullets”) == true:
print(“hit”)
queue_free()

(hint has not been printing. when I placed the detection above the if statement, the mob was detecting collisions, but not with bullets)

Bullet node setup:

Bullet (Node2D)

ColorRect
Area2D
CollisionShape2D
VisibleOnScreenNotifier

Bullet code:

@export var speed = 5

func _ready():
add_to_group(“bullets”)

func _process(delta):
position += Vector2(speed,0).rotated(rotation)

func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()

Player node setup:

Player (Area2D)

AnimatedSprite2D
CollisionShape2D

Player code for creating bullets:

func _on_bullet_timer_timeout():
var b = bullet.instantiate()
b.position = position
b.look_at(get_global_mouse_position())
b.top_level = true
add_child(b)

Make sure bullets have a mask of layer 2, or they won’t collide with the mobs. The way you have it, mobs will collide with bullets, but bullets won’t collide with mobs.

1 Like

Thanks for your help! I set the bullets to have a mask on layer 2 (and later layer 3, just for good measure) but this didn’t seem to fix the problem.