• 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 the1337est · Aug 02, 2015 at 08:00 AM · c#androidtouchswipe

Diagonal swipe issues on Android

Hi,

I'm using MNOGUE's code for detecting 8-way swipe (including diagonals, vertical and horizontal). The problem is, sometimes the diagonal swipes are not detected, and vertical or horizontal swipes happen instead. What can I do to this code to possibly improve the diagonal swipe response? Thanks in advance.

Here's the code:

 private float fingerStartTime  = 0.0f;
 private Vector2 fingerStartPos = Vector2.zero;
 
 private bool isSwipe = false;
 private float minSwipeDist  = 30.0f;
 private float maxSwipeTime = 0.5f;
 
 
 void Update()
 {
 
 if (Input.touchCount > 0 && Time.timeScale > 0.0f) 
             {
             
                 foreach (Touch touch in Input.touches) 
                 {
 
                     switch (touch.phase) 
                     {
 
                     case TouchPhase.Began:
                         /* this is a new touch */
                         isSwipe = true;
                         fingerStartTime = Time.time;
                         fingerStartPos = touch.position;
                         break;
                 
                     case TouchPhase.Canceled:
                         /* The touch is being canceled */
                         isSwipe = false;
                         break;
                 
                     case TouchPhase.Ended:
                     
                         float gestureTime = Time.time - fingerStartTime;
                         float gestureDist = (touch.position - fingerStartPos).magnitude;
                         
                         if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
                         {
 
                             Vector2 direction = touch.position - fingerStartPos;
                             //Vector2 swipeType = Vector2.zero;
                             int swipeType = -1;
                             if (Mathf.Abs(direction.normalized.x) > 0.8)
                             {
 
                                 if (Mathf.Sign(direction.x) > 0) swipeType = 0; // swipe right
                                 else swipeType = 1; // swipe left
                             
                             }
                             else if (Mathf.Abs(direction.normalized.y) > 0.8)
                             {
                                 if (Mathf.Sign(direction.y) > 0) swipeType = 2; // swipe up
                                 else swipeType = 3; // swipe down
                             }
                             else
                             {
                                 // diagonal:
                                 if (Mathf.Sign(direction.x) > 0)
                                 {
 
                                     if (Mathf.Sign(direction.y) > 0) swipeType = 4; // swipe diagonal up-right
                                     else swipeType = 5; // swipe diagonal down-right
                                 
                                 }
                                 else
                                 {
 
                                     if (Mathf.Sign(direction.y) > 0) swipeType = 6; // swipe diagonal up-left
                                     else swipeType = 7; // swipe diagonal down-left
                                 
                                 }
                             
                             }
 
                             switch(swipeType)
                             {
 
                                 case 0: //right
                                     
                                                                         break;
   
                             
                                 case 1: //left
                                     
                                     break;
 
                                 case 2: //up
                                     
                                     break;
 
                                 case 3: //down
                                     
 
                                 case 4: //up right
                                     
 
                                 case 5: //down right
                                 
                                     break;
 
                                 case 6: //up left
                                 
                                     break;
 
                                 case 7: //down left
                                 
                                     
                                     break;
                             
                             }
 
                         }
 
                         break;
                 
                     }
             
                 }
         
             }
 }




Comment
tkamruzzaman

People who like this

1 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

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by tanoshimi · Aug 02, 2015 at 08:02 AM

Lower the sensitivity of the cardinal direction by reducing the 0.8 in lines 44 and 51 to, say, 0.65.

Comment
the1337est

People who like this

1 Show 2 · 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
avatar image the1337est · Aug 03, 2015 at 09:30 AM 1
Share

Thanks tanoshimi. Lowering the sensitivity of horizontal and vertical swipes was the right idea. But there was one small mistake with what you suggested. Lowering sensitivity would mean increasing the threshold value, not decreasing it.

I changed 0.8 to 0.9 and I think the diagonal swipes are better now. Thanks, I'll vote you up.

avatar image tkamruzzaman the1337est · Dec 29, 2015 at 06:34 AM 0
Share

thank you. it is very helpful :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

multi touch / swipe issues 5 Answers

Make sphere shoot to Touch.position error 1 Answer

.NET System ticks on Android - what am I doing wrong? 0 Answers

How to prevent touch going through UI? 4 Answers

Android Touch bug!? 2 Answers


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