The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

How to make the bullet independent from movement of gun barrel?
Thanks for help

this script I use for bullet moving...."gule" in the script is bullet

extends RigidBody

export (int) var gule_speed = 850

func _ready():

func _fixed_process(delta):
    var dir = get_global_transform().basis*Vector3(0,0,-1).normalized()
    if Input.is_action_pressed("shoot"):
        self.set_linear_velocity(Vector3(dir * gule_speed ))
        self.apply_impulse(Vector3(),dir * delta * gule_speed ) 

func _on_gule_area_body_enter( body ):
in Engine by (451 points)
edited by

2 Answers

+2 votes
Best answer

Thanks guys,
Yes I have spawner ( Position3D node) parented to the gun barrel
but I find solution....I set bullet toplevel


and this works bullets are independent of the movements( transformations ) gun barrel after shoot input.
maybe it will help others

enter image description here

enter image description here

by (451 points)

To be honest it's not really a good option. Yes, it works for now, but it's another back door added by someone who didn't understand object orientation good enough.

By calling set_as_toplevel() you are removing ALL influences of parent nodes on the bullet. Let's say you want to implement some kind of level switching animation, so when the player enters a door the whole screen should shrink and move away to the side (like the animations they use on TV for sports replays).
Thanks to the object oriented nature of Godot this is not much of a problem - the whole game is a scene and can be resized, moved, rotated etc.
Unfortunately your bullet won't be affected by that since you disabled ALL influence of parent nodes.

That's why it would be a better idea to just add the bullet as a child to the parent of the spawner.
If you need more control or just want an even cleaner solution you can add a signal that is emitted when a shot is fired that passes the new bullet as a parameter, so the scene that owns the spawner can decide which node should be the parent of the new bullet.

The example with the replay cam may not work very well in this case, but being able to spontaneously turn a scene into a subscene of a new larger scene is probably the biggest feature of Godot which enables you to come up with designs that would be a lot more awkward to build in other engines, so by training to build everything in a reuseable way it will become easier to be creative with the engine later.

func shoot():
    var bullet = preload("res://objects/test_bullet_01.tscn").instance()

This is probably a better solution and works but bullet is spawning on the center of a
gunbarrel not from the bulletspawn node.

+1 vote

If you have bullets as children of a Node2D gun, your bullets will get the gun and other parents transformations.

I prefer to have a bullet pool somewhere on the scene and get reference of it by searching groups, is more dynamic and scene-independent.

Another option is to add them to a Node child of the gun or make the gun a Node itself, Node does not have position or any transform information and bullets added there won't be affected by any parent transform (not sure if this is a bug or intended but is useful).

by (7,890 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.