• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by ahaseeb91 · Feb 11, 2013 at 01:56 PM · androidtoucheventswipeevent-handling

any one now about the touch events of android swipe events on the android

if anyone can provide me the code for the swipe events for unity it will be great .... please help me out......

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image

Answer by $$anonymous$$ · Feb 11, 2013 at 03:51 PM

This is basically the method that I use. I have tested this on a Galaxy tab some time back (I had to change some values like comfortZone, minSwipeDist, maxSwipeTime) and it worked fine. So you may have to play around with the values to get the desired result. But it works. I did a quick test with this right now with iPhone remote, which worked just fine. Hope this helps!

 using UnityEngine;
 using System.Collections;
 
 public class InputHandler : MonoBehaviour 
 {    
     // Swipe related
     private float                     comfortZone = 500.0f; //Experiment with these values to get desired result
     private float                     minSwipeDist = 10.0f; //Experiment with these values to get desired result
     private float                     maxSwipeTime = 15f;      //Experiment with these values to get desired result
     private enum                     eSwipeDirection 
     {
         None,
         Left,
         Right,
         Up,
         Down,
     }
     private float startTime;
     private Vector2 startPos;
     private bool couldBeSwipe;   
     private eSwipeDirection         m_LastSwipe = InputHandler.eSwipeDirection.None;    
     private Vector2                    m_HorizontalSwipeVector;
     private Vector2                    m_VerticalSwipeVector;
 
     
     void Update () 
     {
         HandleTouchGestureInput();
     }
     
     private void HandleTouchGestureInput()
     {    
         if(Input.touches.Length == 1)
         {
             //Finger data
             Touch touchedFinger = Input.touches[0];
             
             switch(touchedFinger.phase)
             {            
             case TouchPhase.Began: // stop/start swipe
                 m_LastSwipe = InputHandler.eSwipeDirection.None;        
                 couldBeSwipe = true;
                 startPos = touchedFinger.position;                
                 startTime = Time.time;
                 break;
             case TouchPhase.Moved: //Detection if not a swipe
                 if (Mathf.Abs(touchedFinger.position.y - startPos.y) > comfortZone) 
                 {
                     Debug.Log("Not a swipe. Swipe is " + (int)(Mathf.Abs(touchedFinger.position.y - startPos.y) - comfortZone) + "px outside the comfort zone.");
                     couldBeSwipe = false;    
                 }
                 
                 if (Mathf.Abs(touchedFinger.position.x - startPos.x) > comfortZone) 
                 {
                     Debug.Log("Not a swipe. Swipe is " + (int)(Mathf.Abs(touchedFinger.position.x - startPos.x) - comfortZone) + "px outside the comfort zone.");
                     couldBeSwipe = false;    
                 }
                 break;
             case TouchPhase.Ended: //Swipe detection                    
                 if (couldBeSwipe)
                 {                    
                     float swipeTime = Time.time - startTime;                    
                     Vector3 swipeDir = touchedFinger.position - startPos;
                     float swipeDist = swipeDir.magnitude;    
                     Vector3 swipeDirNormalized = swipeDir.normalized;
                 
                     Debug.Log("swipeDist : " + swipeDist + " and min swipe dis is : " + minSwipeDist);
                     Debug.Log("swipeTime : " + swipeTime + " and max swipe time is : " + maxSwipeTime);
                     
                     if ((swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist)) 
                     {
 //                        // It's a swiiiiiiiiiiiipe!                    
                         float tolerance = 0.1f;
                         float minSwipeDot = Mathf.Clamp01( 1.0f - tolerance );
                         
                         switch(Input.deviceOrientation)
                         {
                         case DeviceOrientation.FaceDown:
                         case DeviceOrientation.FaceUp:
                         case DeviceOrientation.LandscapeLeft:
                         case DeviceOrientation.LandscapeRight:
                             m_HorizontalSwipeVector = Vector2.right;
                             m_VerticalSwipeVector = Vector2.up;
                             break;
                         case DeviceOrientation.Portrait:
                         case DeviceOrientation.PortraitUpsideDown:
                         case DeviceOrientation.Unknown:
                             m_HorizontalSwipeVector = Vector2.up;
                             m_VerticalSwipeVector = -Vector2.right;
                             break;
                         default:
                             break;
                         }
                         
                         if( Vector2.Dot(swipeDirNormalized, m_HorizontalSwipeVector) >= minSwipeDot )
                         {
                             m_LastSwipe = InputHandler.eSwipeDirection.Right;
                         }
                         
                         if( Vector2.Dot(swipeDirNormalized, -m_HorizontalSwipeVector) >= minSwipeDot )
                         {
                             m_LastSwipe = InputHandler.eSwipeDirection.Left;                            
                         }
                         
                         if( Vector2.Dot(swipeDirNormalized, m_VerticalSwipeVector) >= minSwipeDot )
                         {
                             m_LastSwipe = InputHandler.eSwipeDirection.Up;                            
                         }
                         
                         if( Vector2.Dot(swipeDirNormalized, -m_VerticalSwipeVector) >= minSwipeDot )
                         {
                             m_LastSwipe = InputHandler.eSwipeDirection.Down;                            
                         }
                         
                         Debug.Log("Last swipe : " + m_LastSwipe);
                     }
                 }
                 break;
             default:
                 break;
             }
         }
     }
 }
 
Comment
Bunny83

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

multi touch / swipe issues 5 Answers

Camera swipe AND gameObject touch (Android) 1 Answer

detect Swipe in four directions (android) 7 Answers

Android "mouse" speed while GetMouseButtonDown(0) 0 Answers

Do On swipe finger? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges