The bullets are not exactly coming out of the mouth of the gun....and this happens when the player turns towards left

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

The image of the bullet coming out..

Bullet.gd

extends Sprite

var dir
const SPEED = 3000
var velocity = Vector2()
var direction

func start_at(rot,pos):
rotation = rot
position = pos
velocity = Vector2(SPEED,0).rotated(rot)

func _physics_process(delta: float) → void:
position += velocity * delta

func _on_VisibilityNotifier2D_screen_exited() → void:
queue_free()

*Shoot ()

if Input.is_action_just_pressed(“Shoot”):
var pbullet = preload(“res://src/Characters/Pistol_bullet.tscn”).instance()

	bullet_container.add_child(pbullet)
	
	
	pbullet.start_at($AnimatedSprite/Pistol.global_rotation,$AnimatedSprite/Pistol/Position2D.global_position)

https://photos.app.goo.gl/Fug12pJXgVEUsJwJ8

The image

i_am_dhrutiman1999 | 2020-07-26 17:42

Can you put all of your code into the codeblock? It is harder to read this way.

ichster | 2020-07-26 18:07

if Input.is_action_just_pressed(“Shoot”):
var pbullet = preload(“res://src/Characters/Pistol_bullet.tscn”).instance()
bullet_container.add_child(pbullet) pbullet.start_at($AnimatedSprite/Pistol.global_rotation,$AnimatedSprite/Pistol/Position2D.global_position)

i_am_dhrutiman1999 | 2020-07-26 18:14

:bust_in_silhouette: Reply From: Johnny2

Because your bullet is set as a child of bullet container, it will inherit transforms of its parent (i.e. bullet_container), which is likely moved by your player.

To fix this, you need your bullet to either be a child of your root get_tree().add_child(pbullet)
seeing as the bullet_container is redundant as you set the position and rotation of it anyway,

or in your ‘Bullet.gd’ script, then add
func _ready(): set_as_top_level(true)
and it will inherit the transforms from bullet_container, and your start_at method becomes redundant. This is the method I would recommend.

I tried but none of the solutions work… problem is the ‘y’ co- ordinate of the Position2D also changes when the player flips which logically shouldn’t happen

i_am_dhrutiman1999 | 2020-07-27 16:24

:bust_in_silhouette: Reply From: ichster

Make sure the parent(s) of the pbullets have a local transform origin of <0, 0>. Since you are setting the starting local position of the bullets to the global position of the pistol’s bullet spawning position ($AnimatedSprite/Pistol/Position2D), if a parent’s local transform is a bit different than <0, 0>, then that will be perpetuated into the pbullet. If you don’t want to do this, you could try adding the local transform of the parent(s) to the pbullet upon calling start_at:

func start_at(rot,pos):
    rotation = rot
    position = pos + get_parent().get_position()
    velocity = Vector2(SPEED,0).rotated(rot)