Bullets aren't spawning near the guns/following their position

Godot Version

<4.2.2 >

Question

Hello everyone,
I’m using an older tutorial to try and make a small 3d shooter sort of like Starfox or or Ex Zodiac.

When it comes to shooting targets, I have Node3D objects attached to my spaceship mesh that stand in as “guns”. They are referenced in the Player class like this:

@onready var guns = [$Gun, $Gun2]
@onready var main = get_tree().current_scene
var Bullet = load("res://bullet.tscn")

and firing them uses a for loop, like this:

if Input.is_action_just_pressed("ui_accept"):
		for i in guns:
			var bullet = Bullet.instantiate()
			main.add_sibling(bullet)
			bullet.transform = i.transform
			bullet.velocity = bullet.transform.basis.z * -600

The bullets fire from the center of the screen, no matter where I move the spaceship. If I change the “gun” position on the player mesh, the guns get stuck there. I even attached box meshes as children to make sure the Node3D “guns” weren’t just hanging in their initial position and not moving; but it seems like bullets are spawning, ignoring the position of the guns, and moving forward in space. Does anyone have hints to help me spawn the bullet in the right place?

Thank you in advance!

bullet.transform = i.transform

Change this to:

bullet.global_position = i.global_position

This will change the global position of the bullet to the global position of the gun. Make sure that the player and gun meshes are on a different collision layer then the bullet so they don’t collide.

1 Like

It worked like a charm, thank you! I’m going to be studying more of the documentation from here on to avoid running into little snags like this.

1 Like