Search code examples
c#unity-game-enginevectorquaternions

Unity - converting a Quaternion to a Vector3


I've seen quite a lot of questions like this, and the trouble is - the question makes no sense. They are not the same thing, it is a bit like saying "how do I convert an apple into a brick"?

But usually the underlying question does make sense, it is the wording that is suspect.

What I want to do is this:

  • I have an object in the world space frame of reference with a rotation represented by the Quaternion R.
  • if that object were to move forwards relative to its local frame of reference, ie straight along its local Z axis, by a nominal amount ...
  • ... what would be the Vector3 whose (normalised) components represent that movement in the world frame of reference?

Now, I think the answer is

    Vector3 ans = R * Vector3.forward;

Is this right, and if so, why? (If not right, then what is the answer?)


Solution

  • Yes that is correct.

    Quaternion * Vector3 takes a given vector and rotates it according to the given rotation.

    So if your R is basically the object's world-space transform.rotation then the result will be the objects local forward (positive Z) vector as a vector in world space coordinates.


    You can also directly use the object's transform.forward vector which basically equals transform.rotation * Vector3.forward