I have nodes set up that shoot bullets so I can attach them to my enemy, and have them shoot from wherever the node is placed. problem is, if I try to make the bullet equal to the position of the node then it starts firing from a position relative to the map instead. if I do get_parent().position it fires from the correct place, but it's from the center of the enemy. here's my code and a screenshot example of what's happening:

func create_new_bullet():
	var new_bullet = blood_shot.instantiate()
	new_bullet.position = position // if I change this to get_parent().position it's the right place but the wrong relative place. 
	new_bullet.rotation = 0
	new_bullet.get_node("Sprite2D").flip_h = randi_range(0,1) // this is just for visual variety
	new_bullet.scale = Vector2(0.5,0.5)
	main_node.add_child(new_bullet) // this parents it to the world node after setting everything up so it doesn't rotate with the enemy's body


the bullet is supposed to come from the nodes I placed on the enemy's eyes that shoot bullets, but instead it's from the world origin. did I do something weird without realizing it?

    • So when you use position, you are using the relative position to the direct parent's node origin
    • Let's say the position of the "shooting thing" relative to the parent's origin is (0, 0)
    • You are setting the bullet position to be just that (0, 0)
    • When you are adding the bullet to the main node, its position is gonna be (0 ,0) relative to its origin.
    • Using global_position instead of position should solve the problem
    • So instead of new_bullet.position = position
    • You can replace it with new_bullet.global_position = global_position

    I hope this helps

  • So when you use position, you are using the relative position to the direct parent's node origin
  • Let's say the position of the "shooting thing" relative to the parent's origin is (0, 0)
  • You are setting the bullet position to be just that (0, 0)
  • When you are adding the bullet to the main node, its position is gonna be (0 ,0) relative to its origin.
  • Using global_position instead of position should solve the problem
  • So instead of new_bullet.position = position
  • You can replace it with new_bullet.global_position = global_position

I hope this helps

    BroTa that did fix it, but let me make sure I understand why it did before I mark that best answer.
    basically, when the node adds the new bullet, it takes in the position of the parent node. so because the node in it's own scene was 0,0 that meant the bullet was added at 0,0
    BUT if I use global position instead of just position, that means it's added at the actual position of where the node is, instead of relative to the node itself?

    More or less
    According to a note inside this page of the docs:

    A node's local coordinates, like position, are relative to their parent. Global coordinates, like global_position are relative to the world's main axes you can see in the viewport instead.

    • Although the previous article is in 3D, the same applies in 2D.

    • So Let's say you have a character scene, whose weapon's position is (-10, 0) relative to the character.

    • Now let's say the character exists on (50, 20) in the world.

    • This weapon's local coordinates, which you can access through the position property, is relative to the character. returns (-10, 0)

    • This same weapon's global coordinates would be character's position in the world + the weapon's position relative to the character.

    • So the weapon's global coordinates, which you can access through the global_position property, in this example would be (40, 20)

    • Now we want to disarm our character, by removing the weapon from character and add weapon item to the world.

    • If we use the position property (-10, 0) to set its position in the world, the weapon will appear in just that coordinates, (-10, 0) as if it teleported.

    • However, if we use the global position of this weapon (40, 20) to set its position in the world it will appear just there, and that's what we want.

    I hope this clarified everything