This has been the bane of my existence for the past 2 weeks and have not been able to get it to do what I want. I have the dial turning correctly, But using the mouse or touch is not very intuitive. I can turn the dial by dragging the mouse to the left or right on the horizontal. but when I try and make circular motions it does not work.
Any help would be appreciated.
I’ve answered this question several times with different solutions for both GUI.DrawTexture() and for object in 3D space. Here is one of those links:
http://answers.unity3d.com/questions/389586/rotate-gui-texture-by-touch.html
Solution if object is rotating on the Z. Most of the credit goes to robertbu.
private Quaternion originalRotation;
private float startAngle = 0;
public void Start()
{
originalRotation = this.transform.rotation;
}
public void InputIsDown()
{
#if UNITY_IPHONE || UNITY_ANDROID
originalRotation = this.transform.rotation;
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 vector = Input.GetTouch(0).position - screenPos;
startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
//startAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg; // uncomment to pop to where mouse is
#else
originalRotation = this.transform.rotation;
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 vector = Input.mousePosition - screenPos;
startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
//startAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg; // uncomment to pop to where mouse is
#endif
}
public void InputIsHeld()
{
#if UNITY_IPHONE || UNITY_ANDROID
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 vector = Input.GetTouch(0).position - screenPos;
float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle , this.transform.forward);
newRotation.y = 0; //This and the line below, may need to be changed depending on what axis the object is rotating on.
newRotation.eulerAngles = new Vector3(0,0,newRotation.eulerAngles.z);
this.transform.rotation = originalRotation * newRotation;
#else
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 vector = Input.mousePosition - screenPos;
float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle , this.transform.forward);
newRotation.y = 0; //see comment from above
newRotation.eulerAngles = new Vector3(0,0,newRotation.eulerAngles.z);
this.transform.rotation = originalRotation * newRotation;
#endif
}