Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment
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

· Add your reply
  • Sort: 
avatar image
1

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
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

TouchPhase doesn't end when player swipes off screen 1 Answer

Swipe menu, problem! 0 Answers

android input vs testing input 0 Answers

How do I intercept touch events in Android native code? 0 Answers

Detect if finger lifted off screen 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