Skip to content

Instantly share code, notes, and snippets.

@cuibonobo
Created June 30, 2015 21:27
Show Gist options
  • Select an option

  • Save cuibonobo/09709c38b218e1f3fd0d to your computer and use it in GitHub Desktop.

Select an option

Save cuibonobo/09709c38b218e1f3fd0d to your computer and use it in GitHub Desktop.
Unity Swipe Control
Vector3 fp;
Vector2 lp;
float dragDistance = Screen.height * 0.2f;
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Ended){
lp = touch.position;
float xDistance = Mathf.Abs(lp.x - fp.x);
float yDistance = Mathf.Abs(lp.y - fp.y);
if (xDistance > dragDistance || yDistance > dragDistance){
// Check if the horizontal movement is greater than the vertical movement
if (xDistance > yDistance){
if (lp.x > fp.x) { //Right swipe
isWalking = true;
direction = "right";
Debug.Log("Right Swipe");
} else { //Left swipe
isWalking = true;
direction = "left";
Debug.Log("Left Swipe");
}
} else {
if (lp.y > fp.y) { //Up swipe
isWalking = true;
direction = "up";
Debug.Log("Up Swipe");
} else { //Down swipe
isWalking = true;
direction = "down";
Debug.Log("Down Swipe");
}
}
} else {
// That was a tap!
}
}
// Just do the one touch for now...
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment