Rotate Gui Texture by Touch

I have got a script to rotate a gui texture with a certain angle, but I’m not able to find a rotate by touch algorithm.

Something like the one in this game: the wheel rotates by touch.

Any ideas?

Here is my take on the problem. I did not know GUI.DrawTexture()s could be rotated. The core logic was borrowed from this here from code that was posted by @ben pitt. This code needs to attached to an empty game object.

``````using UnityEngine;
[ExecuteInEditMode()]
public class RotatableGuiItem : MonoBehaviour {
public Texture2D texture = null;
public Vector2 size = new Vector2(128, 128);

private float angle = 0;
private Vector2 pos = new Vector2(0, 0);
private Rect rect;
private Vector2 pivot;
private bool rotating = false;
private float initialMouseAngle;

void Start() {
}

pos = new Vector2(transform.localPosition.x, transform.localPosition.y);
rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y);
Debug.Log ("Rect="+rect);
pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
}

void OnGUI() {
Vector2 guiMouse = Input.mousePosition;
guiMouse.y = Screen.height - guiMouse.y;
if (Input.GetMouseButtonDown(0) && rect.Contains(guiMouse)) {
Vector2 v2T = ((Vector2)guiMouse - pivot);
initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
rotating = true;
}
else if (Input.GetMouseButton(0) & rotating) {
Vector2 v2T = ((Vector2)guiMouse - pivot);
angle = (Mathf.Atan2 (v2T.y, v2T.x) - initialMouseAngle)  * Mathf.Rad2Deg;
}
else if (Input.GetMouseButtonUp(0)) {
rotating = false;
}
Matrix4x4 matrixBackup = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, pivot);
GUI.DrawTexture(rect, texture);
GUI.matrix = matrixBackup;
}
}
``````

It’s working, but the angles values are strange, is there any way to limit it to 360 and -360 ?