how to change a value over time in a coroutine ?

hey hey, first of all :i know that this kind of question has been asked many times before. i want to say that i was reading and then trying much to adapt it to my need but i didnt come to a solution.
i have a script in which the y component of transform.eulerangles is changed in an IEnumerator function using there a coroutine.

   IEnumerator y_rotation()
    {
        
        while (true) // run this forever and ever
        {
            if (is_attacked == true) // if in attack mode, do rotation logic
            {
                //yRotation = Mathf.SmoothDamp(0,-90,ref Velocity,transTime);  
                yRotation = -90.0f;
                yield return StartCoroutine(Wait());
                yRotation = 90.0f;
                yield return StartCoroutine(Wait());
            }
            yield return null; // wait a single frame to let Unity breath
        }
    }

which is executed in FixedUpdate :

   void FixedUpdate()
    {
        transform.eulerAngles = new Vector3(0, yRotation, 0);
        rb.AddRelativeForce(Vector3.forward * thrust);
        rb.velocity = transform.forward * rb.velocity.magnitude;
    }

what i want to achiefe is that it takes about 1 second until yRotation = -90
i’ve tried it with mathf.lerp or mathf.smoothdamp nothing works, i even tried it with a for loop (…i++)
but the value is in a while loop ! so my pc went on strike. I’m pretty confused now so i would be thankfully for any help.

Try this, and get rid of those left and wait CoRoutines.

   float turnSpeed = 3.33f;

 public IEnumerator Turn()
   {
    	       while (true) // run this forever and ever  
               {
                       if (is_attacked == true) // if in attack mode, do rotation logic
                       {
    			      yRotation  += turnSpeed;
                       }
                 yield return null;
              }
}

However, it would be far easier to just use Transform.Roate in Update of FixedUpdate to rotate directly.

If i understand you right you want, while “is_attacked” is true to smoothly pingpong rotate the object between -90 and 90 on the y axis? What’s the initial rotation?

One approach would be:

IEnumerator y_rotation()
{
    while (true)
    {
        if (is_attacked) 
        {
            while (yRotation < 90f)
            {
                yRotation += 90f * Time.deltaTime;
                yield return null;
            }
            while (yRotation > -90f)
            {
                yRotation -= 90f * Time.deltaTime;
                yield return null;
            }
        }
        else
        yield return null; 
    }
}

Note: It’s fine when the angle becomes negative, so don’t wrap it back to 360 outside of the coroutine or the coroutine can never reach -90. If you rotate the object somewhere else where you want to limit the rotation you might want to wrap it around at -180 and 180. Also you should rotate the object in Update, not in FixedUpdate. Otherwise you will see stuttering.

Another way would be to just use Mathf.PingPong in Update. You would need a second variable if you plan to allow other rotations outside the range of -90 to 90.

float t = 0;
float yRotation = 0;
void Update()
{
    if (is_attacked) 
    {
        t += 90f * Time.deltaTime;
        yRotation = Mathf.PingPong(t, 180f) - 90f;
    }
    transform.eulerAngles = new Vector3(0, yRotation, 0);
}