Search Unity

Swipe not working

Discussion in 'Scripting' started by Yash987654321, Aug 29, 2015.

  1. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    I am remaking my TouchScript making it faster and smarter but the swipe would only work Up or Right. It would show none if it needs to go Down or Left. It only gives Left/Right if both MoveX and MoveY are greater than SwipeSensivity, that too inverted like when it should give me Left it gives me down and vice versa.
    Heres my code:
    Code (CSharp):
    1. public enum SwipeTypes{None,Up,Down,Left,Right}
    2.  
    3. public class TouchScript
    4. {
    5.  
    6.     [Tooltip("This mouse button will be used for taking input from the user")]
    7.     public int MainMouseButton = 0;
    8.     [Tooltip("This will be the touch index the script will use as Main Touch")]
    9.     public int MainTouchIndex = 0;
    10.     public float SwipeDistance = 50.0f;
    11.  
    12. void MouseSwipe()
    13.     {
    14.         if(Input.GetMouseButtonDown(MainMouseButton))
    15.         {
    16.             BeginPosition = Input.mousePosition;
    17.         }
    18.         else if(Input.GetMouseButtonUp(MainMouseButton))
    19.         {
    20.             EndPosition = Input.mousePosition;
    21.             Swipe = DetectSwipe();
    22.         }
    23.     }
    24.  
    25.     SwipeTypes DetectSwipe()
    26.     {
    27.         float MoveX = EndPosition.x-BeginPosition.x;
    28.         float MoveY = EndPosition.y-BeginPosition.y;
    29.         Debug.Log(MoveX,MoveY);
    30.  
    31.         if(MoveY >= MoveX)
    32.         {
    33.             if(MoveY < -SwipeDistance)
    34.                 return SwipeTypes.Down;
    35.             else if(MoveY > SwipeDistance)
    36.                 return SwipeTypes.Up;
    37.             else
    38.                 return SwipeTypes.None;
    39.         }
    40.         else
    41.         {
    42.             if(MoveX < -SwipeDistance)
    43.                 return SwipeTypes.Left;
    44.             else if(MoveX > SwipeDistance)
    45.                 return SwipeTypes.Right;
    46.             else
    47.                 return SwipeTypes.None;
    48.         }
    49. }
    Thanks :D
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You should change:
    Code (csharp):
    1.  
    2. if(MoveY >= MoveX)
    3.  
    to:
    Code (csharp):
    1.  
    2. if(Mathf.Abs(MoveY) >= Mathf.Abs(MoveX))
    3.  
    to check against absolutes.

    Right now, I could move 400 pixels to the left, and 1 pixel down, giving me a MoveY of 1 and a MoveX of -400, but you consider the MoveY a more significant move because 1 > -400.
     
    Yash987654321 likes this.
  3. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Hey thanks it worked...... I need to learn more Maths :(