Problem With Spawning Projectiles

Godot Version

4.2.2

Question

Hi. Im still really new to godot game development so i still have a lot of questions. i’ve currently started developing a roguelike, where the player can shoot bullets. for some reason, whenever i create the instance of the bullet, it is way off from the position i told it to spawn at:

I dont really understand why this happens, because im sure i’ve done everything correctly. here are the screenshots just in case:

Player Shooting Code:

Projectile code:

(Speed is set to 5 for testing purposes, that was how i found the bug)

help would be appreciated.

P.S - how would i make it so that the bullet gets shot towards the cursor?

  1. To solve it, first just test the bullet, and if the player shooting script is the player script then use it get_tree().current_scene.add_child.

  2. Do you just want that the bullet moves towards the cursor constantly or just for one time

  1. I just coded it up and it worked just fine! thank you so much
    and if you wouldnt mind, could you explain why that was the issue? im just interested so i dont do it again

  2. just for one time

1 Like
  1. Sure why not, this issue happened because first you added the bullet in player position as a child get it’s parent position, so example the player position is Vector2(10, 10) so after adding the bullet, you applied it’s position to shooting point global position means Vector2(10, 10), I let the shooting point position 0 so 10+10 = 20 means your bullet spawn in Vector2(20, 20) not Vector2(10, 10) and also it is odd to spawn the bullet inside player, so the bullet will moves with the player because as I mentioned a child always follows it’s parent position

  2. After adding the bullet in scene tree, means after add_child, add this code:

bullet.look_at(get_global_mouse_position())

Thanks for the explanation and help. appreciate it

1 Like