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 diagonal touch detection | Need help | C#

Discussion in 'Scripting' started by xXKeLLerman, Jun 4, 2014.

  1. xXKeLLerman

    xXKeLLerman

    Joined:
    Aug 3, 2013
    Posts:
    18
    So this is a prety easy swipe detection code.
    It detects vertical and horizontal swipes

    How would i add detection if the player swiped diagonaly?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Swipe : MonoBehaviour {
    5.    
    6.     Vector2 initTouchPos;
    7.     public float speed = 1f;
    8.  
    9.     void FixedUpdate ()
    10.     {
    11. #if UNITY_4_3
    12.  
    13.         if(Input.GetKey(KeyCode.UpArrow))
    14.         {
    15.             rigidbody2D.AddForce (Vector2.up*speed);
    16.         }
    17.         if(Input.GetKey(KeyCode.DownArrow))
    18.         {
    19.             rigidbody2D.AddForce (Vector2.up*-speed);
    20.         }
    21.         if(Input.GetKey(KeyCode.RightArrow))
    22.         {
    23.             rigidbody2D.AddForce (Vector2.right*speed);
    24.         }
    25.         if(Input.GetKey(KeyCode.LeftArrow))
    26.         {
    27.             rigidbody2D.AddForce (Vector2.right*-speed);
    28.         }
    29. #endif
    30.         int fingerCount = 0;
    31.        
    32.         foreach (Touch touch in Input.touches)
    33.         {
    34.             if(touch.phase == TouchPhase.Began)
    35.             {
    36.                 initTouchPos = touch.position;
    37.             }
    38.            
    39.             if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
    40.             {
    41.                 fingerCount++;
    42.                
    43.                 if(fingerCount == 1 && touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended)
    44.                 {
    45.                     Vector2 touchFacing = (initTouchPos - touch.position).normalized;
    46.                    
    47.                     if(Vector2.Dot(touchFacing, Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    48.                     {
    49.                         Ability("SwipeDown");
    50.                     }
    51.                     if(Vector2.Dot(touchFacing, -Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    52.                     {
    53.                         Ability("SwipeUp");
    54.                     }
    55.                     if(Vector2.Dot(touchFacing, Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    56.                     {
    57.                         Ability("SwipeLeft");
    58.                     }
    59.                     if(Vector2.Dot(touchFacing, -Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    60.                     {
    61.                         Ability("SwipeRight");
    62.                     }
    63.                 }
    64.             }
    65.         }
    66.     }
    67.    
    68.     void Ability(string swipeType)
    69.     {
    70.         switch(swipeType)
    71.         {
    72.            
    73.         case "SwipeUp":
    74.         {
    75.             rigidbody2D.AddForce (Vector2.up*speed);
    76.             break;
    77.         }
    78.         case "SwipeDown":
    79.         {
    80.             rigidbody2D.AddForce (Vector2.up*-speed);
    81.             break;
    82.         }
    83.         case "SwipeLeft":
    84.         {
    85.             rigidbody2D.AddForce (Vector2.right*-speed);
    86.             break;
    87.         }
    88.         case "SwipeRight":
    89.         {
    90.             rigidbody2D.AddForce (Vector2.right*speed);          
    91.             break;
    92.         }
    93.         default:
    94.         {
    95.             Debug.Log("Ability string not found");
    96.             break;
    97.         }
    98.            
    99.         }
    100.     }
    101. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    more "if's" would work

    Code (csharp):
    1.  
    2. if(Vector2.Dot(touchFacing, Vector2.up)>0.8&&Vector2.Dot(touchFacing, Vector2.right)>0.8&&Vector2.Distance(initTouchPos, touch.position)>20)
    3. {
    4. <<up and right>>
    5. }
    6.  
     
  3. xXKeLLerman

    xXKeLLerman

    Joined:
    Aug 3, 2013
    Posts:
    18
    Nope it doesnt work.
    It just detects horizontal and vertical

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Swipe : MonoBehaviour {
    5.    
    6.     Vector2 initTouchPos;
    7.     public float speed = 1f;
    8.  
    9.     void FixedUpdate ()
    10.     {
    11. #if UNITY_4_3
    12.  
    13.         if(Input.GetKey(KeyCode.UpArrow))
    14.         {
    15.             rigidbody2D.AddForce (Vector2.up*speed);
    16.         }
    17.         if(Input.GetKey(KeyCode.DownArrow))
    18.         {
    19.             rigidbody2D.AddForce (Vector2.up*-speed);
    20.         }
    21.         if(Input.GetKey(KeyCode.RightArrow))
    22.         {
    23.             rigidbody2D.AddForce (Vector2.right*speed);
    24.         }
    25.         if(Input.GetKey(KeyCode.LeftArrow))
    26.         {
    27.             rigidbody2D.AddForce (Vector2.right*-speed);
    28.         }
    29. #endif
    30.         int fingerCount = 0;
    31.        
    32.         foreach (Touch touch in Input.touches)
    33.         {
    34.             if(touch.phase == TouchPhase.Began)
    35.             {
    36.                 initTouchPos = touch.position;
    37.             }
    38.            
    39.             if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
    40.             {
    41.                 fingerCount++;
    42.                
    43.                 if(fingerCount == 1 && touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended)
    44.                 {
    45.                     Vector2 touchFacing = (initTouchPos - touch.position).normalized;
    46.                    
    47.                     if(Vector2.Dot(touchFacing, Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    48.                     {
    49.                         Ability("SwipeDown");
    50.                     }
    51.                     if(Vector2.Dot(touchFacing, -Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    52.                     {
    53.                         Ability("SwipeUp");
    54.                     }
    55.                     if(Vector2.Dot(touchFacing, Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    56.                     {
    57.                         Ability("SwipeLeft");
    58.                     }
    59.                     if(Vector2.Dot(touchFacing, -Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    60.                     {
    61.                         Ability("SwipeRight");
    62.                     }
    63.                     //=================================================================================================\\
    64.                     if(Vector2.Dot(touchFacing, Vector2.up) > 0.8 && Vector2.Dot(touchFacing, Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    65.                     {
    66.                         Ability("SwipeDownLeft");
    67.                     }
    68.                     if(Vector2.Dot(touchFacing, -Vector2.up) > 0.8 && Vector2.Dot(touchFacing, -Vector2.right) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    69.                     {
    70.                         Ability("SwipeUpRight");
    71.                     }
    72.                     if(Vector2.Dot(touchFacing, Vector2.right) > 0.8 && Vector2.Dot(touchFacing, -Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    73.                     {
    74.                         Ability("SwipeUpLeft");
    75.                     }
    76.                     if(Vector2.Dot(touchFacing, -Vector2.right) > 0.8 && Vector2.Dot(touchFacing, Vector2.up) > 0.8 && Vector2.Distance(initTouchPos, touch.position) > 20)
    77.                     {
    78.                         Ability("SwipeDownRight");
    79.                     }
    80.                 }
    81.             }
    82.         }
    83.     }
    84.    
    85.     void Ability(string swipeType)
    86.     {
    87.         switch(swipeType)
    88.         {
    89.            
    90.         case "SwipeUp":
    91.         {
    92.             rigidbody2D.AddForce (Vector2.up*speed);
    93.             break;
    94.         }
    95.         case "SwipeDown":
    96.         {
    97.             rigidbody2D.AddForce (Vector2.up*-speed);
    98.             break;
    99.         }
    100.         case "SwipeLeft":
    101.         {
    102.             rigidbody2D.AddForce (Vector2.right*-speed);
    103.             break;
    104.         }
    105.         case "SwipeRight":
    106.         {
    107.             rigidbody2D.AddForce (Vector2.right*speed);          
    108.             break;
    109.         }
    110.         //============================================\\
    111.         case "SwipeUpRight":
    112.         {
    113.             rigidbody2D.AddForce (Vector2.up*speed);
    114.             rigidbody2D.AddForce (Vector2.right*speed);
    115.             break;
    116.         }
    117.         case "SwipeDownLeft":
    118.         {
    119.             rigidbody2D.AddForce (Vector2.up*-speed);
    120.             rigidbody2D.AddForce (Vector2.right*-speed);
    121.             break;
    122.         }
    123.         case "SwipeUpLeft":
    124.         {
    125.             rigidbody2D.AddForce (Vector2.up*speed);
    126.             rigidbody2D.AddForce (Vector2.right*-speed);
    127.             break;
    128.         }
    129.         case "SwipeDownRight":
    130.         {
    131.             rigidbody2D.AddForce (Vector2.up*-speed);
    132.             rigidbody2D.AddForce (Vector2.right*speed);          
    133.             break;
    134.         }
    135.         default:
    136.         {
    137.             Debug.Log("Ability string not found");
    138.             break;
    139.         }
    140.            
    141.         }
    142.     }
    143. }