- Home /
 
 
               Question by 
               Palarus · Nov 25, 2015 at 08:54 PM · 
                rotationquaterniontimeaxis  
              
 
              Rotating around the Y axis then on Z axis makes my object rotate on the X axis
Hello everyone,
I'm trying to rotate a cube over a period of time when I press the arrow keys using a corroutine. Imagine a Rubik's cube of sorts.
I can rotate it on the Y axis without a problem. But if I try rotating it on the Z axis after rotating it on the Y, it actually rotates the cube on the X axis. How can I fix this?
Here is the code:
 private Touch ft = new Touch();
     private float d = 0;
     private bool swipeCheck = false;
     public GameObject cube;
     private Vector3 lerpAngle = new Vector3();
 
     IEnumerator RotateMe(Vector3 byAngles, float inTime)
     {
         var fromAngle = cube.transform.rotation;
         var toAngle = Quaternion.Euler(cube.transform.eulerAngles + byAngles);
         for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
         {
             //Método viejo
             cube.transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
 
             yield return null;
         }
     }
 
     // Use this for initialization
     void Start () {
 
           
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             
             StartCoroutine(RotateMe(Vector3.forward * -90f, 1.5f));
             
 
         }
         else if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             //cube.transform.Rotate(0, 0, 90, Space.World);
             StartCoroutine(RotateMe(Vector3.forward * 90f, 1.5f));
             
         }
         else if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             //cube.transform.Rotate(0, -90, 0, Space.World);
             StartCoroutine(RotateMe(Vector3.up * -90f, 1.5f));
         }
         else if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             //cube.transform.Rotate(0, 90, 0, Space.World);
             StartCoroutine(RotateMe(Vector3.up * 90f, 1.5f));
 
         }
 
               Thank you in advance!
               Comment
              
 
               
              Answer by Palarus · Nov 27, 2015 at 10:13 AM
Solved it! Instead of using quaternions, I used transform.Rotate(); with Space.World to use world axis instead of local ones!
Your answer