# Cannot rotate 180 degree

I have a game object rotating for specific angles. But this only works for -90 to 90 degree rotation.When I try to rotate it 180 degrees it goes on in an infinite loop. Can anyone please help me?

``````IEnumerator RotateSpecifiedDegrees ( int angle )
{
Quaternion currentRotation = this.transform.rotation;
while( Quaternion.Angle(this.transform.rotation, currentRotation) < Mathf.Abs(angle) )
{
if( angle > 0 )
{
transform.RotateAround( transform.position, Vector3.up, rotateSpeed*Time.deltaTime );
}
else if( angle < 0 )
{
transform.RotateAround(transform.position, Vector3.down, rotateSpeed*Time.deltaTime);
}
yield return null;
}
}
``````

This is how you may want to organize such routine:

# Linear Rotation

``````IEnumerator LinearRotationRoutine ( float degrees , Vector3 axis , float duration )
{
Quaternion initialRotation = transform.rotation;
Quaternion finalRotation = initialRotation * Quaternion.AngleAxis( degrees , axis );

float timeStart = Time.time;
float timeEnd = timeStart + duration;

while( Time.time < timeEnd )
{
yield return null;
float animationTime = Mathf.Clamp01( ( Time.time - timeStart ) / duration );
transform.rotation = Quaternion.Slerp( initialRotation , finalRotation , animationTime );
}
}
``````

Linear rotation for 180 degrees in 1 second around (1,1,1) axis, playing in a loop:

# Elastic Rotation

There is also a second option - to include tweening in the interpolation method:

``````IEnumerator ElasticRotationRoutine ( float degrees , Vector3 axis , float duration )
{
Quaternion initialRotation = transform.rotation;
Quaternion finalRotation = initialRotation * Quaternion.AngleAxis( degrees , axis );

float timeStart = Time.time;
float timeEnd = timeStart + duration;

while( Time.time < timeEnd )
{
yield return null;
float animationTime = Mathf.Clamp01( ( Time.time - timeStart ) / duration );
animationTime = easeOutElastic( animationTime );
transform.rotation = Quaternion.SlerpUnclamped( initialRotation , finalRotation , animationTime );
// note: won't work with Quaternion.Slerp(). It must be Quaternion.SlerpUnclamped() here ^
}
}

float easeOutElastic ( float t )
{
const float c4 = (2f*Mathf.PI)/3f;
return t==0 ? 0 : t==1 ? 1 : Mathf.Pow(2f,-10f*t)*Mathf.Sin((t*10f-0.75f)*c4)+1f;
}
``````

Elastic rotation for 180 degrees in 3 second around (1,1,1) axis, playing in a loop: