SwipeControlsCS.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * FUNCTION: Detects and controls the swipes during gameplay.
  3. *
  4. * USED BY: This script is a part of the "Player" prefab.
  5. *
  6. * INFO: Call the getSwipeDirection() function in the Update() function of your script responsible for character control
  7. *
  8. */
  9. using UnityEngine;
  10. using System.Collections;
  11. public class SwipeControlsCS : MonoBehaviour {
  12. public enum SwipeDirection
  13. {
  14. Null = 0,//no swipe detected
  15. Duck = 1,//swipe down detected
  16. Jump = 2,//swipe up detected
  17. Right = 3,//swipe right detected
  18. Left = 4//swipe left detected
  19. }
  20. //Constants
  21. private float fSensitivity = 15;
  22. //VARIABLES
  23. //distance calculation
  24. private SwipeDirection sSwipeDirection; //string to receive swipe output (Debug Only)
  25. private float fInitialX;
  26. private float fInitialY;
  27. private float fFinalX;
  28. private float fFinalY;
  29. private int iTouchStateFlag; //flag to check
  30. private float inputX; //x-coordinate
  31. private float inputY; //y-coordinate
  32. private float slope; //slope (m) of the the
  33. private float fDistance; //magnitude of distance between two positions
  34. // Use this for initialization
  35. void Start ()
  36. {
  37. fInitialX = 0.0f;
  38. fInitialY = 0.0f;
  39. fFinalX = 0.0f;
  40. fFinalY = 0.0f;
  41. inputX = 0.0f;
  42. inputY = 0.0f;
  43. iTouchStateFlag = 0;
  44. sSwipeDirection = SwipeDirection.Null;
  45. }
  46. void Update()
  47. {
  48. if (iTouchStateFlag == 0 && Input.GetMouseButtonDown(0)) //state 1 of swipe control
  49. {
  50. fInitialX = Input.mousePosition.x; //get the initial x mouse/ finger value
  51. fInitialY = Input.mousePosition.y; //get the initial y mouse/ finger value
  52. sSwipeDirection = SwipeDirection.Null;
  53. iTouchStateFlag = 1;
  54. }
  55. if (iTouchStateFlag == 1) //state 2 of swipe control
  56. {
  57. fFinalX = Input.mousePosition.x;
  58. fFinalY = Input.mousePosition.y;
  59. sSwipeDirection = swipeDirection(); //get the swipe direction
  60. if (sSwipeDirection != SwipeDirection.Null)
  61. iTouchStateFlag = 2;
  62. }//end of state 1
  63. if (iTouchStateFlag == 2 || Input.GetMouseButtonUp(0)) //state 3 of swipe control
  64. {
  65. //sSwipeDirection = SwipeDirection.Null;
  66. iTouchStateFlag = 0;
  67. }//end of M.R. swipe control
  68. }
  69. /*
  70. * FUNCTION: Calculate the swipe direction
  71. */
  72. private SwipeDirection swipeDirection()
  73. {
  74. //calculate the slope of the swipe
  75. inputX = fFinalX - fInitialX;
  76. inputY = fFinalY - fInitialY;
  77. slope = inputY / inputX;
  78. //calculate the distance of tap start and end
  79. fDistance = Mathf.Sqrt( Mathf.Pow((fFinalY-fInitialY), 2) + Mathf.Pow((fFinalX-fInitialX), 2) );
  80. if (fDistance <= (Screen.width/fSensitivity))//higher the dividing factor higher the sensitivity
  81. return SwipeDirection.Null;
  82. if (inputX >= 0 && inputY > 0 && slope > 1)//first octant JUMP
  83. {
  84. return SwipeDirection.Jump;
  85. }
  86. else if (inputX <= 0 && inputY > 0 && slope < -1)//eighth octant JUMP
  87. {
  88. return SwipeDirection.Jump;
  89. }
  90. else if (inputX > 0 && inputY >= 0 && slope < 1 && slope >= 0)//second octant RIGHT
  91. {
  92. return SwipeDirection.Right;
  93. }
  94. else if (inputX > 0 && inputY <= 0 && slope > -1 && slope <= 0)//third octant RIGHT
  95. {
  96. return SwipeDirection.Right;
  97. }
  98. else if (inputX < 0 && inputY >= 0 && slope > -1 && slope <= 0)//seventh octant LEFT
  99. {
  100. return SwipeDirection.Left;
  101. }
  102. else if (inputX < 0 && inputY <= 0 && slope >= 0 && slope < 1)//sixth octant LEFT
  103. {
  104. return SwipeDirection.Left;
  105. }
  106. else if (inputX >= 0 && inputY < 0 && slope < -1)//fourth octant DUCK
  107. {
  108. return SwipeDirection.Duck;
  109. }
  110. else if (inputX <= 0 && inputY < 0 && slope > 1)//fifth octant DUCK
  111. {
  112. return SwipeDirection.Duck;
  113. }//end of else if
  114. return SwipeDirection.Null;
  115. }//end of SwipeDirection function
  116. /*
  117. * FUNCTION: Return swipe direction.
  118. * RETURNS: Returns NULL if no swipes are detected.
  119. * Returns SwipeDirection if a swipe is detected
  120. */
  121. public SwipeDirection getSwipeDirection()
  122. {
  123. if (sSwipeDirection != SwipeDirection.Null)
  124. {
  125. var etempSwipeDirection = sSwipeDirection;
  126. sSwipeDirection = SwipeDirection.Null;
  127. return etempSwipeDirection;
  128. }
  129. else
  130. return SwipeDirection.Null;
  131. }
  132. }