keywords:Unity, Linear Algebra & Plane Geometry API

Basic concept


Please explain Quaternions!


unity3D 彻底搞懂 Quaternion LookRotation FromToRotation SetLookRotation方法


Convert Quaternion to Direction Vector

A quaternion doesn’t have a direction by itself. It is a rotation. It can be used to rotate any vector by the rotation it represents. Just multiply a Vector3 by the quaternion:

Vector3 targetForward = targetRot * Vector3.forward;
Vector3 targetUp = targetRot * Vector3.up;


Convert Direction Vector to Quaternion
Vector3 BaseDire = new Vector3(0f, 1f, 0f);
Quaternion Rot = Quaternion.LookRotation(DestDirection, BaseDire);
Convert Quaternion to Matrix4x4
Quaternion rotation = GameOjb.transform.rotation;
Matrix4x4 m = Matrix4x4.Rotate(rotation);


Vector Calculating

Converting Matrix4x4 (Rotation Matrix) to Quaternion
public static Quaternion QuaternionFromMatrix(Matrix4x4 m) 
    return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1)); 


Converting Matrix4x4 (Rotation Matrix) to Vector3 (Direction)
Matrix4x4 Matrix = Matrix4x4.Rotate(Obj.transform.rotaion);
Vector3 MoveDirection = Matrix.MultiplyVector(Vector3.forward);
Transforms direction from local space to world space
public Vector3 TransformDirection(Vector3 direction);

You should use Transform.TransformPoint for the conversion if the vector represents a position rather than a direction.

public Vector3 TransformPoint(Vector3 position);


Transforms a direction from world space to local space
public Vector3 InverseTransformDirection(Vector3 direction);

You should use Transform.InverseTransformPoint if the vector represents a position in space rather than a direction.

public Vector3 InverseTransformPoint(Vector3 position);


Quaternion Calculating

Converts a rotation to angle-axis representation (angles in degrees).
// Extracts the angle - axis rotation from the transform rotation
float angle = 0.0f;
Vector3 axis =;
transform.rotation.ToAngleAxis(out angle, out axis);


Creates a rotation which rotates angle degrees around axis.
// Sets the transform's current rotation to a new rotation that rotates 30 degrees around the y-axis(Vector3.up)
transform.rotation = Quaternion.AngleAxis(30, Vector3.up);


how do I modify Quaternion to zero rotations along certain axes?
//Reset x-axis and z-axis to zero.
Transform TraWS = CameraComp.transform;
TraWS.localEulerAngles = new Vector3(0, TraWS.localEulerAngles.y, 0);


Get angle of a Quaternion in axes
Vector3 AnglesVec = RotNewRot.eulerAngles;
Get normalize direction of Quaternion in certain axis
Vector3 forwardDirAfterRoll = rollRotation * Vector3.forward;
Vector3 upDirAfterRoll = rollRotation * Vector3.up;
Vector3 rightDirAfterRoll = rollRotation * Vector3.right;


In individuals, insanity is rare; but in groups, parties, nations and epochs, it is the rule. ― Friedrich Nietzsche