How to make an instance move independently?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Martik

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?

:bust_in_silhouette: Reply From: denxi

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: Instancing with signals — Godot Engine (latest) documentation in English

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)

njamster | 2020-02-05 18:08

Oh cool I didn’t know that, thanks!

denxi | 2020-02-05 23:36