- Home /
Smooth rotating by 90 degrees in x and y axis.
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;
}
Try to clamp your angles within 0 and 360. Unity will use -degrees and >360 and things get messy. The best thing is to avoid euler all together.
Also I'm not sure, but you may need to add a default case to your switch.
Answer by sniper43 · Jan 16, 2015 at 03:13 PM
If you want it to rotate around one axis, use one axis.
Example: to.Rotate( Vector3.up, 90 );
If you want it to go arounf it-s own axis use object.transform.up (or right or forward, but use -up/forward/right for the opposite directions)
to.Rotate( to.transform.up, 90 );
Smoothing can be done with Lerp
to.Rotate(to.transform.up, Math.Lerp(to.transform.forward, Vector3.back, floatamount ) );
Hope this helps.