# Matrix4x4

> A standard 4x4 transformation matrix.

## Definition

* **Type:** Struct
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule
* **Implements:** [IEquatable\<Matrix4x4>](https://learn.microsoft.com/dotnet/api/system.iequatable-1), [IFormattable](https://learn.microsoft.com/dotnet/api/system.iformattable)

```csharp
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 [Vector3](/engine/6000.7/script-reference/unityengine/vector3.md)s, [Quaternion](/engine/6000.7/script-reference/unityengine/quaternion.md)s and functionality of [Transform](/engine/6000.7/script-reference/unityengine/transform.md) class is more straightforward. Plain matrices are used in special cases like setting up nonstandard camera projection.

In Unity, several [Transform](/engine/6000.7/script-reference/unityengine/transform.md), [Camera](/engine/6000.7/script-reference/unityengine/camera.md), [Material](/engine/6000.7/script-reference/unityengine/material.md), [Graphics](/engine/6000.7/script-reference/unityengine/graphics.md) and [GL](/engine/6000.7/script-reference/unityengine/gl.md) functions use Matrix4x4.

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

```csharp
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);
    }
}
```

## Properties

| Property                                                                                            | Description                                                                                                     |
| --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| [decomposeProjection](/engine/6000.7/script-reference/unityengine/matrix4x4/decomposeprojection.md) | This property takes a projection matrix and returns the six plane coordinates that define a projection frustum. |
| [determinant](/engine/6000.7/script-reference/unityengine/matrix4x4/determinant.md)                 | The determinant of the matrix. (Read Only)                                                                      |
| [inverse](/engine/6000.7/script-reference/unityengine/matrix4x4/inverse.md)                         | The inverse of this matrix. (Read Only)                                                                         |
| [isIdentity](/engine/6000.7/script-reference/unityengine/matrix4x4/isidentity.md)                   | Checks whether this is an identity matrix. (Read Only)                                                          |
| [this\[\]](/engine/6000.7/script-reference/unityengine/matrix4x4/item.md)                           | Access element at sequential index (0..15 inclusive).                                                           |
| [lossyScale](/engine/6000.7/script-reference/unityengine/matrix4x4/lossyscale.md)                   | Attempts to get a scale value from the matrix. (Read Only)                                                      |
| [rotation](/engine/6000.7/script-reference/unityengine/matrix4x4/rotation.md)                       | Attempts to get a rotation quaternion from this matrix.                                                         |
| [transpose](/engine/6000.7/script-reference/unityengine/matrix4x4/transpose.md)                     | Returns the transpose of this matrix (Read Only).                                                               |

## Static Properties

| Property                                                                      | Description                                                 |
| ----------------------------------------------------------------------------- | ----------------------------------------------------------- |
| [identity](/engine/6000.7/script-reference/unityengine/matrix4x4/identity.md) | Returns the identity matrix (Read Only).                    |
| [zero](/engine/6000.7/script-reference/unityengine/matrix4x4/zero.md)         | Returns a matrix with all elements set to zero (Read Only). |

## Methods

| Method                                                                                        | Description                                                     |
| --------------------------------------------------------------------------------------------- | --------------------------------------------------------------- |
| [GetColumn](/engine/6000.7/script-reference/unityengine/matrix4x4/getcolumn.md)               | Get a column of the matrix.                                     |
| [GetPosition](/engine/6000.7/script-reference/unityengine/matrix4x4/getposition.md)           | Get position vector from the matrix.                            |
| [GetRow](/engine/6000.7/script-reference/unityengine/matrix4x4/getrow.md)                     | Returns a row of the matrix.                                    |
| [MultiplyPoint](/engine/6000.7/script-reference/unityengine/matrix4x4/multiplypoint.md)       | Transforms a position by this matrix (generic).                 |
| [MultiplyPoint3x4](/engine/6000.7/script-reference/unityengine/matrix4x4/multiplypoint3x4.md) | Transforms a position by this matrix (fast).                    |
| [MultiplyVector](/engine/6000.7/script-reference/unityengine/matrix4x4/multiplyvector.md)     | Transforms a direction by this matrix.                          |
| [SetColumn](/engine/6000.7/script-reference/unityengine/matrix4x4/setcolumn.md)               | Sets a column of the matrix.                                    |
| [SetRow](/engine/6000.7/script-reference/unityengine/matrix4x4/setrow.md)                     | Sets a row of the matrix.                                       |
| [SetTRS](/engine/6000.7/script-reference/unityengine/matrix4x4/settrs.md)                     | Sets this matrix to a translation, rotation and scaling matrix. |
| [ToString](/engine/6000.7/script-reference/unityengine/matrix4x4/tostring.md)                 | Returns a formatted string for this matrix.                     |
| [TransformPlane](/engine/6000.7/script-reference/unityengine/matrix4x4/transformplane.md)     | Returns a plane that is transformed in space.                   |
| [ValidTRS](/engine/6000.7/script-reference/unityengine/matrix4x4/validtrs.md)                 | Checks if this matrix is a valid transform matrix.              |

## Static Methods

| Method                                                                                      | Description                                                                                                                          |
| ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| [Frustum](/engine/6000.7/script-reference/unityengine/matrix4x4/frustum.md)                 | This function returns a projection matrix with viewing frustum that has a near plane defined by the coordinates that were passed in. |
| [Inverse3DAffine](/engine/6000.7/script-reference/unityengine/matrix4x4/inverse3daffine.md) | Computes the inverse of a 3D affine matrix.                                                                                          |
| [LookAt](/engine/6000.7/script-reference/unityengine/matrix4x4/lookat.md)                   | Create a "look at" matrix.                                                                                                           |
| [Ortho](/engine/6000.7/script-reference/unityengine/matrix4x4/ortho.md)                     | Create an orthogonal projection matrix.                                                                                              |
| [Perspective](/engine/6000.7/script-reference/unityengine/matrix4x4/perspective.md)         | Create a perspective projection matrix.                                                                                              |
| [Rotate](/engine/6000.7/script-reference/unityengine/matrix4x4/rotate.md)                   | Creates a rotation matrix.                                                                                                           |
| [Scale](/engine/6000.7/script-reference/unityengine/matrix4x4/scale.md)                     | Creates a scaling matrix.                                                                                                            |
| [Translate](/engine/6000.7/script-reference/unityengine/matrix4x4/translate.md)             | Creates a translation matrix.                                                                                                        |
| [TRS](/engine/6000.7/script-reference/unityengine/matrix4x4/trs.md)                         | Creates a translation, rotation and scaling matrix.                                                                                  |

## Operators

| Operator                                                                            | Description                                                                                                                                                      |
| ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Matrix4x4](/engine/6000.7/script-reference/unityengine/matrix4x4/op-implicit.md)   | Converts a [float4x4](/engine/6000.7/script-reference/unity/mathematics/float4x4.md) to a [Matrix4x4](/engine/6000.7/script-reference/unityengine/matrix4x4.md). |
| [float4x4](/engine/6000.7/script-reference/unityengine/matrix4x4/op-implicit.md)    | Converts a [Matrix4x4](/engine/6000.7/script-reference/unityengine/matrix4x4.md) to a [float4x4](/engine/6000.7/script-reference/unity/mathematics/float4x4.md). |
| [operator \*](/engine/6000.7/script-reference/unityengine/matrix4x4/op-multiply.md) | Multiplies two matrices.                                                                                                                                         |
