GUI.Matrix and setting center of rotation

Discussion in 'Immediate Mode GUI (IMGUI)' started by Predster, Mar 12, 2008.

1. Predster

Joined:
Sep 11, 2007
Posts:
145
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!

I though of perhaps using GUIUtility.RotateAroundPivot, but after reading this thread:
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):

Code (csharp):
1. var rotationradar : Texture2D; //the radar image
2. var target : Transform;   //assign main camera to this variable
3.
4.
5.
6. function OnGUI () {
7.   var tOffset = Vector3.zero;   //transform
8.   var tScale = Vector3.one;     //Scale
9.   var newRotation = target.rotation.eulerAngles.z;   //rotation variable
10.   GUI.matrix =  Matrix4x4.TRS(tOffset, Quaternion.Euler(0, 0, newRotation), tScale);
11.
12.   GUI.Label(Rect(10, 10, 320, 240), rotationradar);  //draw Gui element
13. }
14.
15. function Update () {
16. transform.rotation.eulerAngles.z = target.rotation.eulerAngles.z;   //get the rotation on the z-axis
17. }
Can anyone help out? Thanks.

2. Brian-Kehrer

Joined:
Nov 7, 2006
Posts:
411
this is a little thing i made

Code (csharp):
1.
2. var difference = (((angleBetween + Mathf.PI) * Mathf.Rad2Deg) - CameraTransform.eulerAngles.y);
3. //this is an angle of rotation
4.
5. var tempv2 = Vector2(Screen.width/2, Screen.height - 40);
6. //this is pivot point
7.
8.     GUIUtility.RotateAroundPivot(difference,tempv2);
9.     GUI.Label(Rect(tempv2.x-15, tempv2.y-15,30,30),CompassMark);
10. //the -15 makes sure the pivot is centered since its 30,30
11.

Joined:
Nov 7, 2005
Posts:
4,770
4. Predster

Joined:
Sep 11, 2007
Posts:
145
Awesome! It appears to be shipped. But I'm not sure exactly how to use it (from the docs).

Code (csharp):
1. 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?

5. Brian-Kehrer

Joined:
Nov 7, 2006
Posts:
411
see my previous example

or here, clarified

Code (csharp):
1.
2. var theAngle : float = 45.0;
3. //this is an angle of rotation
4.
5. var thePoint : Vector2 = Vector2(100,100);
6. //this is pivot point
7.
8. GUIUtility.RotateAroundPivot(theAngle,thePoint);
9. //i think everything below this gets rotated
10.
11.    GUI.Label(Rect(thePoint.x-15, thePoint.y-15,30,30),"hello");
12. //the -15 makes sure the pivot is centered since its 30,30
13.
to match camera rotation, hmm - a quick way
Code (csharp):
1.
2. var theCamera : Camera;
3. function Update(){
4. var theAngle = theCamera.transform.eulerAngles.y;
5. }
6.

6. Predster

Joined:
Sep 11, 2007
Posts:
145
Thanks Brian! But this code gives me an "Unknown Identifier" error for "theAngle"

to match camera rotation, hmm - a quick way
Code (csharp):
1.
2. var theCamera : Camera;
3. function Update(){
4. var theAngle = theCamera.transform.eulerAngles.y;
5. }
6.
[/quote]

Does it need to be defined elsewhere, for instance

Code (csharp):
1. var theAngle : float = 45.0;
2. //this is an angle of rotation
As in the first example?

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

7. Brian-Kehrer

Joined:
Nov 7, 2006
Posts:
411
yeah, that was just psuedo code. Make it a public variable or something

Code (csharp):
1.
2. var theAngle : float = 0.0;
3.
4. function Update(){
5. theAngle = .....
6.
7. }
8.

8. Predster

Joined:
Sep 11, 2007
Posts:
145
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

Code (csharp):
1.
2. var rotationradar : Texture2D; //the radar image
3. var target : Transform;   //assign main camera to this variable
4.
5. function OnGUI () {
6.   var tOffset = Vector3.zero;   //transform
7.   var tScale = Vector3.one;     //Scale
8.   var newRotation = target.rotation.eulerAngles.z;   //rotation variable
9.   var thePoint : Vector2 = Vector2(210,170);
10.   var theAngle : float = 360;
11.   GUI.matrix =  Matrix4x4.TRS(tOffset, Quaternion.Euler(0, 0, newRotation), tScale);
12.   GUIUtility.RotateAroundPivot (theAngle, thePoint);
13.
14.   GUI.Label(Rect(50,50, 320, 240), rotationradar);  //draw Gui element
15. }
16.
17. function Update () {
18. transform.rotation.eulerAngles.z = target.rotation.eulerAngles.z;   //get the rotation on the z-axis
19. }
20.
21.

9. Brian-Kehrer

Joined:
Nov 7, 2006
Posts:
411
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.

Code (csharp):
1. var rotationradar : Texture2D; //the radar image
2. var target : Transform;   //assign main camera to this variable
3. var theAngle : float = 0.0;
4. var thePoint : Vector2 = Vector2.zero;
5.
6. function OnGUI () {
7.
8.
9.   GUIUtility.RotateAroundPivot (theAngle, thePoint);
10.
11.   GUI.Label(Rect(50,50, 320, 240), rotationradar);  //draw Gui element
12. }
13.
14. function Update () {
15.
16.   thePoint = Vector2(210,170);
17.   theAngle = target.rotation.eulerAngles.z;
18.
19. }

10. Predster

Joined:
Sep 11, 2007
Posts:
145
Brian, thanks!! It works!!

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.