Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Swipe in all directions Touch and Mouse

Discussion in 'Scripting' started by Gustav_Bok, Jan 8, 2013.

  1. alevy97

    alevy97

    Joined:
    Sep 3, 2018
    Posts:
    2
    Still helpful in 2019. Much appreciated Gustav_Bok!
     
  2. Llits

    Llits

    Joined:
    Feb 8, 2017
    Posts:
    6
    I'm trying to get the first 4 directional swipes script to work, but when I build the solution, it says there's closing brackets or semicolons expected in places where they shouldnts be. But all my brackets are closed and the semicolons are fine. Has anyone else experienced this?
     
  3. seanDp32

    seanDp32

    Joined:
    May 24, 2016
    Posts:
    1
    Hi All,

    I was feeling lazy to write my own script so I used the script on this page and it works fine for me here is what I have so far
    Code (CSharp):
    1.  
    2.  
    3.     public static Swipe swipeDirection;
    4.  
    5.     public float minSwipeLength = 200f;
    6.     Vector2 firstPressPos;
    7.     Vector2 secondPressPos;
    8.     Vector2 currentSwipe;public enum Swipe { None, Up, Down, Left, Right };
    9.  
    10.     public static Swipe swipeDirection;
    11.  
    12.     public float minSwipeLength = 200f;
    13.     Vector2 firstPressPos;
    14.     Vector2 secondPressPos;
    15.     Vector2 currentSwipe;
    16.  
    17.  
    18.  
    19.  
    20. void update (){
    21.  
    22. DetectSwipe();
    23. }
    24.  
    25.  
    26. public void DetectSwipe()
    27.     {
    28.  
    29.  
    30.  
    31.         if (Input.touches.Length > 0)
    32.         {
    33.             Touch t = Input.GetTouch(0);
    34.  
    35.             if (t.phase == TouchPhase.Began)
    36.             {
    37.                 firstPressPos = new Vector2(t.position.x, t.position.y);
    38.             }
    39.  
    40.             if (t.phase == TouchPhase.Ended)
    41.             {
    42.                 secondPressPos = new Vector2(t.position.x, t.position.y);
    43.                 currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
    44.  
    45.                 // Make sure it was a legit swipe, not a tap
    46.                 if (currentSwipe.magnitude < minSwipeLength)
    47.                 {
    48.                     swipeDirection = Swipe.None;
    49.                     return;
    50.                 }
    51.  
    52.                 currentSwipe.Normalize();
    53.  
    54.                 if (!(secondPressPos == firstPressPos))
    55.                 {
    56.                     if (Mathf.Abs(currentSwipe.x) > Mathf.Abs(currentSwipe.y))
    57.                     {
    58.                         if (currentSwipe.x < 0)
    59.                         {
    60.                             Debug.Log("right");
    61.                         }
    62.                         else
    63.                         {
    64.                             Debug.Log("left");
    65.                         }
    66.                     }
    67.                     else
    68.                     {
    69.                         if (currentSwipe.y < 0)
    70.                         {
    71.                             Debug.Log("down");
    72.                         }
    73.                         else
    74.                         {
    75.                             Debug.Log("up");
    76.                         }
    77.                     }
    78.                 }
    79.  
    80.  
    81.  
    82.             }
    83.             else
    84.             {
    85.                 swipeDirection = Swipe.None;
    86.             }
    87.         }
    88.  
    89.     }
    90.  
    91.  
    92.  
    93.  

    I hope this help someone
     
    Last edited: Sep 1, 2019
  4. EightGreatGamesLLC

    EightGreatGamesLLC

    Joined:
    Sep 25, 2017
    Posts:
    4
    thanks its awesome
     
  5. SilverFoxStudios

    SilverFoxStudios

    Joined:
    Dec 11, 2019
    Posts:
    1
    Here is the correct version of this script. Basically there is a little bug and i fixed that.
     

    Attached Files:

    Maverickyy and MD_Reptile like this.
  6. jrpaglu

    jrpaglu

    Joined:
    Jul 24, 2018
    Posts:
    1
    @brad - keys
    Thank You ! So Much Its Work on Both Mobile and Windows...
     
  7. tonycsharks

    tonycsharks

    Joined:
    Dec 31, 2015
    Posts:
    26
    Thanks for this script. but can you please help with the 2 finger swipe and 3 finger swipe. Tried several methods but none was working properly with 2 and 3 finger swipes. Thanks in advance..
     
  8. tabiiii

    tabiiii

    Joined:
    Apr 26, 2022
    Posts:
    1
    How to make character camera look around with swipe??