# How to make the transform rotate backwards but in FixedUpdate instead in coroutine?

First time I tried this method:

public IEnumerator DoRotation(float time)
{
var newRotation = Quaternion.AngleAxis(180, Vector3.up) * transform.rotation;
var progress = 0f;

while (progress < 1)
{
progress += Time.deltaTime / time;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, progress);
yield return new WaitForEndOfFrame();
}
}


Then I used a flag helper inside Update() to start the coroutine only once but when the transform rotates backwards it also start moving and when moving the transform it shakes/stutters.

I had the same problem with another rotation part and when moving it to FixedUpdate() it solved the shaking problem.

I'm not sure why it's shaking like that.

I tried this in the FixedUpdate:

var newRotation = Quaternion.AngleAxis(180, Vector3.up) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotationSpeed);


But it keeps rotating the transform 180 degrees the opposite direction nonstop.

How to make the transform rotate backwards in FixedUpdate instead of in coroutine?

How about using WaitForFixedUpdate instead of WaitForEndOfFrame? You should put the yield before your code to ensure your code is called in FixedUpdate.

Also, I find tracking the elapsed time easier to reason about. And you probably want to lerp from the start rotation to the end to ensure you're linearly rotation from one to the other:

var startRotation = transform.rotation;
var newRotation = Quaternion.AngleAxis(180, Vector3.up) * transform.rotation;

var elapsed = 0f;
while (elapsed < time)
{
yield return new WaitForFixedUpdate();

elapsed += Time.deltaTime;
var progress = elapsed / time;
transform.rotation = Quaternion.Slerp(startRotation, newRotation, progress);
}


The advantage of lerping from start to end is that you can pass the progress value to an easing function to adjust how it interpolates.

Note that this doesn't guarantee that it reaches the end point because we stop looping before elapsed == time. To solve that, you could change the loop condition and clamp progress between 0 and 1.