• Godot HelpProgramming
  • New to Godot: How to make bullets destroy self when colliding with a tilemap wall or enemy?

Hi! I am extremely new, just started using Godot yesterday and I followed this tutorial online:

I have finished the tutorial, but when the bullets hit enemies or wall (which was used as a tile map) the bullets do not destroy themselves, they just pile up in the room. This will effect performance I am sure and my intention is to not have them pile up. How would I go about doing this? I can share my code, but i am not sure which script to share.

So i have been reading and found out I can use queue_free. But where do I put it in my player script to delete bullets that collide with a wall (tileset)?

Here is my player code:

extends KinematicBody2D

var movespeed = 500

var bullet_speed = 700
var bullet = preload("res://Bullet.tscn")

func _ready():
	pass # Replace with function body.

func _physics_process(delta):
	var motion = Vector2()
	
	if Input.is_action_pressed("up"):
		motion.y -= 1
	if Input.is_action_pressed("down"):
		motion.y += 1
	if Input.is_action_pressed("right"):
		motion.x += 1
	if Input.is_action_pressed("left"):
		motion.x -= 1
	
	motion = motion.normalized()
	motion = move_and_slide(motion * movespeed)
	look_at(get_global_mouse_position())
		
	if Input.is_action_pressed("LMB"):
		$Main.play("attack")
		fire()
	elif Input.is_action_pressed("RMB"):
		$Main.play("move")
	else:
		$Main.play("idle")

func fire():
	var bullet_instance = bullet.instance()
	bullet_instance.position = get_global_position()
	bullet_instance.rotation_degrees = rotation_degrees
	bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed,0).rotated(rotation))
	get_tree().get_root().call_deferred("add_child",bullet_instance)
	
func kill():
	get_tree().reload_current_scene()

func _on_Area2D_body_entered(body):
	if "Enemy" in body.name:
		kill()

Probably in the fire function. Or have it in a script on the bullet itself.

Yeah, I’d put the script in the bullet itself, so that way the bullet can manage destroying itself and then you won’t have to worry about losing references or anything like that. I’d also recommend adding the queue_free function call to the code that detects the collision for the bullet, since often you want the bullet to be destroyed upon hitting anything, regardless of whether the thing it hit can be damaged or not.

Edit: Also, welcome to the forums :smile:

2 years later