- Home /
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).
Didn't find the answer, but it appears to be co$$anonymous$$g from objects that are directly in front of it, ground mesh it works as intended, but if I have a cube in front of me, it just angles 90 degrees down for some reason.
Answer by N1warhead · Feb 17, 2018 at 10:15 AM
Found an answer on this page. https://answers.unity.com/questions/141775/limit-local-rotation.html
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.
Your answer
Follow this Question
Related Questions
Split quaternion into multiple angle-axis rotations for hierarchical ordered appliance 3 Answers
need my object to rotate between specific angles on the x axis 2 Answers
Prevent child rotation but still control it yourself 1 Answer
Camera clamp angle has issue at inspector angle of 0 degrees 0 Answers
Top down camera always facing north - avoid residual rotation 1 Answer