how to shoot the bullet from desired position

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

Hello. I started learning game programming from yesterday. By following couples of youtube tutorials videos, I was able to make topdown player movement and shoot the bullet. However, when I put the bullet spawner as a instance child of the player in order to make it looks like the player is shooting the bulet, this wired thing happened. It seems like the bullet position is affected by the player rotation. How can I fix this?

enter image description here

The code for the Spawner:

extends Node2D

export var bulletScene : PackedScene

func _ready():
	pass # Replace with function body.

func _unhandled_input(event):
	if(event.is_action_pressed("fire")):
		var bullet = bulletScene.instance() as Node2D
		get_parent().add_child(bullet)
		bullet.global_position = self.global_position
		bullet.direction = (get_global_mouse_position() - global_position).normalized()
		bullet.rotation = bullet.direction.angle()

The code for the bullet:

extends KinematicBody2D

const speed = 290

export var smokeScene : PackedScene
export var bulletImpact : PackedScene

var direction = Vector2.ZERO

func _ready():
	pass # Replace with function body.

func _process(delta):
	var collisionResult = move_and_collide(direction * speed * delta)
	if collisionResult != null:
		var smoke = smokeScene.instance() as Particles2D
		get_parent().add_child(smoke)
		smoke.global_position = collisionResult.position
		smoke.rotation = collisionResult.normal.angle()
		
		var impact = bulletImpact.instance() as Node2D
		get_parent().add_child(impact)
		impact.global_position = collisionResult.position
		impact.rotation = collisionResult.normal.angle()
		queue_free()
:bust_in_silhouette: Reply From: vnmk8

instance the bullet outside the player scene use get_node("level").add_child(bullet) or bullet.set_as_toplevel(true) so the bullet doesn’t inherit the parent’s transform