Rotate object with euler angle

hello,
What i’m trying to do is to rotate my object pvot from -180 to -180, so a full rotation, actually i have this

IEnumerator Rotate(float duration)
{
     float startRotation = transform.eulerAngles.y;
     float endRotation = startRotation + 360.0f;
     float t = 0.0f;
     while ( t  < duration )
     {
         t += Time.deltaTime;
         float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 360.0f;
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation,
         transform.eulerAngles.z);
         yield return null;
     }
}

But after some degrees it will rotate in a wrong direction, i suppose because quaternion rotate with x and y, how to fix it?

Thank’s u in advance.

Euler angles are terrible. If you use them at all, they absolutely must be write only, or you will get side effects like this (the linked article goes into more detail).

Use Quaternion’s other functions, like Quaternion.AngleAxis for this case. Something along these lines:

transform.rotation *= Quaternion.AngleAxis(Time.deltaTime * 360f / duration, Vector3.up);

Thank’s for your help!
It will start but never end , i want it will stop on -180

You could store the starting rotation and then use Q.AA this way.

Quaternion startRotation = transform.rotation;
float t = 0.0f;
     while ( t  < duration )
     {
         t += Time.deltaTime;
transform.rotation = startRotation * Quaternion.AngleAxis(360f * t / duration, Vector3.up);
         yield return null;
     }[/code[

Thank’s you for your help, i tryied your code but it never reach the target, and the t, will stop at 1.9 of(2) the are goin to loop from 1.8 to 1.9…

Your original code would have had the same problem, had it completed a circle - it’s an attribute of the t < duration logic. If it’s important to end on the exact 360-degree rotation after everything else, you need to put a line after the loop that sets that.

Thank’s you for your reply.

I have a working one now,

    private IEnumerator Rotate(
        float in_deltatime,
        int   id,
        float duration,

        EInputState in_secondState)
    {
        Vector3 rot               = _players[id].transform.rotation.eulerAngles;
        float endRotation         = rot.x + 360.0f;
        float t                   = 0.0f;

        while ( t < duration )
        {
            if ( _stopRotation[id] == true )
            {
                yield break;
            }

            t                     += in_deltatime / duration;
            float angle           = Mathf.Lerp(rot.x,endRotation,t);       
            Vector3 fixedRot      = new Vector3 (-angle,rot.y,rot.z );
            _players[id].transform.rotation = Quaternion.Euler( fixedRot );

            yield return null;
        }
       
    }

But sometime after the loop is going to jump from -180 to 140 or something like that, is for the same issue? I have to set the position after the loop?