2D - Keep a "picked up" item at relative position to player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MoehSenshi

So I’m looking for a good/proper way to keep a “grabbed” item at a position that’s relative to the player.

I currently have a solution, but this involves moving the .position of the target object to the global_position of the player’s position. I read that changing the .position inside _physics_process is bad, but I tried putting that position setting line inside _integrate_forces of the target object, but it gave wonky physics when combined with my throw logic.

Is there a better way to keep position a foreign object? I’ve thought of either duplicating that object and making it a child of the player, or change the parent node of the target object (which is currently the child of the “level”) to the player object.

But I couldn’t come up with a solution for either.

func processAction ():
	if Input.is_action_just_pressed("action1"):
		var areas = $Area2D.get_overlapping_areas()
		if (picked_item):
            # this throws the item
			picked_item.grabbed = false
			picked_item.new_impulse = Vector2(800, -300)
			print("picked item false")
			picked_item = null
		elif (areas.size()):
			picked_item = areas[0].top
			picked_item.grabbed = true
			add_child(picked_item.instance())
		pass
	elif Input.is_action_just_pressed("action2"):
		pass
	elif Input.is_action_just_pressed("action3"):
		pass
	elif Input.is_action_just_pressed("action4"):
		pass



func _physics_process(delta):
	processAction()
		
	if (picked_item):
		picked_item.position = $AboveHead.global_position