- Home /
GUI rotation not working
I am trying to rotate a compass in the top corner of my screen, and I have writen a script for it, however incorrect it maybe. It is drawing eveything correctly and there are no syntax errors, but instead of rotating around the centerpoint of the compass, the compass is just moving up and down (based on my direction, the max down it goes corresponds to when I am turning right and all the way around, and the max up it goes corresponds to when I am turning left and all the way around: the the center y position seems to be right when I start, so 0 degrees). Why is this? Here is my script:
var camera1 : Camera; var camera2 : Camera; var camera3 : Camera; var openButton : Texture2D; var closeButton : Texture2D; var mapMarker : Texture2D; var compass : Texture2D; var mapSkin : GUISkin; var characterTransform : Transform; var screenPos : Vector3; var playerDirection : float; var pivotPoint = Vector2(66.5,386.5);
function Update () { if (camera2.enabled) { screenPos = camera.WorldToScreenPoint (characterTransform.position); } }
function OnGUI() { GUI.skin = mapSkin; if (camera1.enabled) { playerDirection = characterTransform.transform.rotation.y; GUIUtility.RotateAroundPivot(playerDirection,pivotPoint);
GUI.DrawTexture(Rect(1145,3,290,290),compass); GUI.DrawTexture(Rect(1275,133,30,30),mapMarker); if (GUI.Button(new Rect(1115,270,50,50),openButton)) { camera1.enabled = false; camera2.enabled = true; camera3.enabled = false; } } if (camera2.enabled) { GUI.DrawTexture(Rect(screenPos.x - 12,730 - screenPos.y,20,20),mapMarker); if (GUI.Button(new Rect(1250,100,100,100),closeButton)) { camera1.enabled = true; camera2.enabled = false; camera3.enabled = true; } } }
function Start () { camera2.enabled = false; }
Answer by yoyo · Jan 22, 2011 at 05:35 AM
Your pivot point is a long way away from the rectangle in which you're drawing the compass. You need to set the pivot to the center of the rectangle, something like (1290, 148).
Thanks. Also, it turns out that with a transform variable, transform.rotation uses Quaternion ins$$anonymous$$d of degrees, so I needed to use transform.eulerAngles. but my pivot point was way off too, and yours was perfectly in the center of my graphic. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613100520im_/https://answers.unity.com/themes/thub/images/avi.jpg)