0 votes

I am making a 2D sidescrolling shooter and just got the code to shoot a bullet working. I created a second scene named "Bullet" with an Area2D, a sprite and a collision box as a child of area2d. I have a player script in my main scene
Code for the Area2D node:

extends Area2D


 func _ready():
    pass

 func _physics_process(delta):
    position.x += 10

Code for the player (only shooting part)

onready var BULLET = preload("res://Bullet.tscn")
func _process(delta):
    if Input.is_action_just_pressed("shoot"):
        var bullet = BULLET.instance()
        get_node("/root/Game").add_child(bullet)
        bullet.global_position = $Position2D.global_position

I would like the bullet to get deleted when colliding with any objects in my game (This mainly includes a tileset with collision detecion). I read you could link body_entered with a body of your choice, but the tileset is in the main scene and its nodes are not listed under the node tab.

How do I go about detecting collision for this Area2D in another scene?

in Engine by (18 points)

1 Answer

+1 vote

Few options to approaching this

Providing You want to queue bullet free on ANY collision, easiest option is to script bullet himself. Connect his own onbodyentered to himself, and queue him free when it is triggered. You dont need references for anything for this. And even if You wanted to know what are You colliding with it is still possible, because onbodyentered is passed with (body) argument, which You can check if

is TileMap #this is how You check class names

Queuing free is almost always function that should be called by node that is supposed to be queued free, not by other instances

by (5,136 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.