Every time I flip directions, my bullets flip too
I belive that its because the muzzle where they are being shot (located in the wand) also changes directions, so if that’s the case how can I make the bullet independent of the muzzle?
Bullet code
extends Area2D
var speed = 300
var direction = 1
func _ready():
$Sprite.play("default")
func _physics_process(delta):
position.x += speed * direction * delta
Muzzle code:
extends Marker2D
@onready var Fire = preload("res://Characters/Fire.tscn")
func _physics_process(delta: float) -> void:
if Global.can_fire == true:
var bullet_temp = Fire.instantiate()
bullet_temp.direction = -1
add_child(bullet_temp)
Global.can_fire = false
Your problem is the result of you adding the bullet instance as a child of the Muzzle node. You should add the bullet instance at the root level to avoid hierarchy transformations from affecting the bullet.
Because your bullet code is incorrect, your bullet only move in the x axis, was “working” before because was attached to the muzzle but that also was wrong, you need to change your bullet code:
extends Area2D
var speed = 300
# Direction needs to be a vector
var direction := Vector.ZERO
func _ready():
$Sprite.play("default")
func _physics_process(delta):
# When you're moving a projectil like that, always use the global position
#position.x += speed * direction * delta
global_posiition += speed * direction * delta
Now you need to get the direction your character is facing and set the bullet_temp.direction before add the bullet in the scene, i recommend your move the shooting code for the character script, if you’re using a CharacterBody2D and moving using move_and_slide you can get your current direction using velocity.normalized(). If you need help to make this change you can show the character script and a image of your scene structure.