I have this code that rotates a Steering Wheel on the screen. It works very well, but I want to limit it to 360 and -360.
public var texture : Texture2D = null;
public var size : Vector2 = new Vector2(128, 128);
private var angle : float = 0;
private var pos : Vector2 = new Vector2(0, 0);
private var rect : Rect;
private var pivot : Vector2;
private var rotating : boolean = false;
private var initialMouseAngle : float;
function Start() {
UpdateSettings();
}
function UpdateSettings() {
pos = new Vector2(transform.localPosition.x, Screen.height - 110);
rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y);
pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
}
function OnGUI() {
if (Application.isEditor) { UpdateSettings(); }
if(Input.touchCount > 0) {
var touch : Touch = Input.GetTouch(0);
var guiMouse : Vector2 = Vector2(touch.position.x, touch.position.y);
guiMouse.y = Screen.height - guiMouse.y;
if ((touch.phase == TouchPhase.Began) rect.Contains(guiMouse)) {
var v2T : Vector2 = (guiMouse - pivot);
initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
rotating = true;
}
else if ((touch.phase == TouchPhase.Moved) rotating) {
var v2T2 : Vector2 = (guiMouse - pivot);
angle = (Mathf.Atan2 (v2T2.y, v2T2.x) - initialMouseAngle) * Mathf.Rad2Deg;
if(angle < 0)
angle += 360;
Debug.Log(angle);
}
else if ((touch.phase == TouchPhase.Ended)) {
rotating = false;
angle = 0;
}
}
var matrixBackup : Matrix4x4 = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, pivot);
GUI.DrawTexture(rect, texture);
GUI.matrix = matrixBackup;
}
The problem is that the angles while turning are always like this:
