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.

godotengine.org | Twitter

0 votes

Hello experts!

In my game there is a player and its arm with a gun. This arm is independent from the player, so it can be rotating with the mouse. I would like that the bullets create at the front of the gun.
The trick is that the origin of this armed gun at its right end, not the left where the player look. I would like create the bullets away from this origin to the width of the sprite of the gun.
Here is my code:

if (Input.is_mouse_button_pressed(BUTTON_LEFT) and can_shoot==true):
        var b=bullet.instance()
        get_parent().add_child(b)
        b.position=position #This should be supplemented with offset by the width of the sprite
        b.rotation=(get_global_mouse_position()-self.position).angle()+45
Godot version 3.3 Stable Official Win64
in Engine by (52 points)

1 Answer

+1 vote

Create a Position2D as a child of your gun sprite that is located at the end of the barrel where you want your bullets to spawn. Instead of b.position = position use b.global_position = the_position2d_node.global_position

by (3,906 points)

I would like solving this with something mathematical computing, but I don't know which Godot functions are appropriate for this. Maybe Vector2()? Sin os Cos?

I have degrees in mathematics. This is the way I would solve it hahaha

Easiest way actually.

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.