I have an several objects as a children of a parent object and I want to unparent 2 of the children.
transform.parent = null;
The problem is that when I unparent the two objects, they instantly move to the global coordinate equivalent of their local coordinates (let me explain).
Let’s say that the parent object is located at 100, 100, 100. Also, let’s say that both the children objects are at 1, 1, 1 (which is relative to the parent). When the two children are unparented, they move to 1, 1, 1, but now they are global coordinates.
The result is that as soon as the two children are unparented, they instantly move to a unwanted location. The goal is to keep them in the exact same location as before they were unparented. Is this possible?
I realize that I did not explain very well, let me know if/how I can clarify.
Vector3 temp = transform.position; // world pos
transform.parent = null; // *should* not move, but you say...
transform.position = temp; // restore world position
I found the solution. Oddly enough, I simply had to uncheck the “Play Automatically” box in the animation component of the parent object. Now, when the children are unparented, they stay in the same spot as expected.
Have you found the solution? I see the same problem now. I don’t change any player rotation manualy. Just jump on rotated platform (parent player to platform) and when jumping out (unparenting), the global player’s rotation is changed.
I can probably solve it using some rotation offset but I don’t understand why this happens.
When you want to change the pozition of the object while you are in parent use for example transform.localPosition. Let say cube.transform.localPosition = new Vector3(-distance, 0, 0); Now when you unparent with cube.transform.parent=null , the global pozition will not be changed.
In my case it was displaced because of some hidden variables in IK. Visually it was correct, values in inspector were correct. But internally some values were displaced because of a new parenting structure and animations that were built without that parent. My solution was making a seperate object instead of using the original object.