Vector2 GetTouchInput()
{
//Check if Input has registered more than zero touches
if (Input.touchCount <= 0)
{
return Vector2.zero;
}
int horizontal = 0;
int vertical = 0;
Touch firstTouch = Input.touches[0];
//Check if the phase of that touch equals Began
if (firstTouch.phase == TouchPhase.Began)
{
//If so, store position of that touch for later calculations
touchOrigin = firstTouch.position;
}
else if (firstTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
{
Vector2 touchEnd = firstTouch.position;
float x = touchEnd.x - touchOrigin.x;
float y = touchEnd.y - touchOrigin.y;
// Set touchOrigin.x to -1 so that our else if statement will
// evaluate false and not repeat immediately.
touchOrigin.x = -1;
// Check if the difference along the x axis is greater than the
// difference along the y axis.
if (Mathf.Abs(x) > Mathf.Abs(y))
{
horizontal = x > 0 ? 1 : -1;
}
else
{
vertical = y > 0 ? 1 : -1;
}
}
return new Vector2(horizontal, vertical);
}