Unparenting Objects Causing Transform Issues

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.

You are indeed correct about it being something on the character, the mouse look script.

I cannot work out what is causing it, I have been changing and commenting stuff but nothing seems to help.

using UnityEngine;

public class NewMouseLook : MonoBehaviour {

    public static NewMouseLook playerCamera;

    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    private float maxSpeed = Mathf.Infinity;
    private float smoothTime = 0.3f;

    private bool isZoomed;

    GameObject character;

    void Start ()
    {
        character = this.transform.parent.gameObject;
    }
   
    void Update ()
    {
        if (!PlayerControl.playerControl.isPaused)
        {
            Vector2 md = new Vector2(cInput.GetAxis("MHorizontal"), cInput.GetAxis("MVertical"));

            md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
            smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
            smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
            mouseLook += smoothV;
            mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);

            transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
            character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
        }
        if(cInput.GetKeyDown("Zoom"))
        {
            if(!isZoomed)
            {
                GetComponent<Camera>().fieldOfView = 20f;
                isZoomed = true;
            }
            else
            {
                GetComponent<Camera>().fieldOfView = 70f;
                isZoomed = false;
            }
        }
    }
}