Bullets flip direction

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?
ezgif-1-f7c4d2d4bf

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.

get_tree().root.add_child(bullet_temp)

from Godot Docs | Change scenes manually

I hope that helps.

1 Like

The root level would be the main player node?

I don’t know – it’s not my game. However, if you use get_tree().root it will be the tree’s “root window” (see docs).

I tried to replace the “add_child(bullet_temp)” with “get_tree().root.add_child(bullet_temp)”, but that stops the projectile from spawning

I don’t think they stopped spawning, they just spawning outside the screen, you need to set the initial position to the muzzle:

func _physics_process(delta: float) -> void:
	if Global.can_fire ==  true:
		var bullet_temp = Fire.instantiate()
		bullet_temp.direction = -1
		get_tree().root.add_child(bullet_temp)
		bullet_temp.global_position = global_position # Try add this
		Global.can_fire = false

Now the bullet will always go to the left

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.

1 Like