Arrow Instancing

Godot Version

4.2.1

Question

Hey guys.

I’m making this game which has an archer, and therefore he shoots arrows ( go figure ). My problem is that, i want to instantiate the arrow as soon as the player presses the shoot button, and then give it movement after they release it. To do that, I have a Marker2D on the player, and the arrows is instantiated on top of it, as a child of the player. As soon as the button is released, I set the “Top_level” value of the arrow as true so it stops following the player.

This works, and the arrow shoots perfectly. Except from the fact that it shoot from the position the player was in when they began holding the key. So if you hold the key and move around, the arrow spawns correctly on the player, but when you shoot, it just teleports somewhere else and shoots from there.

This is the code on the player:

func _process(delta):
#this is just so the arrow spins and the like#
if is_holding == true:
current_arrow.set_direction(sign(arrow_spawner.position.x))

if Input.is_action_pressed("shoot") and can_shoot:
	hold_arrow()
	can_shoot = false

if Input.is_action_just_released("shoot") and is_holding:
	shoot_arrow()

func hold_arrow() → void:
add_child(current_arrow)
current_arrow.set_direction(sign(arrow_spawner.position.x))
is_holding = true

func shoot_arrow() → void:
is_holding = false
current_arrow.top_level = true
current_arrow.global_position = arrow_spawner.global_position
current_arrow.fly(multiplier)
can_shoot = true
current_arrow = equip_arrow(0)

And this is the Fly function on the Arrow prefab:

func fly() → void:
linear_velocity = Vector2(direction * Flying_Speed, 0)

I dont think this should be happening, since i even do “current_arrow.global_position = arrow_spawner.global_position” right before shooting, so it should get current position of the marker ( Arrow_Spawner ).

So i dont know what should I do. Let me know if you guys can think of something, or if my explanation was bad or anything xD.

I dont know why some of the code was Code-like and some wasnt :confused:

Sometimes making calls immediately won’t properly update them (I had a similar issue in the past), so a workaround would be to set the global_position of the arrow to be that of the player every frame instead of just when shoot_arrow() is called.