Search Unity

Rotate a quaternion 20 degrees around Y axis.

Discussion in 'Scripting' started by EducaSoft, May 27, 2011.

  1. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Hi,

    I have a quaternion containing valid data for a transforms rotation.

    How can I rotate this quaternion 20 degrees around Y axis ?

    So if my quaternion is called "myQuaternion" , how should I rotate it 20° around Y ?


    Kind regards,

    Bart
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,175
    myQuaternion *= Quaternion.Euler(Vector3.up * 20);

    --Eric
     
  3. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Yes Eric, so if I understand well, this multiplies the original quaternion with a quaternion representing a rotation around up vector (0,1,0) for 20° ?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,175
  5. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Ah, so rotating 10° on X, 20° on Y and 30° on Z is that then the line below or not ?

    myQuaternion *= Quaternion.Euler(Vector3(10,20,30));
     
  6. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    207
    Nope. You have to rotate around each axis individually, using the Vector of the axis times the amount of rotation.
     
  7. Vusa360

    Vusa360

    Joined:
    Oct 4, 2017
    Posts:
    10
    Just to add to this, if you are using this method in conjunction with Matrix4x4.TRS then there is a possibility you will get an error such as Quaternion To Matrix conversion failed because input Quaternion is invalid {0.000000, 0.819208, 0.000000, 0.573472} l=0.999972. To get round this you need to normalise the quaternion before using it. See this Unity answer.