Creating bullets at some pixels before the player

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

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
:bust_in_silhouette: Reply From: timothybrentwood

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

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?

Tomi | 2021-05-13 14:05

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

timothybrentwood | 2021-05-13 14:12

Easiest way actually.

magicalogic | 2021-05-14 07:57