What i am trying to do is i have 2 parent objects having 5 children objects each!
so i have a bomb gameObject which i want to move from parent 1 to parent 2’s child’s local position…
I am trying to make that bomb object child of parent 2 & then trying to move it to that particular child’s
local position. but it’s going somewhere else.
when i use the world position it works fine but i don’t want that because when i move that parent object
the bomb Doesn’t goes to the new updated position…
Can anyone help me with this plz>>??? its urgent
transform.localPosition only applies to the child, basically an offset of the parent. You can only have 1 parent, any other attached objects to it are children, or children of children. So to define that in world space, you need to say
Vector3 child2Pos = parentPosition + child.localPosition;
Therefor taking the parents position, and then adding the offset(the local position of the child). Unless it is a child of a child:
Vector3 child3Pos = parentPosition + child2pos + child3.localPosition;
If you’re trying to do math that weird? But for all intents and purposes, you just need to call the actual child’s world position:
Vector3 child2Pos = child2.transform.position;
Vector3 child3Pos = child3.transform.position;
And if you’re trying to change parents with
transform.SetParent(otherTransform); it won’t work if they are prefabs. They actually have to be in
Hierarchy in order to re-parent. One of the funniest things in Unity, no parent support on objects you wish to save and set as prefabs.