Skip to main content

Simple swipe and tap on mobile phone

  •  Simple swipe and tap on mobile phone


  1.  private Vector3 fp;   //First touch position
  2.     private Vector3 lp;   //Last touch position
  3.     private float dragDistance;  //minimum distance for a swipe to be registered
  4.  
  5.     void Start()
  6.     {
  7.         dragDistance = Screen.height * 15 / 100; //dragDistance is 15% height of the screen
  8.     }
  9.  
  10.     void Update()
  11.     {
  12.         if (Input.touchCount == 1) // user is touching the screen with a single touch
  13.         {
  14.             Touch touch = Input.GetTouch(0); // get the touch
  15.             if (touch.phase == TouchPhase.Began) //check for the first touch
  16.             {
  17.                 fp = touch.position;
  18.                 lp = touch.position;
  19.             }
  20.             else if (touch.phase == TouchPhase.Moved) // update the last position based on where they moved
  21.             {
  22.                 lp = touch.position;
  23.             }
  24.             else if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
  25.             {
  26.                 lp = touch.position;  //last touch position. Ommitted if you use list
  27.  
  28.                 //Check if drag distance is greater than 20% of the screen height
  29.                 if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
  30.                 {//It's a drag
  31.                  //check if the drag is vertical or horizontal
  32.                     if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
  33.                     {   //If the horizontal movement is greater than the vertical movement...
  34.                         if ((lp.x > fp.x))  //If the movement was to the right)
  35.                         {   //Right swipe
  36.                             Debug.Log("Right Swipe");
  37.                         }
  38.                         else
  39.                         {   //Left swipe
  40.                             Debug.Log("Left Swipe");
  41.                         }
  42.                     }
  43.                     else
  44.                     {   //the vertical movement is greater than the horizontal movement
  45.                         if (lp.y > fp.y)  //If the movement was up
  46.                         {   //Up swipe
  47.                             Debug.Log("Up Swipe");
  48.                         }
  49.                         else
  50.                         {   //Down swipe
  51.                             Debug.Log("Down Swipe");
  52.                         }
  53.                     }
  54.                 }
  55.                 else
  56.                 {   //It's a tap as the drag distance is less than 20% of the screen height
  57.                     Debug.Log("Tap");
  58.                 }
  59.             }
  60.         }
  61.     }

Comments

Popular posts from this blog

AWSD controller with c#

      using   UnityEngine ;   using   System.Collections ;   using   System.Collections.Generic ;     [ RequireComponent  ( typeof ( Rigidbody ) ) ]   public   class  PlayerController  :  MonoBehaviour  {         public   float  movementSpeed  =  5 . 0f ;         void  Start  ( )   {                 }             void  Update  ( )   {           Rigidbody rigidbody  =  GetComponent < Rigidbody > ( ) ;             if ( Input . GetKey ( KeyCode . W ) )   {              transform . position   +=  Vector3 . forward   *  Time . deltaTime   *  movementSpeed ;           }     ...

Project 7 Soy Vitou (ITE G8_A2) in Royal University of Phnom Penh (RUPP)

7-segment Display An LED or Light Emitting Diode, is a solid state optical pn-junction diode which emits light energy in the form of photons. The emission of these photons occurs when the diode junction is forward biased by an external voltage allowing current to flow across its junction, and in Electronics we call this process electroluminescence. The actual colour of the visible light emitted by an LED, ranging from blue to red to orange, is decided by the spectral wavelength of the emitted light which itself is dependent upon the mixture of the various impurities added to the semiconductor materials used to produce it. 7-segment Display Light emitting diodes have many advantages over traditional bulbs and lamps, with the main ones being their small size, long life, various colours, cheapness and are readily available, as well as being easy to interface with various other electronic components and digital circuits. But the main advantage of light emitting diodes is that because of th...