Why aren't my bullets going straight when i move?

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

So i’m trying to make a top-down 2d shooter in Godot 3.5, and for the most part, it works. The bullets fire fine when i’m not moving. However there’s this thing where when i move too fast, the bullets start to shoot out at the wrong direction.
Here’s what the code for the fire() function looks like:

It might be because your player has moved by the time add_child() is called. Have you tried calling add_child immediately instead of deferred?

SteveSmith | 2022-10-01 07:29

So just remove _deferred?

pokemonleafgreen | 2022-10-01 18:46

Not quite: “get_tree().get_root().add_child(bullet_instance)”.

If that doesn’t work, try setting the bullets position to the correct position in its _onready().

SteveSmith | 2022-10-01 21:21

did you set it as toplevel so it doesn’t follow any parents?

strangerturtle | 2022-10-06 00:12

Figured it out, and i’ll leave it here for anyone else who might have the same problem; i was instancing the bullets from inside the player body, and it was colliding with itself, causing the bullets to ricochet off the character. I solved it by just putting an Area2D in front of the character.

pokemonleafgreen | 2022-11-14 02:42