I have a very basic script which, when triggered, allows me to parent or unparent an object to/from another object.
This is mainly used to assign the player as a child of an elevator or moving platform to prevent a vibrating effect when the gravity fights the upward force of the elevator.
There is a slight issue with this that I have noticed: When I unparent an object, if it’s parent was rotated (didn’t have zeroed rotational transforms) the player is snapped to whatever that rotation was upon unparenting, which causes the player in my case to rotate instantly to the left by 90 degrees when unparented (when the elevator reaches the top).
Is there any easy fix/am I doing something wrong?
using UnityEngine;
public class ObjectParentChild : MonoBehaviour
{
public void MakeObjectChild(GameObject childObject)
{
childObject.transform.parent = this.transform;
}
public void UnparentChild(GameObject childObject)
{
childObject.transform.parent = null;
}
}
Try using SetParent(null, true) instead - it’s a little more explicit and reliable.
If that doesn’t work, try using these functions on a generic object (e.g. not the player) and see if that works as expected - if it does, your player control script might be modifying the rotation of the player.