How to get rotation of a Game Object??

Code:

I’m sure the error is within this part, because when I change both returns to true, then it works like a charm, resetting the game every frame.

I’ve also tried currentHandle.transform.rotation.Equals(0) != true for the conditional
and currentHandle.transform.rotation.z != 0.
AND currentHandle.transform.rotation != Quaternion.Identity
AND ALSO currentHandle.transform.rotation != Quaternion.EulerAngles(0, 0, 0)
You get the point…

Help would be most appreciated!

Never test floating point (float) quantities for equality / inequality. Here’s why:

https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

“Think of [floating point] as JPEG of numbers.” - orionsyndrome on the Unity3D Forums

Literal float / double issues:

And thanks to halley for this handy little visual floating point converter:

https://www.h-schmidt.net/FloatConverter/IEEE754.html

As an alternative to trying to compare exactly:

bool closeEnough = Mathf.Abs(a - b) < margin;

Set “margin” to some reasonably small value. As long as a and b are no further apart than that value, the result will be true.

Note that there is Mathf.Approximate, but that only gives you a very, very small margin (the epsilon value). That’s probably not appropriate if you’re checking if a handle is in the right position!

Still not working. Here’s my program:

using UnityEngine;
using TMPro;

public class AllClocks : MonoBehaviour
{

// All Clocks

public GameObject[ ] longHandles = new GameObject[9];

public TextMeshProUGUI WinText;
public GameObject Button;

// Winstate stuff

private bool CheckWinState()
{
for (int i = 0; i < longHandles.Length; i++)
{

// Resetting Values

bool rotationMoreThanNegMargin;
bool rotationLessThanMargin;

// Initializing

GameObject currentHandle = longHandles*;*
//Variables
int margin = 5;
int negMargin = margin * -1;
//If the rotation is more than -5.
if (currentHandle.transform.rotation.z > negMargin)
{
rotationMoreThanNegMargin = true;
}
else
{
rotationMoreThanNegMargin = false;
}
//If the rotation is less than 5.
if (currentHandle.transform.rotation.z < margin)
{
rotationLessThanMargin = true;
}
else
{
rotationLessThanMargin = false;
}
if (rotationLessThanMargin == true && rotationMoreThanNegMargin == true)
{
return false;
}
}
return true;
}
private int clockIndex = 100;
KeyCode[ ] INPUT_REFERENCES = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8, KeyCode.Alpha9};
private void Start()
{
RandomizeClockRotations();
}
private void Update()
{
if (CheckWinState())
{
EndGame();
}
for (int i = 0; i < INPUT_REFERENCES.Length; i++)
{
if (Input.GetKey(INPUT_REFERENCES*))
_
{_
clockIndex = i + 1;
_
}_
_
}_
if (Input.GetKeyDown(KeyCode.Alpha0))
_
{_
for (int i = 0; i < longHandles.Length; i++)
_
{_
if (clockIndex <= 9 && clockIndex >= 0)
_
{_
if (i == clockIndex - 1)
_
{_
_longHandles.transform.Rotate(Vector3.back, 60);
}
else
{_
_longHandles.transform.Rotate(Vector3.forward, 60);
}
}
}
}
}
private void RandomizeClockRotations()
{
for (int i = 0; i < longHandles.Length; i++)
{
int initialRandomNumber = Random.Range(0, 3);_

int rotationRandomnumber = 120 * initialRandomNumber;
_longHandles.transform.Rotate(Vector3.forward, rotationRandomnumber);
}
}
private void EndGame()
{
enabled = false;
WinText.text = “You Win!”;
Button.SetActive(true);
}
public void ResetState()
{
enabled = true;
RandomizeClockRotations();
WinText.text = “”;
Button.SetActive(false);
clockIndex = 100;
}
}*_

Be aware that the values you read in the inspector for transform and rotation values are their local values, and in particular with rotation it’s a ‘readable’ version of the actual rotation values going under the hood.

Start Debug.Log()-ing to see what numbers are being read off the objects eular angles and where your code is going wrong. This should have been your first step in general, otherwise you’re shooting in the dark.

Also use code tags next time, please. There’s instructions stickied to this subforum.

using UnityEngine;
using TMPro;

public class AllClocks : MonoBehaviour
{

    // All Clocks




    public GameObject[] longHandles = new GameObject[9];


    public TextMeshProUGUI WinText;
    public GameObject Button;

    // Winstate stuff

    private bool CheckWinState()
    {
        for (int i = 0; i < longHandles.Length; i++)
        {

            // Resetting Values

            bool rotationMoreThanNegMargin;
            bool rotationLessThanMargin;

            // Initializing

            GameObject currentHandle = longHandles[i];

            //Variables

            int margin = 5;
            int negMargin = margin * -1;

            //If the rotation is more than -5.

            if (currentHandle.transform.rotation.z > negMargin)
            {
                rotationMoreThanNegMargin = true;
            }
            else
            {
                rotationMoreThanNegMargin = false;
            }

            //If the rotation is less than 5.

            if (currentHandle.transform.rotation.z < margin)
            {
                rotationLessThanMargin = true;
            }
            else
            {
                rotationLessThanMargin = false;
            }


            if (rotationLessThanMargin  == true && rotationMoreThanNegMargin == true)
            {

                return false;
            }
           
        }
        return true;
    }
   
   
    private int clockIndex = 100;

   
    KeyCode[] INPUT_REFERENCES = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8, KeyCode.Alpha9};
    private void Start()
    {
        RandomizeClockRotations();
    }
   
    private void Update()
    {

        if (CheckWinState())
            {  
                EndGame();
            }
       
        for (int i = 0; i < INPUT_REFERENCES.Length; i++)
        {
            if (Input.GetKey(INPUT_REFERENCES[i]))
            {
                clockIndex = i + 1;
            }
        }
        if (Input.GetKeyDown(KeyCode.Alpha0))
            {
               
               

                for (int i = 0; i < longHandles.Length; i++)
                {
                    if (clockIndex <= 9 && clockIndex >= 0)
                    {
                    if (i == clockIndex - 1)
                    {
                        longHandles[i].transform.Rotate(Vector3.back, 60);
                    }
                   
                    else
                    {

                        longHandles[i].transform.Rotate(Vector3.forward, 60);
                    }
                    }
                }
               
            }

       
    }
    private void RandomizeClockRotations()
    {
    for (int i = 0; i < longHandles.Length; i++)
    {
        int initialRandomNumber = Random.Range(0, 3);
        int rotationRandomnumber = 120 * initialRandomNumber;
        longHandles[i].transform.Rotate(Vector3.forward, rotationRandomnumber);
    }
    }

    private void EndGame()
    {
        enabled = false;
        WinText.text = "You Win!";
        Button.SetActive(true);
    }
    public void ResetState()
    {
        enabled = true;
        RandomizeClockRotations();
        WinText.text = "";
        Button.SetActive(false);
        clockIndex = 100;

    }
   

}