Rotating smoothly in an arc when the angle goes over/under 0

I recall this was answered really well in a different thread, but I now can’t find it and didn’t save it!

Say we have an object that rotates over the course of the game and we want to sometimes have a gear or object that rotates within a 60 degree arc from the front of the initial object. How do we do that smoothly when occasionally that arc will overlap Angle 0 (so saying stay within Object.Angle()-30 and Object.Angle()+30 doesn’t work).

You can have negative angles, and go beyond +/- 360 as well. Have you tried it already? Maybe the problem is somewhere else.

Yes, sorry. I explained that element kinda poorly. My bad!

I want to create an object at the front of another and rotate it toward the mouse, but only -30 or +30 from the original object’s angle. Works fine unless that overlaps the angle of 0, so crossing that threshold makes it greater than, instead of less than.

I’m now thinking it’s possible just using a joint connection with restrictions, will try that next.

I’m not exactly clear on what the issue is, but it sounds like you may want to explore setting the angle to mod(angle + 360, 360) to prevent the angle from overlapping the 0 angle.

Going into negative numbers doesn’t change the direction of greater/less than. This should work for any angle:

clamp(origin - 30, angle, origin + 30)