Object not changing local position when assigned to a new parent

Godot Version

Godot 4.2.2

Question

I have a set of objects that were originally children of a blank node, and via code they are supposed to have their parent be set to the camera, then move locally relative to the camera. However when trying to change their position, their position is changed relative to their global position.
image

The way I am changing the parent is from a script attached to the camera using:

add_child(target_card)

target_card is acquired from a raycast using raycast_result[“collider”]

when trying to change its position I am using:

target_card.set_position(Vector3(0,5,0))

I am using a set position because the camera will move after the card is made a child to it. I just want it to stay 5 units away from the camera.

Using reparent you can set if the node should keep it’s global transform, I think you want to try false?

target_card.reparent(self, false)
1 Like

Use target_card.reparent(the_camera). target_card will be relative to its current parent until reparented.

1 Like

Using .reparent() worked for its position but when the camera rotates the card isn’t also rotating with it. The rotation is synced when I call reparent but after that, if the camera rotates the card isn’t. If I start the scene with a block already set as a child it will rotate with the camera so that its rotation never changes relative to the camera. Can I also replicate this after using .reparent().

A node will always inherit its parent’s transform (position, rotation, scale, and skew). It your card isn’t rotating with the camera after being parented to the camera, something in your code is affecting it.

in the script for the camera I have:
target_card.set_position(Vector3(0,0,-20))
running in _process() if a card is selected

no other code that changes the rotation or position of the card is running