Quaternions: Set up an Quaternion to give a specific angle on a specific axis

Again, just dropping this here so I know where to find it. The following will set up a 1 degree rotation around the x axis (in this case it’s just rotating a Unity transform by 1 degree in the x)

    Quaternion q = a_activeMesh.m_meshFilter.gameObject.transform.rotation;
    Quaternion q2 = CreateFromAxisAngle(1.0f, 0.0f, 0.0f, 1.0f * Time.deltaTime);
    q = q * q2;
    a_activeMesh.m_meshFilter.gameObject.transform.rotation = q;
Quaternion CreateFromAxisAngle(float x, float y, float z, float a)
    // Here we calculate the sin( theta / 2) once for optimization
    float factor = Mathf.Sin(a / 2.0f);

    Quaternion q;

    q.x = x * factor;
    q.y = y * factor;
    q.z = z * factor;

    // Calcualte the w value by cos( theta / 2 )
    q.w = Mathf.Cos(a / 2.0f );

    return q.normalized;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: