Search Unity

Problem With Rotations and LocalEulerAngles

Discussion in 'Scripting' started by Kushovu, Jun 18, 2018.

  1. Kushovu

    Kushovu

    Joined:
    May 26, 2018
    Posts:
    12
    Hi there!
    I got this problem when trying to set as correct and incorret a variable depending on collider rotations
    the problem its that somethimes it just screw up all the rotations and set as correct when should be incorrect, i checked also all the code but didnt find anything, here is my code

    Code (CSharp):
    1.  
    2.                 // Check if has No Collition so set it as buildable
    3.                 if (MainColliderList.Count == 0) SetBuildable(true);
    4.                 // Check if has Only 1 Collition
    5.                 else if (MainColliderList.Count == 1)
    6.                 {
    7.                     // Check if that collition Type is same as this
    8.                     if (Type == MainColliderList[0].GetComponent<PlacedObjectID>().Type)
    9.                     {
    10.                         // Set Colition Y Rotation to a Variable
    11.                         NormalizedColRot = (int)MainColliderList[0].transform.gameObject.transform.localEulerAngles.y;
    12.                         // Set This Y Rotation to a Variable
    13.                         NormalizedThisRot = (int)transform.gameObject.transform.localEulerAngles.y;
    14.                            
    15.                         if (Type == ObjectTypes.TriangleFoundation || Type == ObjectTypes.HalfSquareFoundation)
    16.                         {
    17.                             // Invert Col Rotation to check it later
    18.                             NormalizedColRot += 180;
    19.                             // Check if it Excedes the 355 it should Restart 360 grades
    20.                             if (NormalizedColRot >= 355) NormalizedColRot -= 360;
    21.                             // Check if Colition rotation its close to This Rotation Inverted then Set Buildable
    22.                             if (NormalizedColRot + 2f > NormalizedThisRot && NormalizedColRot - 2f < NormalizedThisRot) SetBuildable(true);
    23.                             else SetBuildable(false);
    24.                         }
    25.                         else if (Type == ObjectTypes.HalfTriangleFoundation || Type == ObjectTypes.QuarterSquareFoundation)
    26.                         {
    27.                             // Check if Colition rotation its close to This Rotation then Set Blocked
    28.                             if (NormalizedColRot + 2f > NormalizedThisRot && NormalizedColRot - 2f < NormalizedThisRot) SetBuildable(false);
    29.                             else SetBuildable(true);
    30.                         }
    31.                     }
    32.                 }
    33.                 // Check if has Only 2 Collition
    34.                 else if (MainColliderList.Count == 2)
    35.                 {
    36.                     // Check if that collition Type is same as this
    37.                     if (Type == MainColliderList[0].GetComponent<PlacedObjectID>().Type && Type == MainColliderList[1].GetComponent<PlacedObjectID>().Type)
    38.                     {
    39.                         // Set First Colition Y Rotation to a Variable
    40.                         NormalizedColRot = (int)MainColliderList[0].transform.gameObject.transform.localEulerAngles.y;
    41.                         // Set Second Colition Y Rotation to a Variable
    42.                         NormalizedColRot1 = (int)MainColliderList[1].transform.gameObject.transform.localEulerAngles.y;
    43.                         // Set This Y Rotation to a Variable
    44.                         NormalizedThisRot = (int)transform.gameObject.transform.localEulerAngles.y;
    45.  
    46.                         if (Type == ObjectTypes.HalfTriangleFoundation || Type == ObjectTypes.QuarterSquareFoundation)
    47.                         {
    48.                             // Check if Colition rotation of two objecrt its close to This Rotation then Set Blocked
    49.                             if (NormalizedColRot0 - 2f < NormalizedThisRot && NormalizedColRot0 + 2f > NormalizedThisRot ||
    50.                                 NormalizedColRot1 - 2f < NormalizedThisRot && NormalizedColRot1 + 2f > NormalizedThisRot)
    51.                                 SetBuildable(false);
    52.                             else SetBuildable(true);
    53.                         }
    54.                         else SetBuildable(false);
    55.                     }
    56.                 }
    57.                 // Check if has Only 3 Collition
    58.                 else if (MainColliderList.Count == 3)
    59.                 {
    60.                     // Check if that collition Type is same as this
    61.                     if (Type == MainColliderList[0].GetComponent<PlacedObjectID>().Type && Type == MainColliderList[1].GetComponent<PlacedObjectID>().Type && Type == MainColliderList[2].GetComponent<PlacedObjectID>().Type)
    62.                     {
    63.                         // Set First Colition Y Rotation to a Variable
    64.                         NormalizedColRot = (int)MainColliderList[0].transform.gameObject.transform.localEulerAngles.y;
    65.                         // Set Second Colition Y Rotation to a Variable
    66.                         NormalizedColRot1 = (int)MainColliderList[1].transform.gameObject.transform.localEulerAngles.y;
    67.                         // Set Thirt Colition Y Rotation to a Variable
    68.                         NormalizedColRot2 = (int)MainColliderList[2].transform.gameObject.transform.localEulerAngles.y;
    69.                         // Set This Y Rotation to a Variable
    70.                         NormalizedThisRot = (int)transform.gameObject.transform.localEulerAngles.y;
    71.  
    72.                         if (Type == ObjectTypes.HalfTriangleFoundation || Type == ObjectTypes.QuarterSquareFoundation)
    73.                         {
    74.                             // Check if Colition rotation of three objecrt its close to This Rotation then Set Blocked
    75.                             if (NormalizedColRot0 - 2f < NormalizedThisRot && NormalizedColRot0 + 2f > NormalizedThisRot ||
    76.                                 NormalizedColRot1 - 2f < NormalizedThisRot && NormalizedColRot1 + 2f > NormalizedThisRot ||
    77.                                 NormalizedColRot2 - 2f < NormalizedThisRot && NormalizedColRot2 + 2f > NormalizedThisRot)
    78.                                 SetBuildable(false);
    79.                             else SetBuildable(true);
    80.                         }
    81.                         else SetBuildable(false);
    82.                     }
    83.                 }
    84.                 else SetBuildable(false);
    Maybe it would be a better way of doing this, also added that for changin rotation of my current object, i just do this:
    Code (CSharp):
    1.  if (Input.GetButtonDown("Fire2")) CurrentRotation += new Vector3(0, 90, 0);
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    I don't really understand your code very well but one thing I did notice is that you're using the transform.localEulerAngles to check against - that's generally a bad idea. Unity stores them as quaternions internally so you might not get back what you think.

    For example, if you set transform.localEulerAngles to 90,180,0 then get it back again it might have become 90,270,90. These both represent the same visual angle but would upset your code if it's relying on the values staying the same.

    The easiest way to solve it is to use your own Vector3 variable (e.g. myLocalEulerAngles) for all the calculations and for adding the rotation when 'Fire2' is pressed. In your update loop you can set the gameObject.transform.localEulerAngles with your 'myLocalEulerAngles'.
     
    Kushovu likes this.
  3. Kushovu

    Kushovu

    Joined:
    May 26, 2018
    Posts:
    12
    Yeah after you said that i check and yes the problem its that i was searching for 0 grades and the object has 360 as you said they visually looks the same but have different values, so this can be solved by substracting 360 after they get 360 grades or more, i suppose this would work, ill try and post back result, also thanks bro!
     
  4. Kushovu

    Kushovu

    Joined:
    May 26, 2018
    Posts:
    12
    Got it Solved, You were right the problem was that i was comparing directly two float values extracted from localEulerAngles, The solution was to instead of storing a float, i stored a quaternion, so i can use Quaternion.Angle To compare them and then just check for a threshold of that angle and done!
     
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Cool, glad it's all working - Eulers can be a bit of a pain to work with. Best to stick with Quaternions :)
     
    Kushovu likes this.