|
|
|
|
Reply From: |
Bean_of_all_Beans |
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:
-
I created a script for the “bullet” and attach it to the RigidBody2D.
-
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.
-
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.
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 _on_Area2D_body_entered(body):
if “Enemy” in body.name:
queue_free()
func _on_Bullet_body_entered(body):
if body is TileMap:
queue_free()
here is how i am trying to attach the bullet script from entered body
dnsmatch | 2020-08-27 22:40
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.
Bean_of_all_Beans | 2020-08-27 23:34
Thanks, that was very helpful