- Home /
Problem when checking a Quaternion Eulerangle axis
So, as shown in the code attached below I am having a problem checking the Z axis of my gameobject. The odd part is that it's just the line: "CameraBody.transform.eulerAngles.z <= rotation2 " at the bottom, that would be line #52. If I take that away and just check the boolean then the if statement goes off. But it seems for some reason that the addition of checking the angle doesn't work. So for example Rotation 2 is set at -50 and the cameraBody is rotating towards negative along the Z-axis. Once the Camera passes -50 and continues lower into the negatives the if statement doesn't go off. But upon switching the switch statement case to "greater than" so that line #39, the if statement will go off just fine. Any ideas or any already answered questions would be appreciated. Thank you!
void Update () {
QuestionMark ();
if (detectingPlayer == false && detectedplayer == false) {
switch (Operator1) {
case ">":
if (ReverseRotation == false && cameraBody.transform.eulerAngles.z >= rotation1) {
cameraBody.transform.localRotation *= Quaternion.Euler (0, 0, 0.2f);
//Debug.Log ("Positive Rotation 1");
} else if(ReverseRotation != true) {
Debug.Log ("Making reverse rotation true #1");
ReverseRotation = true;
}
if(ReverseRotation == true) {
//ReverseRotation = true;
cameraBody.transform.localRotation *= Quaternion.Euler (0, 0, -0.2f);
//Debug.Log ("Negative Rotation 1");
}
break;
case "<":
if (ReverseRotation == false && cameraBody.transform.eulerAngles.z <= rotation1) {
cameraBody.transform.localRotation *= Quaternion.Euler (0, 0, 0.2f);
//Debug.Log ("Positive Rotation 2");
} else if(ReverseRotation != true) {
Debug.Log ("Making reverse rotation true #2");
ReverseRotation = true;
}
if(ReverseRotation == true) {
//ReverseRotation = true;
cameraBody.transform.localRotation *= Quaternion.Euler (0, 0, -0.2f);
//Debug.Log ("Negative Rotation 1");
}
break;
}
switch (Operator2) {
case ">":
//Debug.Log ("In greater than, Operator 2 A");
if (cameraBody.transform.eulerAngles.z >= rotation2 && ReverseRotation == true) {
ReverseRotation = false;
//cameraBody.transform.localRotation *= Quaternion.Euler (0, 0, -0.4f);
Debug.Log ("Reverse Rotation = false 3");
}
break;
case "<":
Debug.Log ("In less than, Operator 2 B");
Debug.Log ("Reverse rotation is: " + ReverseRotation);
//Debug.Log ("Rotation 2 is: " + rotation2);
if (cameraBody.transform.eulerAngles.z <= rotation2 && ReverseRotation == true) {
ReverseRotation = false;
//cameraBody.transform.localRotation *= Quaternion.Euler (0, 0, -0.4f);
Debug.Log ("Reverse Rotation = false 4");
}
break;
}
Answer by ElijahShadbolt · Feb 25, 2018 at 02:01 AM
An answer from this question may be of help. Euler angles are stored as values (in degrees) from 0 to 360, not -180 to 180. Instead of comparing it to -50, maybe try 310 ? You would also have to check two ranges: 0 to 50 and 310 to 360, instead of just -50 to 50.
Thank you very much for that link to someone else's solution, it's exactly what I needed! Also, thank you for that information on Euler Angles, i never knew that it was only 0 to 360. I'll keep that in $$anonymous$$d next time I mess around with Euler Angles!
Your answer
Follow this Question
Related Questions
Instantiating an object in front of the player 3 Answers
Continuously rotating by 90 doesn't get me the initial angle 0 Answers
Rotation losing precision 0 Answers
Whats the Difference Between Rotation EulerAngles and Quaternions? 2 Answers
How can I store the individual values of EulerAngles on a Database then re-apply 2 Answers