Hello and thanks for entering on my post.
Long story short im trying to make a turret that rotates to look at the mouse position, but it's rotation is limited to 90 degrees total (-45° to 45°). The code i have right now works perfectly for a static turret, the issue is that this turret is a child of something that can rotate and spin freely, and it starts "stuttering" when it faces 180°. The current code is fairly simple:
global_rotation = lerp_angle(global_rotation, global_position.direction_to(get_global_mouse_position()).angle(), 0.2)
rotation = clamp(rotation, -0.785398, 0.785398)
Basically the first part controls the global rotation of the turret, making it look at the mouse position smoothly.
The second part controls the turret's local rotation, telling it not to go past -45° or 45°.
The turret works perfectly even when the node it's attached to starts turning, it tries to look at the mouse position without exceeding it's limits, but as soon as the parent turns enough to have rotated the turret 180° it starts rotating wildly.
Im aware this issue is because with angles, 360 and 0 are the same thing, but numerically they are very far apart, that's why variants like "lerp_angle" exist.
Anyways, any help is greatly appreciated!