Search Unity

Quaternion values between inspector and script

Discussion in 'Scripting' started by shaundavies, Jan 31, 2018.

  1. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    Hi I cant figure out why I am getting different quaternion values between altering objects via inspector and through script.

    I am trying to rotate an object and have constraints set that when the quaternion X and W values meet a threshold to spin negating the oscillation of quaternion values. My current script works when I rotate an object through the inspector. I cant find a way to rotate an object in game however through quaternion it appears the rotate command is the only solution and when uses that my script does not register my constraints.

    Any thoughts would be really appreciated.
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Can you show a code example of how you're trying to rotate an object in script?
     
  3. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    Sure - Have this object as a child to circle swap gameObject. I have circle swap at rotation (0,0,0)

    Code (CSharp):
    1.  
    2.         if (transform.localRotation.x < 0 && transform.localRotation.w < 0)
    3.         {
    4.  
    5.             if (!alter)
    6.             {
    7.                 transform.localRotation = new Quaternion(0, transform.localRotation.y, transform.localRotation.z, 1);
    8.                 circleSwap.transform.localRotation = new Quaternion(180, circleSwap.transform.localRotation.y, circleSwap.transform.localRotation.z, circleSwap.transform.localRotation.w);
    9.                 alter = true;
    10.             }
    11.         }
    12.  
    13.         if (transform.localRotation.x > 0 && transform.localRotation.w > 0)
    14.         {
    15.  
    16.             if (alter)
    17.             {
    18.                 transform.localRotation = new Quaternion(0, transform.localRotation.y, transform.localRotation.z, 1);
    19.                 circleSwap.transform.localRotation = new Quaternion(0, circleSwap.transform.localRotation.y, circleSwap.transform.localRotation.z, circleSwap.transform.localRotation.w);
    20.                 alter = false;
    21.             }
    22.         }
    23.  
    24.  
    25.         // Left Rotation
    26.  
    27.         if (transform.localRotation.x > 0 && transform.localRotation.w < 0)
    28.         {
    29.  
    30.             if (!alter2)
    31.             {
    32.                 transform.localRotation = new Quaternion(0, transform.localRotation.y, transform.localRotation.z, 1);
    33.                 circleSwap.transform.localRotation = new Quaternion(180, circleSwap.transform.localRotation.y, circleSwap.transform.localRotation.z, circleSwap.transform.localRotation.w);
    34.                 alter2 = true;
    35.             }
    36.         }
    37.  
    38.         if (transform.localRotation.x < 0 && transform.localRotation.w > 0)
    39.         {
    40.  
    41.             if (alter2)
    42.             {
    43.                 transform.localRotation = new Quaternion(0, transform.localRotation.y, transform.localRotation.z, 1);
    44.                 circleSwap.transform.localRotation = new Quaternion(0, circleSwap.transform.localRotation.y, circleSwap.transform.localRotation.z, circleSwap.transform.localRotation.w);
    45.                 alter2 = false;
    46.             }
    47.         }
    48.  
    49.  
    50.         print(transform.localRotation.x + " " + transform.localRotation.w);
     
  4. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    So this works with Rotating the object through the inspector
     
  5. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    When I add say -
    transform.Rotate(100 * Time.deltaTime, 0, 0);

    Everything in that code is negated and the values oscillate

    if I try -
    Quaternion pos = transform.localRotation;
    pos.x += 1 * Time.deltaTime;
    transform.localRotation = pos;

    the values stop at 1 and the ability for the math to add linearly dies past 180 degrees. It appears that Unity is capping this quaternion value behind the scenes.
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Looks like you're making the mistake everyone makes on their 1st try: mixing raw quaternion values and degrees (there are dozens of old Qs like this, with various explanations.)
     
    BlackPete and lordofduct like this.
  7. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Yep, if you want to convert degrees into a quaternion, use Quaternion.Euler.
     
  8. shaundavies

    shaundavies

    Joined:
    Jan 31, 2017
    Posts:
    44
    Thanks for the responses!! I believe I have solved the problem. I learned my lesson to not use the inspector for rotation and do everything through script. I had to rewrite my code a bit, but coincidentally it was simpler. :D