Rotating the cube with Quaternion.Lerp

Hi. I’m working on the rotation of the rubik’s cube and having some problems. The first rotation always rotates correctly(90 degrees). However, the rotations coming after the first one are not working correctly; rotates 180 degrees when I try to rotate the cube opposite way as the first rotate, or rotates on two axis when I try to rotate the cube in a different axis as the first rotate.

After the mouse click, I determine the rotation of the cube with FindRotationOfCube(). Then I calculate the direction of the rotation depends on the Mouse movement in RotateDirection(). Last, I rotate the cube with Quaternion.Lerp in RotateSlice().

Here’s my code.

    private void Update()
    {
        switch(stateRotate)
        {
            case "WaitForMouseInput":
                WaitForMouseInput();
                break;

            case "FindRotationOfCube":
                FindRotationOfCube();
                break;

            case "RotateDirection":
                RotateDirection();
                break;

            case "RotateSlice":
                RotateSlice();
                break;
        }
    }
  
    void WaitForMouseInput()
    {
        if(mScript.canRotate)
        {
            stateRotate = "FindRotationOfCube";
        }
    }
  
    void FindRotationOfCube()
    {
        fromRotation = cubie5.transform.rotation;
        rotX = cubie5.transform.rotation.x;
        rotY = cubie5.transform.rotation.y;
        rotZ = cubie5.transform.rotation.z;

        stateRotate = "RotateDirection";
    }
    
    void RotateDirection()
    {
        if(mScript.mouseMovedDir == "Left")
        {
            toRotation = Quaternion.Euler(rotX, rotY + 90, rotZ);
        }
        else if(mScript.mouseMovedDir == "Right")
        {
            toRotation = Quaternion.Euler(rotX, rotY-90, rotZ);
        }
        else if (mScript.mouseMovedDir == "Up")
        {
            toRotation = Quaternion.Euler(rotX, rotY, rotZ - 90);
        }
        else if (mScript.mouseMovedDir == "Down")
        {
            toRotation = Quaternion.Euler(rotX, rotY, rotZ + 90);
        }

        stateRotate = "RotateSlice";
    }

    void RotateSlice()
    {
        cubie5.transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 10);
        stateRotate = "WaitForMouseInput";
    }

Have any suggestions? Thanks.

Here’s a solution. A bit ugly, and I am a bit frustrated that I could not figure out the qTo rotation using Quaternion methods rather than RotateAround(). You can replace the RotateTowards() with Lerp() (since that was your question) if you like, but I liked the feel of RotateTowards() better:

public class RotationsBy90 : MonoBehaviour {
	
	public float speed = 55.0f;
	private Quaternion qTo = Quaternion.identity;
	private float treshold = 1.0f;
	
	void Update () {
		if (Quaternion.Angle (transform.rotation, qTo) > treshold) {
			transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * speed);
			
		}
		else {
			transform.rotation = qTo;
			Quaternion qSave;
			if (Input.GetKeyDown (KeyCode.UpArrow)) {
				qSave = transform.rotation;
				transform.RotateAround (transform.position, Vector3.right, 90.0f);
				qTo = transform.rotation;
				transform.rotation = qSave;
			}
			else if (Input.GetKeyDown (KeyCode.DownArrow)) {
				qSave = transform.rotation;
				transform.RotateAround (transform.position, Vector3.left, 90.0f);
				qTo = transform.rotation;
				transform.rotation = qSave;				
			}
			else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
				qSave = transform.rotation;
				transform.RotateAround (transform.position, Vector3.up, 90.0f);
				qTo = transform.rotation;
				transform.rotation = qSave;			
			}
			else if (Input.GetKeyDown (KeyCode.RightArrow)) {
				qSave = transform.rotation;
				transform.RotateAround (transform.position, Vector3.down, 90.0f);
				qTo = transform.rotation;
				transform.rotation = qSave;			
			}
		}
	
	}
}