# Unity Rotation - Object is rotating on Z, but should be rotating on X

I've been working on a project where I have a cube. When swiped left, the cube will rotate 90 degrees the left, and then when swiped right, the cube will rotate 90 degrees to the right. I have this area working fine.

However, when i go to swipe up and down (To rotate the cube 90 forwards) it becomes slightly unstuck. If the rotation of the object is (0, 0, 0), it works fine. But if I, for example, rotate once to the left and then tried rotate fowards, the cube will correctly rotate left, but then the forward rotation will rotate on the Z axis and just spin the cube (So the face looks like its moving in right in a clockwise direction).

Here is the code:

public Transform rotateAround;
public float rotateSpeed;

bool isRotating;
Touch touchOn;
Touch touchEnd;

float changeX, changeY;

// Update is called once per frame
void Update()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
{
touchOn = Input.GetTouch(0);
}

if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
touchEnd = Input.GetTouch(0);
var difference = touchOn.deltaPosition - touchEnd.deltaPosition;

changeX = (touchOn.position.x - touchEnd.position.x);
changeY = (touchOn.position.y - touchEnd.position.y);

Debug.Log("Changes: x: " + changeX + " y: " + changeY);

if (changeX > 25f)
{
StartCoroutine(Rotate(transform, rotateAround, -Vector3.up, 90, rotateSpeed));
}
else if (changeX < -25f)
{
StartCoroutine(Rotate(transform, rotateAround, Vector3.up, 90, rotateSpeed));
}

if (changeY > 25f)
{
StartCoroutine(Rotate(transform, rotateAround, Vector3.left, 90, rotateSpeed));
}
else if (changeY < -25)
{
StartCoroutine(Rotate(transform, rotateAround, -Vector3.left, 90, rotateSpeed));
}
}
}

public IEnumerator Rotate(Transform thisTransform, Transform otherTransform, Vector3 rotateAxis, float degrees, float totalTime)
{
if (!isRotating)
{
isRotating = true;

Quaternion startRotation = thisTransform.localRotation;
Vector3 startPosition = thisTransform.position;

transform.RotateAround(otherTransform.position, rotateAxis, degrees);

Quaternion endRotation = thisTransform.rotation;
Vector3 endPosition = thisTransform.position;

thisTransform.rotation = startRotation;
thisTransform.position = startPosition;

float rate = degrees / totalTime;

for (float i = 0.0f; i < degrees; i += Time.deltaTime * rate)
{
yield return null;
thisTransform.RotateAround(otherTransform.position, rotateAxis, Time.deltaTime * rate);
}

thisTransform.rotation = endRotation;
thisTransform.position = endPosition;
isRotating = false;
}
}


Both the camera and the cube start with rotations of 0, 0, 0. The script is placed on the camera, with the rotateAround transform object been set as a cube.

Any help would be appreciated!