Spawning node in 3d goes to world origin

Godot Version

4.3

Question

I am working on my first 3d game within godot. I am trying to convert a bullet spawner that I have used before in 2d to 3d. The bullet spawns, however the node goes to (0,0,0) instead of the position I am trying to get it to go.

func ShootBullets() -> void:
	for i in shots:
		var  newBullet := bullet.instantiate()
		# i have tried global_position here aswell
		newBullet.transform.origin = global_transform.origin
		
		if shots == 1:
			# This part works.
			newBullet.global_rotation = Vector3(0,randf_range(0,360),0)
		else:
			# Arc Radius
			var arcRad := deg_to_rad(arc)
			var increment := arcRad / (ammo - 1)
			newBullet.global_rotation = (
				parent.global_rotation +
				increment * i - 
				arcRad / 2
			)
		# I tried moving this above global posion setting but also did not work, same issue.
		get_tree().root.call_deferred("add_child", newBullet)

I am also getting this error if it matters:
E 0:00:05:0976 bullet_spawner.gd:24 @ ShootBullets(): Condition “!is_inside_tree()” is true. Returning: Transform3D()
<C++ Source> scene/3d/node_3d.cpp:345 @ get_global_transform()
bullet_spawner.gd:24 @ ShootBullets()
player_script.gd:46 @ _input()

Would this work? I assume this function is on your Spawner and you want to copy over the Spawner’s position to the bullet, right? Then it should work.

func ShootBullets() -> void:
	for i in shots:
		var newBullet: Node3D = bullet.instantiate()
		get_tree().root.add_child(newBullet)
		newBullet.global_position = global_position

		if shots == 1:
			# This part works.
			newBullet.global_rotation = Vector3(0,randf_range(0,360),0)
		else:
			# Arc Radius
			var arcRad := deg_to_rad(arc)
			var increment := arcRad / (ammo - 1)
			newBullet.global_rotation = (
				parent.global_rotation +
				increment * i - 
				arcRad / 2
			)
1 Like

that works! i think i was just overcomplicating it with the call_defered part. thank you!

1 Like