Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
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
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
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
Add comment · 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.

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

Unity3d Get Cube Pressed On Mobile 1 Answer

Bug? Need to wait about 10 seconds to get back to normal 1 Answer

Stopping rotation at 90° and -90°/270°. 0 Answers

Touch - drag different objects 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