Crosshair weird duplication and bullets not following it's path

Godot Version

Godot 4.5.1

Question

I’m making a simple top-down shooter with a Player, a Crosshair, and Enemies.

The Crosshair is a Node2D with a Sprite2D, and it is a child of the Player. There is only one Crosshair in the Player scene.

However, when I run the game, the Crosshair looks like it is duplicated, even though I never instance it twice. I recorded a video, but unfortunately the forum does not allow video uploads. Video here because the forum doesn’t let me put videos :frowning:)

This is the script attached to the Crosshair:

extends Node2D

func _process(_delta: float) → void:
moveCrosshair()

Custom methdods

func moveCrosshair() → void:
# The distance that will be set from his parent
var fixedCrosshairDistance: float = 100.0
position = (get_global_mouse_position() - global_position).normalized() * fixedCrosshairDistance

The Crosshair should stay at a fixed distance from the Player and point toward the mouse.

Additionally, I spawn bullets as children of the Crosshair, but because the Crosshair follows the mouse every frame, the bullets also follow the mouse instead of continuing in a straight line.

Player code that spawns the bullets:

extends CharacterBody2D

Attributes

var health: float

func _ready() → void:
health = 10.0

func _process(delta: float) → void:
pass

func _input(event: InputEvent) → void:
if event.is_action_pressed(“shoot”):
shoot()
print(“Shot”)

Custom Methods

func shoot() → void:
var bulletScene: PackedScene = preload(“res://Scenes/bullet.tscn”)
var bulletInstance = bulletScene.instantiate()
$Crosshair.add_child(bulletInstance)

func takeDamage(damageAmount: float) → void:
health -= damageAmount

if health <= 0:
	die()

func die() → void:
pass

Bullet code:

extends CharacterBody2D

var damage: float
var speed: float

var mousePosition: Vector2

func _ready() → void:
damage = 5.0
speed = 300.0

mousePosition = get_global_mouse_position()

func _physics_process(delta: float) → void:
var direction := (mousePosition - global_position).normalized()

global_position += direction * speed * delta

i want to understand why this happens and how to solve it, i’m new to godot and i’m trying to learn it

Direction vector of the crosshair should go from player’s global position to mouse global position, not from crosshair’s global position.

Nodes always inherit their parent’s transform. So bullets should be parented to a world or level node, not to crosshair.

1 Like