I want to rotate cube in one of x or y axis by 90 degrees. This code works in half. Sometimes cubes is taking bad rotating path and instead of rotating clearly one axis, it rotate in all axis.
cubex.transform.rotation = Quaternion.RotateTowards(cubex.transform.rotation, to, speed * Time.deltaTime);
var touch = Input.GetTouch (0);
var phase: TouchPhase = touch.phase;
switch(phase)
{
case TouchPhase.Began:
apos = touch.position;
break;
case TouchPhase.Ended:
bpos = touch.position;
var ang = Mathf.Atan2(apos.y - bpos.y , apos.x - bpos.x) * Mathf.Rad2Deg + 180;
if(ang <=360 && ang >270){
to = Quaternion.Euler(cubex.transform.eulerAngles.x, cubex.transform.eulerAngles.y - 90 , 0);
}else if(ang <=270 && ang >180){
to = Quaternion.Euler(cubex.transform.eulerAngles.x - 90, cubex.transform.eulerAngles.y , 0);
}else if(ang <=180 && ang >90){
to = Quaternion.Euler(cubex.transform.eulerAngles.x, cubex.transform.eulerAngles.y + 90 , 0);
}else if(ang <=90 && ang >=0){
to = Quaternion.Euler(cubex.transform.eulerAngles.x +90, cubex.transform.eulerAngles.y , 0);
}
break;
}