system
September 30, 2011, 7:46am
1
I’m having trouble trying to get my GUI object to rotate around its central point.
its meant to be an arrow that circles around a mini-map pointing towards the objective.
I’m using Unity 3.4.0f5
The arrow shows and is positioned correctly but it just wont rotate.
“rotAngle = GameObject.Find(“ObjectiveWatch”).GetComponent(WatchObjective).direction;” is fed by a Euler Angle.
var minimapArrow : Texture2D;
private var rotAngle : float = 0;
private var pivotPoint : Vector2;
function OnGUI() {
GUI.depth=149;
GUI.Label (Rect ((Screen.width) - 226, (Screen.height * 0.20) + 7, 202, 202), minimapArrow);
rotAngle = GameObject.Find("ObjectiveWatch").GetComponent(WatchObjective).direction;
//Debug.Log("direction = " + rotAngle);
pivotPoint = Vector2((Screen.width) - 327, (Screen.height * 0.20) + 108);
GUIUtility.RotateAroundPivot (rotAngle, pivotPoint);
}
A classic mistake, you have to draw you arrow image AFTER changing the rotation matrix of the GUI. Make sure to set the matrix to identity after the operation.
`
//do the rotation
GUIUtility.RotateAroundPivot(rotAngle, pivotPoint);
//draw image
GUI.DrawLabel(Rect(Screen.width - 226, (Screen.height * 0.20) + 7, 202, 202), minimapArrow);
//reset
GUI.matrix = Matrix4x4.identity;
`
Consider using DrawTexture() instead of Label() as it's a bit more efficient if you just want to draw a Texture2D. (You have to set the dimensions manually though as it's not keeping the aspect ratio automatically)
i find it also hard to believe that it’s not possible to rotate a gui texture.
maybe this might help: http://forum.unity3d.com/threads/36951-Rotate-A-GUI-SPrite-element-texture
system
September 30, 2011, 11:41am
3
Thank you Lee Gibson!!
thanks to you i managed to get a script that worked for rotating the minimaps arrow with ease!!
the most fiddly part is positioning the pivot point.
var minimapArrow : Texture2D;
private var rotAngle : float;
private var pivotPoint : Vector2;
function OnGUI() {
var matrixBackup:Matrix4x4 = GUI.matrix;
rotAngle = GameObject.Find("ObjectiveWatch").GetComponent(WatchObjective).direction;
pivotPoint = Vector2((Screen.width) - 125, (Screen.height * 0.20) + 100);
GUIUtility.RotateAroundPivot (rotAngle, pivotPoint);
GUI.depth=149;
var myRect:Rect = Rect((Screen.width) - 223, (Screen.height * 0.20) + 4, 202, 202);
GUI.DrawTexture (myRect, minimapArrow);
GUI.matrix = matrixBackup;
}