Version: 2017.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Gizmos.matrix

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static var matrix: Matrix4x4;
public static Matrix4x4 matrix;

Description

Sets the Matrix4x4 that the Unity Editor uses to draw Gizmos.

The Gizmos.matrix stores the position, rotation and scale of the Gizmos. By default, Gizmos always uses world coordinates. The default Gizmos.matrix transforms the world coordinates using a default identity matrix. Transform.localToWorldMatrix changes local coordinate space to world space.

GameObjects often use local coordinates. Gizmos.matrix changes these local coordinates into world coordinates to allow Gizmos to use them. For example, a rotating object uses local coordinates. A transfer into world coordinates happens using Gizmos.matrix. To visualise the object, use Gizmos.DrawCube. See below.

To use the example to draw a red, semi-transparent, cube gizmo:
1. Place this example script on a Cylinder at the origin.
2. Select the Cylinder in the Hierarchy and then click the Play button.
3. Next, click the Scene button. The gizmo should appear.
The cylinder will rotate in Play mode and be seen rotating in Scene view.

#pragma strict
// Gizmos.matrix example
public class Example extends MonoBehaviour {
	public var rotationSpeed: float = 50.0f;
	function OnDrawGizmosSelected() {
		Gizmos.color = new Color(0.75f, 0.0f, 0.0f, 0.75f);
		// coordinates for the matrix transformation.
		Gizmos.matrix = transform.localToWorldMatrix;
		Gizmos.DrawCube(Vector3.zero, Vector3.one);
	}
	// Rotate the cylinder.
	function Update() {
		var zRot: float = rotationSpeed * Time.deltaTime;
		transform.Rotate(0.0f, 0.0f, zRot);
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Gizmos.matrix example

public class Example : MonoBehaviour { // Allow the speed of rotation to be changed. public float rotationSpeed = 50.0f;

void OnDrawGizmosSelected() { Gizmos.color = new Color(0.75f, 0.0f, 0.0f, 0.75f);

// Convert the local coordinate values into world // coordinates for the matrix transformation. Gizmos.matrix = transform.localToWorldMatrix; Gizmos.DrawCube(Vector3.zero, Vector3.one); }

// Rotate the cylinder. void Update() { float zRot = rotationSpeed * Time.deltaTime; transform.Rotate(0.0f, 0.0f, zRot); } }