Position is not inherited when using "add_child()"

Godot Version

4.6.1

Question

I’m working on an FPS game, one where the bullets are physical objects (RigidBody3D). I have weapons be added to the playercharacter node when the player acquires them, however I have ran into some issues.

The bullets that I fire always spawn at (0,0,0), even with the player moving around and rotating. The bullets do fire at the right angle however. I’m honestly not sure what is going on, since everything should be working fine, so there might be a problem outside of the scripts themselves.

I will say that the weapon did work fine when I originally had the pistol node be a child of the playercharacter in the editor. I removed it and instead used “add_child()” since I wanted to add/remove it when the player picked it up, and only then did the problems start.

The script for adding the pistol is:

	if global.haspistol == true:
		var pistolpath = preload("res://scenes/weapons/pistol.tscn").instantiate()
		add_child(pistolpath)

The pistol itself:

extends Node3D

@onready var gunbarrelraycast = $RayCast3D

func _ready():

	get_parent().connect("use", _use)

func _use():

	var instance = preload("res://scenes/weapons/projectiles/bullet.tscn").instantiate()
	instance.position = $RayCast3D.target_position
	instance.transform.basis = gunbarrelraycast.global_transform.basis
	get_parent().add_child(instance)

func _process(delta):
	pass

And the bullet:

extends RigidBody3D

const BULLETSPEED = 400.0

@onready var mesh = $MeshInstance3D

func _ready():
# move where the player fired it from
	request_ready()
	linear_velocity = Vector3(0,0,0)
	apply_central_force(self.transform.basis * Vector3( 0, 0, - BULLETSPEED))
# don't want missed bullets to lag up the game, so despawn after a while
	await get_tree().create_timer(10).timeout
	queue_free()

func impact():
	# create flashbang effect
	var instance = preload("res://scenes/weapons/vfx/pistolflashbang.tscn").instantiate()
	instance.position = self.global_position
	get_parent().add_child(instance)
	queue_free()

func _physics_process(delta):
# if hit something, despawn
	if self.get_colliding_bodies():
		print(str(get_colliding_bodies()))
		impact()

Set global_position after adding the node as a child.

normalized is on the right track — the order matters here, and there are two separate things getting mixed up in `_use()`.

First, `$RayCast3D.target_position` is the end of the ray in the RayCast3D node’s own local space, not a position in the world, so assigning it to `instance.position` before the bullet has a parent does not put the bullet at the muzzle.

Second, `position` is local to the bullet’s parent. Set the transform after the bullet is in the tree, using the global values:

```gdscript

var instance = bullet.instantiate()

get_tree().current_scene.add_child(instance)

instance.global_transform = gunbarrelraycast.global_transform

```

That gives the bullet both the muzzle position and its facing direction in one step, which also matters because your bullet’s `_ready()` drives it along its own basis.

I would also add the bullet to the current scene, rather than under the player as `get_parent().add_child(instance)` does. As a child of the player it keeps inheriting the player’s movement and rotation after it is fired, which is rarely what you want for a projectile, and it makes the spawn position harder to reason about.

The same pattern shows up in `impact()` too: `instance.position = self.global_position` is set before `add_child()`, so a global value is being stored as a local one. Setting `global_position` after adding the flashbang as a child would fix that one in the same way.

One thing worth checking while you are there: the pistol itself is added with `add_child()` and no position is set for it, so it sits at (0, 0, 0) in the player’s local space. That is fine if the pistol scene was built around its own origin, but if you positioned it in the editor as a child of the player, that offset is lost when you instance it at runtime. If the bullet still appears at the origin after the change above, print `gunbarrelraycast.global_position` just before you assign it — that will tell you whether the ray itself is where you expect it to be, or whether the pistol is the thing sitting at (0, 0, 0).