# GUI.Matrix and setting center of rotation

Greetings,

I’d like to draw a little rotation radar thing using UnityGUI, like the middle dial (with the blue) here: http://www.itreviews.co.uk/graphics/normal/games/g125.jpg.

Currently, I’m using GUI.Matrix to rotate the texture, but I can’t seem to figure out how to define the center of rotation!

http://forum.unity3d.com/viewtopic.php?t=8834&highlight=rotatearoundpivot

I gather it’s not shipped yet.

This is the code I am using (I want to ONLY get the rotation of the main camera on the Z-axis, and have the 2D texture rotate accordingly):

``````var rotationradar : Texture2D; //the radar image
var target : Transform;   //assign main camera to this variable

function OnGUI () {
var tOffset = Vector3.zero;   //transform
var tScale = Vector3.one;		//Scale
var newRotation = target.rotation.eulerAngles.z;   //rotation variable
GUI.matrix =  Matrix4x4.TRS(tOffset, Quaternion.Euler(0, 0, newRotation), tScale);

GUI.Label(Rect(10, 10, 320, 240), rotationradar);  //draw Gui element
}

function Update () {
transform.rotation.eulerAngles.z = target.rotation.eulerAngles.z; 	//get the rotation on the z-axis
}
``````

Can anyone help out? Thanks.

this is a little thing i made

``````var difference = (((angleBetween + Mathf.PI) * Mathf.Rad2Deg) - CameraTransform.eulerAngles.y);
//this is an angle of rotation

var tempv2 = Vector2(Screen.width/2, Screen.height - 40);
//this is pivot point

GUIUtility.RotateAroundPivot(difference,tempv2);
GUI.Label(Rect(tempv2.x-15, tempv2.y-15,30,30),CompassMark);
//the -15 makes sure the pivot is centered since its 30,30
``````

It is shipped already in 2.0.2 (release notes). If in doubt, just try it, and if it works then it’s shipped

Awesome! It appears to be shipped. But I’m not sure exactly how to use it (from the docs).

``````RotateAroundPivot (angle : float, pivotPoint : Vector2)
``````

Can I just set the pivot point as two screen coordinates e.g. ( Vector2(50,50))?
If I want it to rotate around 360 degrees (with camera rotation), can I just set it to 360 degrees?

see my previous example

or here, clarified

``````var theAngle : float = 45.0;
//this is an angle of rotation

var thePoint : Vector2 = Vector2(100,100);
//this is pivot point

GUIUtility.RotateAroundPivot(theAngle,thePoint);
//i think everything below this gets rotated

GUI.Label(Rect(thePoint.x-15, thePoint.y-15,30,30),"hello");
//the -15 makes sure the pivot is centered since its 30,30
``````

to match camera rotation, hmm - a quick way

``````var theCamera : Camera;
function Update(){
var theAngle = theCamera.transform.eulerAngles.y;
}
``````

Thanks Brian! But this code gives me an “Unknown Identifier” error for “theAngle”

to match camera rotation, hmm - a quick way

``````var theCamera : Camera;
function Update(){
var theAngle = theCamera.transform.eulerAngles.y;
}
``````

[/quote]

Does it need to be defined elsewhere, for instance

``````var theAngle : float = 45.0;
//this is an angle of rotation
``````

As in the first example?

Edit: Whoops I completely missed your first post, also thanks!

yeah, that was just psuedo code. Make it a public variable or something

``````var theAngle : float = 0.0;

function Update(){
theAngle = .....

}
``````

Ok, I see

Basically, I’ve been trying to set that variable to a variety of constants between 45 and 360 degrees and it hasn’t changed anything. Also, changing the Vector2 values hasn’t changed anything either.

The script I have doesn’t seem to have any errors, but also doesn’t change the center of rotation at all :x

``````var rotationradar : Texture2D; //the radar image
var target : Transform;   //assign main camera to this variable

function OnGUI () {
var tOffset = Vector3.zero;   //transform
var tScale = Vector3.one;		//Scale
var newRotation = target.rotation.eulerAngles.z;   //rotation variable
var thePoint : Vector2 = Vector2(210,170);
var theAngle : float = 360;
GUI.matrix =  Matrix4x4.TRS(tOffset, Quaternion.Euler(0, 0, newRotation), tScale);
GUIUtility.RotateAroundPivot (theAngle, thePoint);

GUI.Label(Rect(50,50, 320, 240), rotationradar);  //draw Gui element
}

function Update () {
transform.rotation.eulerAngles.z = target.rotation.eulerAngles.z; 	//get the rotation on the z-axis
}
``````

try this
EDIT: make sure your rotation is in the z axis, usually for fps it’s in the y axis – also, i just tested this, and it works.

``````var rotationradar : Texture2D; //the radar image
var target : Transform;   //assign main camera to this variable
var theAngle : float = 0.0;
var thePoint : Vector2 = Vector2.zero;

function OnGUI () {

GUIUtility.RotateAroundPivot (theAngle, thePoint);

GUI.Label(Rect(50,50, 320, 240), rotationradar);  //draw Gui element
}

function Update () {

thePoint = Vector2(210,170);
theAngle = target.rotation.eulerAngles.z;

}
``````

Brian, thanks!! It works!! :lol: :lol: :lol:

I do need it for the z-axis–I’ve added a script to the main camera for “barrel roll” type rotation, so I wanted a gauge to show the rotation.