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 ;           }           else   if ( Input . GetKey ( KeyCode . S ) )   {              rigidbody . position   +=  Vector3 . back   *  Time . deltaTime   *  movementSpeed ;           }           else   if ( Input . GetKey ( KeyCode . A ) )   {              rigidbody . position   +=  Vector3 . left   *  Time . deltaTime   *  movementSpeed ;           }           else   if ( Input . GetKey ( KeyCode . D ) )   {              rigidbody . p

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This tutorial gives a complete understanding of Java. This reference will take you through simple and practical approaches while learning Java Programming language.

  Java Tutorial Java - Home Java - Overview Java - Environment Setup Java - Basic Syntax Java - Object & Classes Java - Constructors Java - Basic Datatypes Java - Variable Types Java - Modifier Types Java - Basic Operators Java - Loop Control Java - Decision Making Java - Numbers Java - Characters Java - Strings Java - Arrays Java - Date & Time Java - Regular Expressions Java - Methods Java - Files and I/O Java - Exceptions Java - Inner classes Java Object Oriented Java - Inheritance Java - Overriding Java - Polymorphism Java - Abstraction Java - Encapsulation Java - Interfaces Java - Packages Java Advanced Java - Data Structures Java - Collections Java - Generics Java - Serialization Java - Networking Java - Sending Email Java - Multithreading Java - Applet Basics Java - Documentation Java Useful Resources Java - Questions and Answers Java - Quick Guide Java - Useful Resources Java - Discussion Java - Examples Selected Reading UPSC IAS Exams Notes Developer's Best Practice

Computing language for chapter 3 : The trigonometric function