Search Unity

rotation problem

Discussion in 'Scripting' started by raoul, Aug 25, 2007.

  1. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Hi,

    I am trying to align an object (object 3) 90 degrees to the line between 2 other objects, this is on the y-axis. The transform of Object 1 is set using transform.RotateAround Object 2 where the angle depends on horizontal mouse movement. So I thought of using the rotation value of the y-axis of object 1 + 90 for object 3. This resulted in strange behaviour so for testing I just set the y rotation without the 90 degrees:

    Code (csharp):
    1.  
    2. var rotObj : Transform;
    3. var pAngle : float;
    4. ...
    5. pAngle = rotObj.transform.rotation.y;
    6. transform.rotation.y = pAngle;
    7. print(rotObj.transform.rotation.y + " - " + pAngle);
    8.  
    There are 2 things:
    1.Although very close, the rotation values are never 100% the same. Then when printing the values, it seems it is printing in radians while it is shown in degrees in the inspector panel. Is this where the difference comes from and if so, is there a way to always use degrees instead of radians?

    2. From 0 up to around 145 degrees of rotation on the y-axis on object 1 all is fine. The rotation value for object 3 is almost the same as object 1. Then towards 180 degrees for object 1, the rotation of object 3 stays behind and after the 180 degrees the rotation of object is actually going back to 0. The same happens when moving the mouse to the other direction. From 360 to 200 all is fine and where object 1 is rotating further towards 0, object 3 is rotating back to 360. During all of this the values printed in radians are exactly the same! Anyone who can tell me what is going on here?

    Is there a better method to calculate the y rotation for object 3?

    Thanks,
    Raoul
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Rotations are stored as Quaternions. I don't pretend to understand quaternions, but I know two things:

    1) the Y value of a quaternion has very little to do with what most people would consider the Y value of a rotation
    2) if you don't fully understand quaternions, never ever touch the x, y, z, and w properties of them.


    You may have better luck with Quaternion.eulerAngles
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep...just to clarify, transform.rotation is not what you want here. Instead you'd use transform.eulerAngles. There are some potential "gotchas" when reading individual axes from eulerAngles, due to being converted from quaternions...you can read up on that in the docs.

    --Eric
     
  4. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Thanks both, transform.eulerAngles did it!