0 votes

I have a shooter node in my main scene which instances bullets. I want the bullets to go in a certain direction but their movement is distorted by shooter's movement. Here's the code for bullets:

func _process(delta):
position.x += delta * 300

So what should I do to make bullets independent?

in Engine by (28 points)

1 Answer

+3 votes

I wish I could find the link to the tutorial in the godot documentation that deals with this exact situation, but essentially you want to spawn the bullets not as a child of the player, but as a child of the World. When you spawn a bullet you feed it three things, the position to spawn at, the speed, and the angle it's fired at. This way it has no inherent ties to the players motion.

EDIT: Found it: https://docs.godotengine.org/en/latest/tutorials/misc/instancing_with_signals.html

by (440 points)

But if - for whatever reason - the bullets have to be children of the player, simply add a call to this method in their script:

func _ready():
    set_as_toplevel(true)

Oh cool I didn't know that, thanks!

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.