Search Unity

How to create a new rotation using quaternion?

Discussion in 'General Graphics' started by ambareeshsrja16, Jul 11, 2019.

  1. ambareeshsrja16

    ambareeshsrja16

    Joined:
    Jun 26, 2019
    Posts:
    12
    I have a quaternion : [0.87497, -0.14171, -0.45028, -0.107673], and I want to set an object rotation directly using these values.

    I'm looking for something along the lines of:

    newObj.transform.rotation = Quaternion.<something>( 0.87497 , -0.14171 , -0.45028 , -0.107673);


    What I have been doing so far is:

    newObj.transform.rotation = Quaternion.identity;
    newObj.transform.Rotate(rotX, rotY, rotZ, Space.World);

    Where I calculate rotX, rotY and rotZ by converting quaternions into Euler angles. But I am guessing there should be a direct way of using quaternions.
     
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Maybe this script will be helpful:

    Code (CSharp):
    1. // A quaternion is a four-element vector that can be used to encode any rotation in a 3D coordinate system.
    2. // Technically, a quaternion is composed of one real element and three complex elements.
    3. // https://github.com/przemyslawzaworski/Unity3D-C-programming/blob/master/quaternion.cs
    4.  
    5. using UnityEngine;
    6.  
    7. public class quaternion : MonoBehaviour
    8. {
    9.     Vector4 EulerToQuaternion(Vector3 p)
    10.     {
    11.         p.x *= Mathf.Deg2Rad;
    12.         p.y *= Mathf.Deg2Rad;
    13.         p.z *= Mathf.Deg2Rad;
    14.         Vector4 q;
    15.         float cy = Mathf.Cos(p.z * 0.5f);
    16.         float sy = Mathf.Sin(p.z * 0.5f);
    17.         float cr = Mathf.Cos(p.y * 0.5f);
    18.         float sr = Mathf.Sin(p.y * 0.5f);
    19.         float cp = Mathf.Cos(p.x * 0.5f);
    20.         float sp = Mathf.Sin(p.x * 0.5f);
    21.         q.w = cy * cr * cp + sy * sr * sp;
    22.         q.x = cy * cr * sp + sy * sr * cp;  
    23.         q.y = cy * sr * cp - sy * cr * sp;
    24.         q.z = sy * cr * cp - cy * sr * sp;
    25.         return q;
    26.     }
    27.  
    28.     Vector3 QuaternionToEuler (Vector4 p)
    29.     {
    30.         Vector3 v;
    31.         Vector4 q = new Vector4 (p.w, p.z, p.x, p.y);
    32.         v.y = Mathf.Atan2 (2f * q.x * q.w + 2f * q.y * q.z, 1 - 2f * (q.z * q.z + q.w * q.w));
    33.         v.x = Mathf.Asin (2f * (q.x * q.z - q.w * q.y));
    34.         v.z = Mathf.Atan2 (2f * q.x * q.y + 2f * q.z * q.w, 1 - 2f * (q.y * q.y + q.z * q.z));
    35.         v *= Mathf.Rad2Deg;
    36.         v.x = v.x>360 ? v.x-360 : v.x;
    37.         v.x = v.x<0 ? v.x+360 : v.x;
    38.         v.y = v.y>360 ? v.y-360 : v.y;
    39.         v.y = v.y<0 ? v.y+360 : v.y;
    40.         v.z = v.z>360 ? v.z-360 : v.z;
    41.         v.z = v.z<0 ? v.z+360 : v.z;
    42.         return v;
    43.     }
    44.  
    45.     void Start()
    46.     {
    47.         Vector3 euler = new Vector3(Random.Range(-360f,360f),Random.Range(-360f,360f),Random.Range(-360f,360f));
    48.         Vector4 quaternion = new Vector4 (this.transform.rotation.x,this.transform.rotation.y,this.transform.rotation.z,this.transform.rotation.w);
    49.         Debug.Log(EulerToQuaternion(euler).ToString("F8"));  
    50.         Debug.Log(Quaternion.Euler(euler).ToString("F8"));
    51.         Debug.Log(QuaternionToEuler(quaternion).ToString("F8"));
    52.         Debug.Log(this.transform.rotation.eulerAngles.ToString("F8"));
    53.     }
    54. }
     
    Silverhook likes this.
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,638
    You mean like this?
    Code (csharp):
    1.  
    2. newObj.transform.rotation = new Quaternion(0.87497, -0.14171, -0.45028, -0.107673);
    3.  
     
    autobr, Nakoru and viciousesque like this.