• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Character Controller with Unity User Iterface input 1 Answer

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

Timer Freeze Problem 1 Answer

detect Swipe in four directions (android) 7 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges