Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why when rotating object it's not rotating it as I set it?

In default in the editor the object rotation is (the object is a child) :

The rotation is 180,0,0

rotation by default in editor

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Then this code :

public void SetState(string jsonString)
    {
        m_state = JsonUtility.FromJson<State>(jsonString);

        if (m_state.open)
        {
            crateCap.rotation = Quaternion.Euler(90,0,0);

            hasOpened = true;
        }
        else
        {
            crateCap.rotation = Quaternion.Euler(180, 0, 0);

            hasOpened = false;
        }
    }

I want that it will be keep the rotation by default in the else but when it’s getting to the line using a break point :

crateCap.rotation = Quaternion.Euler(180, 0, 0);

I see in the code the rotation is 1.0, 0.0, 0.0, 0.0

rotation in code

In the editor the rotation is :

180, -111.44, 0

Why the Y on the rotation is -111.44 and not 0 as I set it ? I set it to 180, 0, 0

rotation is not 180,0,0 but 180,-111.44,0

>Solution :

Not completely sure about this one, but the problem might come from the fact that your object is a child.
transform.rotation sets the rotation relative to world space.

Use transform.localRotation = Quaternion.Euler(180, 0, 0); instead, as you probably want your object to be rotated relatively to its parent.

Here’s some useful links if you need more informations :
https://docs.unity3d.com/ScriptReference/Transform-localRotation.html

https://docs.unity3d.com/ScriptReference/Transform-rotation.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading