Godot Version
4.3
Question
I am trying to make a projectile weapon in godot 4 3d but the projectiles my gun spawns are spawning in a completely different spot, I use a marker3D “%muzzle” to tell the script where to spawn the bullet, an error I got is
E 0:00:01:0975 gun.gd:9 @ _input(): Condition “!is_inside_tree()” is true. Returning: Transform3D()
<C++ Source> scene/3d/node_3d.cpp:345 @ get_global_transform()
gun.gd:9 @ _input()
so how I understand this, is that the muzzle isn’t in the game scene? the thing is when I did some troubleshooting I used print(muzzle.global_position) and it worked, so it should be in the scene
here is the gun script, I changed global_position to global_transform and it stopped the errors but nothing changed besides that
extends Node3D
@onready var bullet = preload("res://bullet.tscn")
@onready var muzzle = %muzzle
func _ready() -> void:
await get_tree().process_frame # Ensures scene is fully ready
if not muzzle:
push_error("Muzzle not found! Check the node path.")
else:
print("Muzzle is ready at:", muzzle.global_position)
func _input(event: InputEvent) -> void:
if Input.is_action_just_pressed("left_click"):
if not is_inside_tree():
return
var create_bullet = bullet.instantiate()
create_bullet.global_transform = muzzle.global_transform
create_bullet.transform.basis = muzzle.global_transform.basis
add_child(create_bullet)