- Home /
Rotate over time does not work properly
So i have a coroutine where i rotate an object by 90 over 0.5 seconds. The problem that i am encountering is that the result is not that precise. Rotation usually stops at about 85 and never reaches 90. Any idea what makes the code behave like that?
public float rotationSpeed = 0.5f;
IEnumerator LeftRotateCoroutine()
{
var degreesPerFrame = 90 / rotationSpeed * Time.deltaTime;
float time = 0.0f;
while (rotationSpeed >= time)
{
time += Time.deltaTime;
gameObject.transform.Rotate(0, 0, degreesPerFrame );
yield return null;
}
}
Answer by deniskotpletnev · Jul 29, 2020 at 07:52 AM
Try this:
public float rotationSpeed = 0.5f;
IEnumerator LeftRotateCoroutine()
{
var degreesPerFrame = 90 / rotationSpeed * Time.deltaTime;
float rotationVal = 0;
while (rotationVal < 90)
{
rotationVal += degreesPerFrame;
gameObject.transform.Rotate(0, 0, degreesPerFrame );
yield return null;
}
}
it is still losing almost a degree every time i press the button. And even though it might not be something you can see the first time, after you call the function a few time, by the time the object has done a 360, you lose almost 4 degrees which you can actually notice.
Your answer
Follow this Question
Related Questions
Coroutine Rotation Drifting/Imprecise 1 Answer
Camera rotation around player while following. 6 Answers
Rotate an object 180 degrees smoothly in a particular direction 1 Answer
How to slowly rotate a gameObject from 0 to -90f and vice versa every 5 seconds? 1 Answer
How to rotate an object around another 60 degrees with a keypress? 0 Answers