- Home /
Controlling the direction of a rotation
I have a dial with a needle, somewhat resembling a gauge of some kind. I need the needle to always turn the same way around, that is clockwise.
When my needle is at min say -90 and I want it to go to max, say +90, the dial goes counter-clockwise since this is shorter.
Given this code, which turns a transform, how would I force the rotation to take the other way around?
[ContextMenu("Step on it")]
void Test()
{
StartCoroutine(StartAcceleration());
}
IEnumerator StartAcceleration()
{
Quaternion fromRotation = this.transform.localRotation;
Quaternion toRotation = Quaternion.Euler(0,0,-90);
float seconds = 3.0f;
float rate = 1.0f / seconds;
float lerpFactor = 0;
while (lerpFactor < 1.0f)
{
this.transform.localRotation = Quaternion.Slerp(fromRotation, toRotation , lerpFactor);
lerpFactor += Time.deltaTime * rate;
yield return null;
}
}
I have tried Lerping with angles, but I have trouble figuring out how to do the lerp, and what to define in the while conditions, since my angle wraps half-way through from 0 to 360.
Answer by Loius · Feb 19, 2013 at 09:40 AM
You could lerp the angle (single float) instaed of the entire rotation. Lerp from your min angle to max angle and just directly set ,rotation = Quaternion.Euler(0,angle,0).
There shouldn't be any issue with wrapping from 0 to 360; those are the same rotation (and so are 720 and -360, etc).
(you can't force slerp to go a different direction; it's wired to take the shortest route, just like lerp is (though, understandably, it's harder to tell with lerp))
Could you try giving me an example in the code? Since I have tried this before, but I am not sure about how it should be done.
What do I use to lerp on as the angle ins$$anonymous$$d of roRotation? Since this is a Quaternion and Quaternion.Slerp needs two Quaternions and not a float angle.
Don't slerp at all, just lerp the single axis of rotation.
Since you're only rotating around Y you just need to lerp from -90 to 90 and substitute that for angle.
transform.rotation = Quaternion.Euler(0, $$anonymous$$athf.Lerp($$anonymous$$,max,lerpFactor), 0);
If I do this:
needleHub.transform.localRotation = Quaternion.Euler(0, 0, $$anonymous$$athf.LerpAngle(90f, -90f, lerpFactor));
It still goes the wrong direction. So how do I change direction? Substituing -90 with 270 did the same.
I wrote out "$$anonymous$$athf.Lerp" because that is the right function to use. Don't use LerpAngle, it lerps between angles which is just like Slerp. Use $$anonymous$$athf.Lerp.
You are right. I should have done that :-) $$anonymous$$athf.Lerp worked.
Please convert your comment to an to answer if you would.
Answer by mikesmullin · Apr 13, 2015 at 01:09 PM
Another way to do this is to Lerp from 89 to -89 degrees instead of 90 to -90. Since Lerp is "wired to take the shortest route", you are indicating which direction (clockwise or counter-clockwise) you intend to rotate by making it the shorter of the two paths.
This way may be less accurate, but you can mitigate that problem by making the difference much smaller like 89.999. I suppose this problem only happens when you are moving a difference of exactly 180.0 degrees, and don't like the default direction it will choose.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092950im_/https://answers.unity.com/themes/thub/images/avi.jpg)