Bullets aren't getting deleted upon colliding with an object

Godot Version

4.2.2

Question

I have this code that instantiates a bullet when I fire it

func fire():
	var bullet_instance = bullet.instantiate()
	bullet_instance.position = get_global_position()
	bullet_instance.rotation_degrees = rotation_degrees
	bullet_instance.apply_impulse(Vector2(BULLET_SPEED, 0).rotated(bullet_rotation), Vector2())
	get_tree().get_root().call_deferred("add_child", bullet_instance)

and this code in the bullet scene that causes the bullet to delete when it collides with any object.

extends RigidBody2D

func _on_body_entered(body):
	queue_free()
	if body.is_in_group("Players"):
		body.damage_taken()

The bullet is colliding with the objects, but it isn’t getting deleted. I’ve checked that the collision is masking the object it’s trying to collide with (the bullet is on layer 4 and the test object is on layer 3, the bullet is masking layer 3 and the test object is masking layer 4), but the bullet isn’t deleting. What could cause this? I’ve made a previous game that has used this exact code and worked perfectly.

Do you have contact_monitor set to true and > 1 max_contacts_reported?

Hi.
Can you do this to make sure 100% you’re calling _on_body_entered?
If some of those messages are not being printed (mainly the first one), the signal is probably not connected correctly or your collisions are not being detected, probably because the layers are not set up correctly or something similar.

func _on_body_entered(body):
	print ("_on_body_entered: " + str(self));
	queue_free()
	if body.is_in_group("Players"):
		print ("sending signal: body.damage_taken");
		body.damage_taken()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.