Skip to content

Instantly share code, notes, and snippets.

@Korkmatik
Created April 5, 2021 21:16
Show Gist options
  • Save Korkmatik/7f5cceab715ebc882ee3e9b9cdf59bc6 to your computer and use it in GitHub Desktop.
Save Korkmatik/7f5cceab715ebc882ee3e9b9cdf59bc6 to your computer and use it in GitHub Desktop.
Get direction in Unity from two touches
Vector2 touchOrigin = -Vector2.one;
if (Input.touchCount > 0)
{
Touch myTouch = Input.touches[0];
if (myTouch.phase == TouchPhase.Began)
{
touchOrigin = myTouch.position;
}
else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
{
Vector2 touchEnd = myTouch.position;
float x = touchEnd.x - touchOrigin.x;
float y = touchEnd.y - touchOrigin.y;
touchOrigin.x = -1;
if (Mathf.Abs(x) > Mathf.Abs(y))
horizontal = x > 0 ? 1 : -1;
else
vertical = y > 0 ? 1 : -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment