+1 vote

Hi, I am following this youtube tutorial: https://www.youtube.com/watch?v=HycyFNQfqI0

I have everything working, but the bullets, when they collide with the wall which was used as a tileset, the bullets just gather and do not destroy themselves.

How do I go about having the bullets destroy themselves as soon as there is collision with the wall tile?

in Engine by (13 points)

1 Answer

+1 vote

So I did a bit of testing, and I have found that using the signals offered by the RigidBody2D. I connected the "bullet"'s signal to its own script.
However, you will need to set the RigidBody2D's contact_monitor to true, and set contacts_reported to at least 1. This can be done either by script, or using the Inspector tab on the top-right-hand side of the screen.


So how I did the signal and code to clear "stuck" bullets:

  1. I created a script for the "bullet" and attach it to the RigidBody2D.

  2. Using the Node tab at the top-right (it really should be called signals), I connected the body_entered(body: Node) signal directly to the bullet's script. This will generate a function at the bottom of the script called _on_RigidBody2D_body_entered which takes the argument body: Node.
    Don't worry about calling this yourself; Godot calls it whenever the RigidBody2D collides with another Body, such as your player or the TileMap.

  3. I then check to see if the colliding body is a TileMap using the following code:

if body is TileMap:
    queue_free()

What the code does is check if the body is a TileMap. If it is, it runs the built-in queue_free function which will remove the bullet from the game.

by (335 points)

Thanks for the clarification.
I was able to set contacts_reported to 1.

I am confused how you attach a script to body_entered(body: Node)
here is my bullet script:

extends RigidBody2D

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

Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta):

pass

func onArea2Dbodyentered(body):
if "Enemy" in body.name:
queue_free()

func onBulletbodyentered(body):
if body is TileMap:
queue_free()

here is how i am trying to attach the bullet script from entered body
Bullet connection to script(self)

Assuming you attached a script to the RigidBody2D for the bullet, what you do in the editor is select the RigidBody2D in the Node Tree.

Then, on the right-hand side (by default) are two tabs: one labeled "Inspector" -- how you set contacts reported -- and one labeled "Node".

Click the tab labeled "Node", and a list of different signals should show.

Find the body_entered(body: Node) signal (it should be at the top), then double-click it. A dialog box will appear with the current scene's Node Tree, with the RigidBody2D selected -- it should say "RigidBody2D (Connecting From)". If you have a script attached to the RigidBody2D, you should be able to click the "Connect" button at the bottom of the window. This will put the function _on_RigidBody2D_body_entered(body: Node), or _on_Bullet_body_entered(body) if the Rigid Body is named "Bullet", at the bottom of the node's script.

Thanks, that was very helpful

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.