Area2D body entered position help

Godot Version

Godot 4.2.2


When the player(body) enters area2D and picks up item, it reparents it to the player’s body position.

func player_entered(body):

“position = body.position”

I want the item to reparent to the player’s “hand” child node.

I’ve tried referencing the hand node path and “position = hand.position”
but it didn’t work.

What do you mean it did not work? Would be good to be specific with description and supply sample code. Use Preformatted Text to make code easier to read.

1 Like

extends Sprite2D

@onready var collision = $Area2D/CollisionShape2D

@onready var hand = get_node(“/root/Testworld/player/pivot/hand”)

@export var stats : Item
@export var skill : Skill

func _ready():
if stats != null:
texture = stats.icon

func _on_player_entered(body):
position = hand.position

It works but the item doesn’t spawn where the hand node is positioned, it spawns on the left top corner of the screen.

You probably want to set position = Vector2.ZERO for it to snap to the hand position.

func _on_player_entered(body):
    reparent.call_deferred(body.find_child("Weapons"), false)
    position = Vector2.ZERO
    body.add_item(stats, skill)
    collision.set_deferred("disabled", true)

use three ticks for pasting code like so

type or paste code here

1 Like

Your code worked! Just had to change Vector2.ZERO to hand.position. Thank you so much!

Then the only thing I really changed was this “false”

reparent.call_deferred(body.find_child("Weapons"), false)

reparent’s second argument is “keep global transform”, by default it’s true and will alter the node’s position so it remains “in-place” which isn’t what you wanted since you are also editing the position manually.

1 Like