Bullet is not destorying itself when it hits a wall

Godot Version:

The bullets are not disappering when hitting a wall. Here’s my code:

extends Area2D

var speed = 750
@onready var body = $CollisionShape2D
func _physics_process(delta):
	position += transform.x * speed * delta

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	$AnimationPlayer2.current_animation = "Bullet"

func _on_body_entered(body):
	if body.is_in_group("Ghosts") or body.is_in_group("Block") or body.is_in_group("Player"):
	pass # Replace with function body.

if this code is for the bullet, then this block of code should be written inside wall’s script code, not this bullet code, and use body.queue_free() to destroy any node (body) that touches it

did you connect the body_entered signal from bullet’s Area2D or the wall’s Area2d?

I don’t know how to do that because the bullet is in another scene and is set to spawn when you click. Im gussing that you can’t connect signals though scenes. Here is a picture and the code for the gun that spawns in the bullet as well

extends RigidBody2D
@onready var playerlocate = get_tree().get_root().get_node("/root/Node2D/player/CollisionShape2D")
@export var radius = 60
var direction: Vector2
var speed: int = 10

var bulletPath = preload('res://Images/PlayerBullet.tscn')

@onready var attackcool = $CooldownPlayer

func _ready():

func _process(delta: float) -> void:
	var mouse_pos = get_global_mouse_position()
	var player_pos = playerlocate.global_transform.origin 
	var distance = player_pos.distance_to(mouse_pos) 
	var mouse_dir = (mouse_pos-player_pos).normalized()
	rotation_degrees += 90
	if distance > radius:
		mouse_pos = player_pos + (mouse_dir * radius)
		global_transform.origin = mouse_pos
	if global_position.x - get_global_mouse_position().x < 0:
		$Sprite2DGun.scale.y = 2.386
		$Sprite2DGun.scale.y = -2.386
# Called when the node enters the scene tree for the first time.

func move_weapon():
	var mouse_position = get_local_mouse_position()
	rotation = mouse_position.angle()
func _physics_process(delta):
	if Input.is_action_just_released("left click"):

func shoot():
	if attackcool.is_stopped():
		var bullet = bulletPath.instantiate()
		$AnimationPlayer.play("Gun Shoot")

ok, so you get the body_entered signal from the bullet Area2d.
how about the wall? does it has collision shape in the first place?

Yes the wall has a collision shape but its in another scene. How do you connect the signal though scenes.

does it spawn on your world node?

I had the bullets on a different layer than the walls . Sorry for the trouble.

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