Home > Software engineering >  Why when rotating object it's not rotating it as I set it?
Why when rotating object it's not rotating it as I set it?

Time:12-16

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

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

CodePudding user response:

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

  • Related