Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to get rotation of a Game Object??

Discussion in 'Scripting' started by TheFastOne, Jan 13, 2023.

  1. TheFastOne

    TheFastOne

    Joined:
    Dec 20, 2022
    Posts:
    6
    Code: Screen Shot 2023-01-12 at 8.49.43 PM.png


    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!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Never test floating point (float) quantities for equality / inequality. Here's why:

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

    https://forum.unity.com/threads/debug-log-2000-0f-000-1f-is-200.1153397/#post-7399994

    https://forum.unity.com/threads/why-doesnt-this-code-work.1120498/#post-7208431

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

    Literal float / double issues:

    https://forum.unity.com/threads/err...annot-be-implicitly-con.1093000/#post-7038139

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

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

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    As an alternative to trying to compare exactly:

    Code (CSharp):
    1. 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!
     
  4. TheFastOne

    TheFastOne

    Joined:
    Dec 20, 2022
    Posts:
    6
    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;

    }


    }
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,012
    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.
     
  6. TheFastOne

    TheFastOne

    Joined:
    Dec 20, 2022
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class AllClocks : MonoBehaviour
    5. {
    6.  
    7.     // All Clocks
    8.  
    9.  
    10.  
    11.  
    12.     public GameObject[] longHandles = new GameObject[9];
    13.  
    14.  
    15.     public TextMeshProUGUI WinText;
    16.     public GameObject Button;
    17.  
    18.     // Winstate stuff
    19.  
    20.     private bool CheckWinState()
    21.     {
    22.         for (int i = 0; i < longHandles.Length; i++)
    23.         {
    24.  
    25.             // Resetting Values
    26.  
    27.             bool rotationMoreThanNegMargin;
    28.             bool rotationLessThanMargin;
    29.  
    30.             // Initializing
    31.  
    32.             GameObject currentHandle = longHandles[i];
    33.  
    34.             //Variables
    35.  
    36.             int margin = 5;
    37.             int negMargin = margin * -1;
    38.  
    39.             //If the rotation is more than -5.
    40.  
    41.             if (currentHandle.transform.rotation.z > negMargin)
    42.             {
    43.                 rotationMoreThanNegMargin = true;
    44.             }
    45.             else
    46.             {
    47.                 rotationMoreThanNegMargin = false;
    48.             }
    49.  
    50.             //If the rotation is less than 5.
    51.  
    52.             if (currentHandle.transform.rotation.z < margin)
    53.             {
    54.                 rotationLessThanMargin = true;
    55.             }
    56.             else
    57.             {
    58.                 rotationLessThanMargin = false;
    59.             }
    60.  
    61.  
    62.             if (rotationLessThanMargin  == true && rotationMoreThanNegMargin == true)
    63.             {
    64.  
    65.                 return false;
    66.             }
    67.            
    68.         }
    69.         return true;
    70.     }
    71.    
    72.    
    73.     private int clockIndex = 100;
    74.  
    75.    
    76.     KeyCode[] INPUT_REFERENCES = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8, KeyCode.Alpha9};
    77.     private void Start()
    78.     {
    79.         RandomizeClockRotations();
    80.     }
    81.    
    82.     private void Update()
    83.     {
    84.  
    85.         if (CheckWinState())
    86.             {  
    87.                 EndGame();
    88.             }
    89.        
    90.         for (int i = 0; i < INPUT_REFERENCES.Length; i++)
    91.         {
    92.             if (Input.GetKey(INPUT_REFERENCES[i]))
    93.             {
    94.                 clockIndex = i + 1;
    95.             }
    96.         }
    97.         if (Input.GetKeyDown(KeyCode.Alpha0))
    98.             {
    99.                
    100.                
    101.  
    102.                 for (int i = 0; i < longHandles.Length; i++)
    103.                 {
    104.                     if (clockIndex <= 9 && clockIndex >= 0)
    105.                     {
    106.                     if (i == clockIndex - 1)
    107.                     {
    108.                         longHandles[i].transform.Rotate(Vector3.back, 60);
    109.                     }
    110.                    
    111.                     else
    112.                     {
    113.  
    114.                         longHandles[i].transform.Rotate(Vector3.forward, 60);
    115.                     }
    116.                     }
    117.                 }
    118.                
    119.             }
    120.  
    121.        
    122.     }
    123.     private void RandomizeClockRotations()
    124.     {
    125.     for (int i = 0; i < longHandles.Length; i++)
    126.     {
    127.         int initialRandomNumber = Random.Range(0, 3);
    128.         int rotationRandomnumber = 120 * initialRandomNumber;
    129.         longHandles[i].transform.Rotate(Vector3.forward, rotationRandomnumber);
    130.     }
    131.     }
    132.  
    133.     private void EndGame()
    134.     {
    135.         enabled = false;
    136.         WinText.text = "You Win!";
    137.         Button.SetActive(true);
    138.     }
    139.     public void ResetState()
    140.     {
    141.         enabled = true;
    142.         RandomizeClockRotations();
    143.         WinText.text = "";
    144.         Button.SetActive(false);
    145.         clockIndex = 100;
    146.  
    147.     }
    148.    
    149.  
    150. }
    151.