Clamping rotation undesired effects.

// Aim at the target (up and down).
Vector3 dir = hit.point - barrel.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = lookRotation.eulerAngles;
rotation.x = Mathf.Clamp(rotation.x,-15,90);
barrel.rotation = Quaternion.Euler(rotation);

As you can see that code is quite simple, the goal is to clamp the x axis (so the barrel can look up and down at a specific range).

Well it works, however, for some reason when the x Axis reaches roughly 0.0157f it clamps it to positive 90 instantly (which is looking down) and this happens when I try to reach -15 (which is up).

Down work fine, but I can’t for some reason get past the 0 decimal range when getting into the negatives (looking up rotation).

Found an answer on this page.

No idea why a regular clamp doesn’t work, that’s pretty stupid why it would even allow you to put negative numbers and yet not at least warn you that it’s not possible because it would flip it to positive 360 instead.