Created
April 5, 2021 21:16
-
-
Save Korkmatik/7f5cceab715ebc882ee3e9b9cdf59bc6 to your computer and use it in GitHub Desktop.
Get direction in Unity from two touches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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