How do i make it so that a bullets spawn position is a certain node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By austinthepierce

DISCLAIMER: im a total beginner

I figured out how to spawn bullets when I press right mouse

const bulletPath = preload("res://bullet.tscn")

`func _process(delta):’
'if Input.is_action_just_pressed(“primary fire”):


func shoot(): var bullet = bulletPath.instance() get_parent().add_child(bullet)

And i figured out how to make them in the direction of the raycast.
func _process(delta):
translate(Vector3(0,0,1) * SPEED * delta)

However, when I press right trigger, the bullet spawns 2 meters behind the camera. Like, when I look down, it spawns above me. How do I code it so that it spawns exactly where the muzzel is?

How about, when the bullet is spawned, assign the position of the muzzle (i.e. the global position of the muzzle) to the position of the bullet.

Ertain | 2021-09-30 06:21

:bust_in_silhouette: Reply From: yrtv

You added bullet to the muzzle’s parent, not to the muzzle itself. Use add_child(bullet) without get_parrent()

Also use Code Sample button above text input field on this site to preserve GDScript formatting.

1.Paste script code,
2. Select script code with mouse
3. Press Code Sample (looks like curly brackets)
4. Repeat steps 1-3 for all scripts.
5 Check Preview area code now must be nicely formatted


const bulletPath = preload("res://bullet.tscn")

func _process(delta):
	if Input.is_action_just_pressed("primary fire"):
func shoot(): 
	var bullet = bulletPath.instance()