I am basing having a bearing be able to be rotated in game view and when it is between certain rotation angles it displays text, Example if between angle 3-20 displays “correct” and otherwise display “Wrong” however i am running into the issue that it displays “correct” between angles “3-20” AND angles “177-160” I have tried telling it to display “wrong” and even disabling "correct between those values but no success.

I fear that since those value match 3-20 (except reversed from 180) that they cant be edited without affecting the 3-20 values (which i need).

Can anyone offer any advise or tips please?

SCRIPT

{
public float minX = -360.0f;
public float maxX = 360.0f;

public float minY = -45.0f;
public float maxY = 45.0f;

public float sensX = 100.0f;
public float sensY = 100.0f;

float rotationY = 0.0f;
float rotationX = 0.0f;

public GameObject BearingButton;
public GameObject WrongText;
public GameObject RightText;

Euler angles can’t be reliably/consistently converted in this way. While any one set of Euler numbers can be converted reliably into the same Quaternion, the reverse operation will not result in the same three numbers in all scenarios - your Z rotation might appear to be changing erratically when you’re simply rotating around an axis, but the X and Y rotations will have different numbers that make the rotation correct, because there are multiple combinations of Euler numbers that all result in the same rotation. (In its most basic form, think (1,1,1) and (361,361,-359) - these are the same rotation, even though none of the numbers are the same. There are weirder combinations of identical rotations that result in situations like yours)

The Inspector only keeps these numbers human-readable by cheating: it remembers what numbers you typed in, so it doesn’t have to do the unreliable quaternion-to-euler conversion. So when you type in (270,13,45) it knows those are the values you expect, even though there are many other ways it could display that rotation.

However, your script includes no such cheating. If you set transform.localEulerAngles =new Vector3(380f, 0f, 0f), and then get transform.localEulerAngles back, it’s more likely to tell you (20, 0, 0). Mix in those “weirder combinations” I mentioned earlier, and hopefully you can see how you can’t rely on any particular value in any component of the euler angles for testing.

So when testing for those values, don’t get the Euler angles back from the quaternion - use the values you had originally set the rotation with in the first place, rotationX and rotationY. Treat transform.rotation as “output only”.

@StarManta thank you for your explanation and time I greatly appreciate it. Now I can quit wasting my time with it and look for an alternate more reliable method. Thank you.

Depending on how symmetrical your object is, it might appear that your getting a Correct, when it should be wrong… but your are indeed at the correct X rotation. (That is its not actually 160-176 but is indeed 3-20). Try adding some Debugs or Text Boxes that dispaly the X, Y rotations when you get Right and Wrong TextBoxes. If you spin 180 degrees around the Y-axis… then a 3 degree rotation on the X will look like 177 but its still only 3. Its because your object has spun 180 degrees and is facing “backwards” now. So when it rotates 3 degrees around the X its doing it backwards from the same object with 0 degree Y rotation. Your code makes it impossible for a eulerangle of 160 to be “correct” so if it shows correct you just are thinking it looks like 160 when in reality its a 20 degree rotation around the X-axis. The other rotations are making this illusion.

What exactly are you trying to accomplish with the 3-20 degrees in the X rotation = “correct” ?
There could be other ways to get the “direction” your pointing depending on how your project it setup and what your trying to accomplish.

Also this code here:

if (transform.rotation.eulerAngles.x >= (Mathf.Abs(-3)) && transform.rotation.eulerAngles.x <= (Mathf.Abs(-20)))

is Identical to this code:

if (transform.rotation.eulerAngles.x >= (Mathf.Abs(3)) && transform.rotation.eulerAngles.x <= (Mathf.Abs(20)))

So i’m surprised your not seeing the Right and Wrong boxes active at the same time. There is no need to use Math.Abs() on constant integers. Math.ABs(3) will ALWAYS be 3. and Math.ABs(-3) will ALWAYS be 3. You can just use 3 there without calling math.abs

@takatok Thank you for taking the time and explaining (i just picked up unity and scripting at the beginning of January and am still learning it. When you explain it like that I can understand that there is no need for math.ab and the -3/-20 and will clean up the script accordingly. I am also looking for an alternate method as well to achieve the desired rotation.