public struct Matrix4x4 : IEquatable<Matrix4x4>, IFormattable
Remarks
A transformation matrix can perform arbitrary linear 3D transformations (i.e. translation, rotation, scale, shear etc.) and perspective transformations using homogenous coordinates. You rarely use matrices in scripts; most often using Vector3s, Quaternions and functionality of Transform class is more straightforward. Plain matrices are used in special cases like setting up nonstandard camera projection.
Matrices in Unity are column major; i.e. the position of a transformation matrix is in the last column, and the first three columns contain x, y, and z-axes. Data is accessed as:
row + (column*4)
. Matrices can be indexed like 2D arrays but note that in an expression like
mat[a, b]
, a refers to the row index, while b refers to the column index.
Examples
using UnityEngine;public class ExampleScript : MonoBehaviour{ void Start() { // get matrix from the Transform var matrix = transform.localToWorldMatrix; // get position from the last column var position = new Vector3(matrix[0,3], matrix[1,3], matrix[2,3]); Debug.Log("Transform position from matrix is: " + position); }}