Vector2 curPos;
Vector2 lastPos;
Vector2 centerInScreen;
void Update()
{
curPos = eventData.position;
centerInScreen = RectTransformUtility.WorldToScreenPoint(null, transform.position);
Debug.Log("centerInScreen: " + centerInScreen);
float deltaAngle = GetDeltaRadians();
Debug.Log("deltaAngle: " + deltaAngle);
RotateUI(deltaAngle * Mathf.Rad2Deg);
lastPos = curPos;
}
public float GetDeltaRadians()
{
var a = GetLastRadians(centerInScreen);
var b = GetRadians(centerInScreen);
var d = Mathf.Repeat(a - b, Mathf.PI * 2.0f);
Debug.Log("a: " + a + " b: " + b + " d: " + d);
if (d > Mathf.PI)
{
d -= Mathf.PI * 2.0f;
}
return d;
}
public float GetLastRadians(Vector2 pivot)
{
return Mathf.Atan2(lastPos.x - pivot.x, lastPos.y - pivot.y);
}
/// <summary>This will return the angle between the finger and the reference point, relative to the screen.</summary>
public float GetRadians(Vector2 pivot)
{
return Mathf.Atan2(curPos.x - pivot.x, curPos.y - pivot.y);
}