I want to add a gun to the game but i have an issue of changing the guns position to be at the players hand

Godot Version

Godot 4.4

Question

How can i fix it ?

###Script ( If there is an issue pls help if u know the solution)
extends Area2D

Called when the node enters the scene tree for the first time.

func _ready() → void:
pass # Replace with function body.
func gun():
$“…/Gun”.positon.x = $“…/Player/$GunPoint”.position.x
$“…/Gun”.positon.y = $“…/Player/$GunPoint”.position.y
pass

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
position.y += sin(Time.get_ticks_msec() / 200) * 20 * delta

func _on_body_entered(body: Node2D) → void:
body.has_gun = true
gun()

You could add the gun as a child of the player, since children inherit the transform (including position) of their parents. But if that’s not desired, then you need to set the position of the gun every frame (like the process functions) instead of just once when the player enters the area.

I have tried to make it a child of the player but it kept pushing the gun forward
and the process func but I couldint figure it out

$“…/Gun”.position = $“…/Player/GunPoint”.position

I have tried this too but still no result
the gun kept setting it position to the Node2d ( levels ) position

When you added the gun as a child of the player, did you still keep setting the position of the gun? Children add their own position to the position of their parent (and grandparent, on up the tree) to get their global_position.

If you then add the player position into the gun position, it’s effectively adding that position in twice, which will mean the gun flies away from the player. You want the gun position to be either the origin, or some small offset from the origin that tweaks it to the right position relative to the player object. You then want to not touch the gun position at all, other than whatever you want to do for aiming.