Home > other >  someObject.transform.eulerAngles = new Vector3(0f,0f,0f) Not working
someObject.transform.eulerAngles = new Vector3(0f,0f,0f) Not working

Time:09-26

Here is my code when I try to rotate it

enter image description here

    IEnumerator cubeToBlock () {
        yield return new WaitForSeconds(m_cube.GetComponent <Animation> ().clip.length-0.6f);
        block.Play();
        //! Adding RigitBody component to m_cube
        m_cube.AddComponent <Rigidbody> ();
        m_cube.transform.eulerAngles = new Vector3(0f,0f,0f);
        print(m_cube.transform.eulerAngles.x);
    }

It prints 0, so unity thinks that it is 0 but it isn't

enter image description here

What should I do I don't understand, I am new to Unity so if I missed some details please let me know

P.S. m_cube is surely the Main Cube component

CodePudding user response:

It might be that your Cubes parent object is rotated slightly. Rotation and EulerAngles handles global rotation. If you want local rotation, try transform.localEulerAngles and make sure the parent transform is not rotated. If this isn't the problem, please reply.

Edit: The Inspector shows local position / rotation. That's why you need to use localEulerAngles or make sure the parent isn't turned or moved at all.

CodePudding user response:

First of all I suggest you to setup components in Editor when you can rather than adding them through scripting. Here I am referring to to the following line:

m_cube.AddComponent <Rigidbody> ()

Returning to your problem, if the cube should not be driven by forces you can set isKinematic true. You can use RigidBody.MoveRotation for manipulating rotation. This will also update colliders.

  • Related