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 (450 points)
edited by

3 Answers

+1 vote
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 great.....now 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 (450 points)

Yes, that's a good option too.

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.

0 votes

The most likely source of this problem is that your bullets are being parented to the gun object. They will then by default inherit all the transformations of their parent.

Bullets should either be created and added directly to the root, or some category node that shares the same origin as your root node.

If you want to keep them parented, you will have to do some extra work to keep track of a global position for them, increment that value, and update their global origin according to that.

by (5,206 points)

Yes...I got it! ......get_node("/root").add_child(bullet)

+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,868 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 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 webmaster@godotengine.org with your username.