- Home /
using Lerp and Quternion for turning does not work exactly in degrees!!!!?
public void TurnRight (){ StartCoroutine (RotateMe (Vector3.forward * -36, 0.5f));
}
public void TurnLeft () {
StartCoroutine (RotateMe (Vector3.forward * 36, 0.5f));
}
IEnumerator RotateMe (Vector3 byAngles, float inTime) {
var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler ((transform.eulerAngles + byAngles));
for (var t = 0f; t < 1; t += Time.deltaTime / inTime) {
transform.rotation = Quaternion.Lerp (fromAngle, toAngle, t);
yield return null;
}
here's a picture of the problem! https://imgur.com/a/0JBqm
Answer by yummy81 · Feb 13, 2018 at 10:35 PM
Give it a try:
public void TurnRight ()
{
StartCoroutine (RotateMe (-36f, 0.5f));
}
public void TurnLeft ()
{
StartCoroutine (RotateMe (36f, 0.5f));
}
private IEnumerator RotateMe(float byAngles, float inTime)
{
Quaternion target = transform.rotation * Quaternion.AngleAxis(byAngles, Vector3.forward);
float t = 0f;
while(t < 1f)
{
t += Time.deltaTime / inTime;
transform.rotation = Quaternion.Lerp(transform.rotation, target, t);
yield return null;
}
}
Answer by JaCoBReaper · Feb 14, 2018 at 05:15 PM
tnx for your great solution, but you did mentioned some good point on why shouldn't we use that method and after a refresh your comments were gone! i really appreciate your comments on why its not right to use this wrong method!
That comment was not made by me, but by Bunny83. I don't know why it's gone now. He pointed out the mistake in my code, which I corrected and now the piece of code you see above is the final version. When it comes to your code, it misses one iteration for it to work as expected. Take this line of code:
for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
{
and modify it that way:
for (var t = 0f; t < 1;)
{
t += Time.deltaTime / inTime;
tnx for your comment! i did what you suggested , and i really appreciate it!
Your answer
![](https://koobas.hobune.stream/wayback/20220612152557im_/https://answers.unity.com/themes/thub/images/avi.jpg)