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.

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))

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.

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).