- Home /
compass GUI jumps at angle 180.
Hello Everyone,
I have a script that I use for a compass. The problem is, that once the angle reaches close to 180, it jumps down to 0 or a negative value. so the compass only points correctly on one half of the screen.
Would someone be kind enough to take a look and point out where I went wrong with this?
Thank you very much!
var tex : Texture2D; // Texture to be rotated
var goFollow :GameObject; // GameObject to point to
var width :float;
var height : float;
var angle: float = 0;
var pos : Vector2 = new Vector2(0, 0);
var rect : Rect;
var pivot : Vector2;
function Start()
{
if (tex == null)
{
Debug.Log ("Texture or game object to follow is null");
}
pos = new Vector2(transform.localPosition.x, transform.localPosition.y);
rect = new Rect(Screen.width/width, Screen.height/height, 50, 100);
pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
}
function OnGUI()
{
var guiPos : Vector2 = Camera.main.WorldToScreenPoint(goFollow.transform.position);
guiPos.y = Screen.height - guiPos.y;
var v2T : Vector2 = guiPos - pivot;
angle = (Mathf.Atan2 (v2T.y, v2T.x)) * Mathf.Rad2Deg;
var matrixBackup : Matrix4x4 = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, pivot);
GUI.DrawTexture(rect, tex);
GUI.matrix = matrixBackup;
}
Comment
Your answer
Follow this Question
Related Questions
[2D] Get 0-359 compass angle player <--> object 1 Answer
How to automate unity editor screen grabs 0 Answers
Rotate object in a set amount of time 1 Answer
Limit Object Angle 2 Answers
By-pass the 'shortest route' aspect of Quaternion.Slerp 1 Answer