- Home /
Weird behavior between FixedUpdate and WaitForFixedUpdate ??
What I'm going to implement is to rotate a rigid body, the first way is to rotate it in the FixedUpdate method, and the second way is put the rotation into a coroutine.
I thought they would result the same, but I was wrong. Rotation in FixedUpdate works just as I supposed, while rotation in coroutine doesn't.
The two implementations are as follows:
rotate rigidbody in FixedUpdate:
void FixedUpdate() { rbody.MoveRotation(rbody.rotation * Quaternion.Euler(0, 5, 0)); }
rotate rigidbody in coroutine:
void Start() { StartCoroutine("TestRotation"); } IEnumerator TestRotation() { while (true) { rbody.MoveRotation(rbody.rotation * Quaternion.Euler(0, 5, 0)); yield return new WaitForFixedUpdate(); } }
So, what's wrong with the implementation by coroutine? What's the difference between FixedUpdate() and WaitForFixedUpdate()? How could I make it work by coroutine??
Answer by Parkour · Jun 08, 2017 at 05:22 PM
With coroutine is better uses WaitForSeconds:
while (true) {
yield return new WaitForSeconds(0.02f);
rbody.MoveRotation(rbody.rotation * Quaternion.Euler(0, value2, 0));
yield return new WaitForFixedUpdate();
}
Just for testing you have no problems, but the ideal is to avoid coroutines.
I agree with @Parkour ,for your application above (or almost any physics adjustments) there is absolutely no reason for using coroutines, FixedUpdate is there for that specific reason