How to keep player's rotation at parenting

I am developing a 3d-shooter game and I want to make an elevator system. In order for the player to elevate properly without glitches, I have to set the player’s parent to be the elevator itself. However, after the parenting occurs, the player’s rotation changes. I want the player’s rotation to be the same as it was before the parenting. I tried storing and reseting the previous values before parenting, but it didn’t work. What can I do to fix this problem?
Thanks in advance.

If you use transform.parent to set the parent, the world position and rotation won’t change.

This is what I use. But the player’s rotation still changes. Actually, the values in the inspector don’t change, but the player’s rotation changes somehow. And the position values change, but the player’s position seems to be the same.

1 Like

In the Inspector, you’re are seeing the local coordinates. When you parent an object these values have to change (unless the parent is at (0,0,0) and has not been rotated. In any case, the object does not change visually.

This script works as expected (it parents an object to a target without changing the world position and world rotation).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parenting : MonoBehaviour
{
    public Transform parent;
   
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKey(KeyCode.P))
            this.transform.parent = parent;
    }
}

This is my elevator script. It still doesn’t work. The player’s rotation still changes when I press the elevator button.

using UnityEngine;
using System.Collections;

public class TestInteract2 : MonoBehaviour {

    public Animator animator;
    public GameObject player;
    private bool done = false;

    void Interact() {
        if (!done)
        {
            player.transform.parent = this.gameObject.transform.parent.transform;

            animator.SetTrigger("close");
            done = true;
        }
    }
}

It shouldn’t visually change. It’s probably some other script that’s changing the player rotation. Maybe the player controller?

There is also

Check the optional second parameter

I am using the Unity’s Standard Assets and the FirstPersonController.

I tried that, but still nothing happend.

I can not check right now, but it’s possible that FirstPersonController is not supposed to be parented. Deactivate it before parenting just to check if that’s the issue.

Edit:
@GiorgosMih Similar problem as yours here.

I tried disabling the FirstPersonController before parenting and the rotation didn’t change. But if I enable the Controller after that, the rotation will change. As for the reference to the similar problem, I don’t understand what I have to do exactly.

Ok I solved it. I think the problem lies into the FirstPersonController’s MouseLook object. This object has a LookRotation method that in general stores the new calculated rotation to the player’s localRotation. All I did was to store this new rotation to the player’s world rotation as well and it worked.

This is the modified method of the MouseLook class:

public void LookRotation(Transform character, Transform camera)
        {
            float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
            float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;

            m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
            m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);

            if(clampVerticalRotation)
                m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);

            if(smooth)
            {
                character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
                    smoothTime * Time.deltaTime);
                camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
                    smoothTime * Time.deltaTime);
            }
            else
            {
                character.localRotation = m_CharacterTargetRot;
                character.rotation = m_CharacterTargetRot;//this is what I added
                camera.localRotation = m_CameraTargetRot;
            }

            UpdateCursorLock();
        }
6 Likes

Thank you for providing a solution after you found it! However, for me this didn’t work. I’m also using the standard FirstPersonController, but I found that using my fix to changing FPC transforms in an animation referred to here: https://discussions.unity.com/t/612040/11 works perfect in this case.

Just reinitialize your rotations immediately after you reparent (I used my

public void ReInitMouseLook()
{
    m_MouseLook.Init(transform, m_Camera.transform);
}

function linked, but you can call MouseLook.Init() as well in some way, or just make sure you do the following:

m_CharacterTargetRot *= Quaternion.Euler(0f, yRot, 0f);
m_CameraTargetRot *= Quaternion.Euler(-xRot, 0f, 0f);

somewhere directly after your re-parenting (in same frame!)

Matt

YOU ARE THE BEST.

1 Like