Hi, after searching this on internet a lot i couldn’t find a clear answer to my need. So, as other people may have the same problem, i am sharing here!
Basically, you can’t get it to work with a single range. Visualize below:
As you can see, we have two “walls” but thanks god they are not in the same place! We just have to switch between them.
Put it in practice:
You have the “from” rotation, but this from rotation is not defined, so let just make sure to keep in a
360º range using a modulo (% 360).
And then, you have the “to” rotation. What you need to do here is to take the difference from this rotations.
The difference will be used to “detect” whether the rotation is too far from another (more than 180º). And if this is true, you just change this range to a
-180, 180 range.
If in your engine the range is between 0-360 you will keep this, and change to -180-180 when needed.
In Armory, the range is from -180 - 180 // -PI to PI to be more accurate.
angle1 = (from - 360) % 360; // angle1 is just the "from" angle normalized to be in a 360 range. We don't want to deal with bigger numbers. difference = (to - angle1) % 360; // also the difference is normalized, because the angle "to" also may be a big value. if (difference > 180) difference -= 360; // this is responsible to change the -180 - 180 range to a 0-360 range if the difference is greater than 180 degrees. interpolatedAngle = angle1 + difference * Time.realDelta; // the interpolated angle ready to apply. The difference is gradually reduced, to control the speed just multiply it by another number.
For radians just create two variables in the top of the script and replace them in the code:
rad = Math.PI; // for 180 rad2 = Math.PI * 2; // for 360
Thanks to earlin that shared this method here: https://gist.github.com/shaunlebron/8832585#gistcomment-3227412 and also to Unity guy that write it