How do I delete bullets after they collide with an object

Godot Version

4.2.2

Question

I want to delete my bullets once they collide with another Rigidbody2D, but I can’t figure out how. Here’s my bullet code:

Blockquote
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(rotation), Vector2())
get_tree().get_root().call_deferred(“add_child”, bullet_instance)

In you’re bullet’s _process() or _physics_process(), check to see if your bullet has collided with something, then use queue_free() on itself.

Using a visible onscreen node and connecting the “off screen” signal will allow you to queue_free() once they exit screen as well.

I set up the instances in the player scene, will this still work?

That’s fine. It doesn’t matter where the bullet originates.

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