0

I am using the following code to rotate my game objects over a given period of time:

    IEnumerator RotateMe(Vector3 byAngles, float inTime)
{
    Quaternion fromAngle = transform.rotation ;
    Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + byAngles) ;
    for(float t = 0f ; t < 1f ; t += Time.deltaTime/inTime)
    {
        transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t) ;
        yield return null ;
    }
}


public void runCoroutine(Vector3 destination)   {
    StartCoroutine(RotateMe(destination, 0.5f));
}

then I call it in the following way:

runCoroutine(new Vector3(0,0,-90));

I realized from testing that my game objects are not rotating to the specified angles, but close them. Not really sure what is causing this.

Hilarious404
  • 434
  • 3
  • 13

1 Answers1

0

I would have say that using Lerp method is better. For exact time related things, you'd have to introduce the lerp's rate.

transform.rotation = Quaternion.Lerp(fromAngle, toAngle, 1/value)

you can control the "value" to control the rate of rotation. I usually use this method to have a more controlled rotation.

NOTE: Make sure the lerping is done in a "per-frame" method call; update/fixed-update/late-update