Reparenting a node and changing its position

Godot Version

4.2.1

Question

I want to reparent the the gun to Node3d and set is rotation to and position to zero but it when i run the code below position messes up.

gun.reparent(item_hold_position, true)
gun.position = gun.to_local(Vector3(0, 0, 0))

Camera is a child of player and player is in a scene where the gun exist.

issue

Try just:

gun.reparent(item_hold_position, true)
gun.global_position = Vector3(0, 0, 0)

Doesnt work. It still messes up the position.

What are you trying to do? If you’re trying to set it to be at the global position (0,0,0) that will do it? What doesn’t work for you?

That doesnt work for me. I want to reset its local position to Vector3.ZERO and change its rotation to
Vector3(0, -90, 0 )

It’s local position, then just do:

gun.position = Vector3(0, 0, 0)

But you don’t do anything for the rotation, so you’d need to add that, like so:

gun.rotation_degrees = Vector3(0, -90, 0)

I know but when i change either of them it messes up, rotation origin is somewhere else it rotates weirdly. I want it to rotate like its just instantiated.

It’s rotated around it’s center, if you want to rotate around something else you’ll need to do that yourself

Thats what i am saying it does not rotate around its center.

Are you sure you know where it’s center is? This will do exactly that, chances are you’re misunderstanding something

with this only
gun.reparent(item_hold_position, true)

with
gun.reparent(item_hold_position, true)
gun.global_position = Vector3(0, 0, 0)
(Its under the player)
with
gun.reparent(item_hold_position, true)
gun.rotation_degrees = Vector3(0, -90, 0)
(its behind the player)

You need to do both, and use the position instead if you want it local

I did. Same result its mainly under the player.

Try what works for you, make sure you understand what the transforms are doing

I tried everything i can imagine. When i instantiate it works perfectly but i dont want to do that.

Why? That sounds like the proper solution then

I am trying to do something if i do it that way it does not satisfy my needs and i just wanna know why isnt it working.

What are your needs that prevent that? You might want to change your solution if one way works, or check what is different between your two solutions

I know the difference and i just want to know if i reparent the node just like instantiate function does. But i cannot figure it out.

If you explain the difference instead I might be able to help you, but if you don’t want to explain there’s not much I can do to help