ptamzz
ptamzz

Reputation: 9365

How to increase rotation angle with Unity using Quaternion

In my Unity scene, I'm trying to rotate a cube wrt the head movement. Here's my code:

m_cubeName.rotation = Quaternion.Lerp(m_cubeName.rotation, m_Camera.rotation, Time.deltaTime);

It seems to work and rotates exactly the same angle as the head. I want to use a multiplying factor so that when the head rotates, say 1 degree, the cube rotates 2 degrees.

So how do I convert the qauternion rotation value to something I can multiply with a factor?

Upvotes: 4

Views: 2508

Answers (1)

31eee384
31eee384

Reputation: 2803

To rotate the cube by a factor of 2, just rotate it twice:

Quaternion doubleCameraRotation = m_Camera.rotation * m_Camera.rotation;

m_cubeName.rotation = Quaternion.Lerp(
    m_cubeName.rotation,
    doubleCameraRotation,
    Time.deltaTime);

To multiply the rotation by a non-integer factor, you can use Quaternion.LerpUnclamped, (or SlerpUnclamped for better accuracy) then pass the scaling factor as t. For example:

Quaternion doubleCameraRotation = 
    Quaternion.LerpUnclamped(Quaternion.identity, m_Camera.rotation, 2f);

Upvotes: 4

Related Questions