This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

Hey guys, I have a shotgun programmed in that works how I want but I would like the bullets to stop and have the gun be short range. How would I go about removing them after the go a certain distance?

Thanks in advance!

code: https://gyazo.com/6613b125c6e39d28a810c1af0b31285e
gif: https://gyazo.com/1bfc5f895452ae03d02f803f29d8e3c1

in Engine by (23 points)
edited by

1 Answer

+1 vote
Best answer

Save the starting position when the bullet spawns, and if the current position > starting position + preferred distance, queue free.

example (use these scripts on the bullets, which i assume is a rigid body 2d):

extends RigidBody2D

var start = Vector2.ZERO
var direction = Vector2.ZERO
var max = 50 # adjust this in distance

func _ready():
    start = position

func _physics_process():
    position += direction
    var dist = abs(cartesian2polar(position - startpos).x)
    if dist > max:
        self.queue_free()

or alternitavely, you can set a timer and queue free on timeout.

example:

extends RigidBody2D

var start = Vector2.ZERO
var direction = Vector2.ZERO
var maxtime = 2.0 # adjust this in seconds

func _ready():
    var timer = Timer.new()
    timer.wait_time = maxtime
    timer.autostart = true
    add_child(timer)
    timer.connect("timeout", self, "on_timeout")

func on_timeout():
    self.queue_free()

func _physics_process():
    position += direction
by (449 points)
selected by

oh, and in the case that you dont know, queue_free() just deletes the node.

Just an addition on the queue_free() topic:
Use calldeferred('free') instead of queuefree

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 Frequently asked questions and 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 [email protected] with your username.