0 votes

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
actionjustpressed("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?

in Engine by (12 points)

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.

1 Answer

+2 votes

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() 
by (889 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.