Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. void Start(){
  2. dragDistance = Screen.height*20/100; //dragDistance is 20% height of the screen
  3. }
  4.  
  5. void Update(){
  6.  
  7. foreach (Touch touch in Input.touches) //use loop to detect more than one swipe
  8. { //can be ommitted if you are using lists
  9. if (touch.phase == TouchPhase.Began) //check for the first touch
  10. {
  11. fp = touch.position;
  12. lp = touch.position;
  13.  
  14. }
  15.  
  16. if (touch.phase == TouchPhase.Moved) //add the touches to list as the swipe is being made
  17. {
  18. touchPositions.Add(touch.position);
  19. }
  20.  
  21. if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
  22. {
  23. //lp = touch.position; //last touch position. Ommitted if you use list
  24. fp = touchPositions[0]; //get first touch position from the list of touches
  25. lp = touchPositions[touchPositions.Count-1]; //last touch position
  26.  
  27. //Check if drag distance is greater than 20% of the screen height
  28. if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
  29. {//It's a drag
  30. //check if the drag is vertical or horizontal
  31. if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
  32. { //If the horizontal movement is greater than the vertical movement...
  33. if ((lp.x>fp.x)) //If the movement was to the right)
  34. { //Right swipe
  35. Debug.Log("Right Swipe");
  36. }
  37. else
  38. { //Left swipe
  39. Debug.Log("Left Swipe");
  40. }
  41. }
  42. else
  43. { //the vertical movement is greater than the horizontal movement
  44. if (lp.y>fp.y) //If the movement was up
  45. { //Up swipe
  46. Debug.Log("Up Swipe");
  47. }
  48. else
  49. { //Down swipe
  50. Debug.Log("Down Swipe");
  51. }
  52. }
  53. }
  54. }
  55. else
  56. { //It's a tap as the drag distance is less than 20% of the screen height
  57.  
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement