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 NixarXidiqui · Mar 04, 2014 at 10:31 AM · iostouchswipetouches

Swipe Control Issue.. Need help

I'm having some troubles with creating swipe controls for a game. I'm trying to recreate the controls of the game Subway Surfers, where a swipe is detected during the Touch.Moved phase not the Touch.End phase. Right now it works most of the time but once in a while (to often) a vertical swipe will be processed as a horizontal one. I've posed the code below, thanks in advance for any help.

        int swipeIndex = -1; //index of first detected swipe to prevent multiple swipes
        Vector2 startPos = Vector.zero; //starting position of touch
        //minSwipeDist is the distance in inches the player must swipe for a correct swipe
        for (int i = 0; i < Input.touchCount; i++) {
             Touch touch = Input.touches[i];
             switch (touch.phase) {
                 case TouchPhase.Began:
                     if (swipeIndex == -1) {
                         swipeIndex = i;
                         startPos = touch.position;
                     }
                     break;
                 case TouchPhase.Moved:
                     if(i != swipeIndex)
                         break;
                     Vector2 direction = touch.position - startPos;
                     //vertical swipe
                     if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y)) {
                         //swipe up
                         if ((touch.position.y - startPos.y) > minSwipeDist) {
                             //Process Up swipe
                             swipeIndex = -1;
                         }
                         //swipe down
                         else if ((touch.position.y - startPos.y) < -minSwipeDist) {
                             //Process down swipe
                             swipeIndex = -1;
                         }
                     }
                     //horizontal swipe
                     else {
                         //swipe left
                         if ((touch.position.x - startPos.x) < -minSwipeDist) {
                             //process left swipe
                             swipeIndex = -1;
                         }
                         //swipe right
                         else if ((touch.position.x - startPos.x) > minSwipeDist) {
                             //process right swipe
                             swipeIndex = -1;
                         }
                     }
                     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
0

Answer by Graham-Dunnett · Mar 04, 2014 at 10:36 AM

When you first start moving it's easy for the direction code to get it you. You check (line 18) that the touch has moved less in x than in y, and that means a vertical swipe. But, if x==1 and y==2, that does not mean that the swipe will be vertical, it's basically too small a change to know. You probably want to wait until the finger has moved 5% of the screen width or height before deciding what direction they are moving in. You kind of do this with you minSwipeDist variable, but I think you'd want to do this test before you decide there is a swipe.

Comment
Add comment · Show 1 · 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 NixarXidiqui · Mar 04, 2014 at 10:47 AM 0
Share

If I understood correctly, this is what you mean ?? Even though the same issue occurs. The very first touch gets some random behavior.

 foreach(Touch touch in Input.touches)
                     {
                         Vector2 touchPosition = touch.position;
                         
                         if(touch.phase == TouchPhase.Began && swipeID == -1)
                         {
                             swipeID = touch.fingerId;
                             startPos = touchPosition;
                         } 
                         else if(touch.fingerId == swipeID)
                         {
                             Vector2 delta = touchPosition - startPos;
                             
                             if(touch.phase == TouchPhase.$$anonymous$$oved && delta.magnitude > $$anonymous$$$$anonymous$$ovement)
                             {
                                 swipeID = -1;
                                 if($$anonymous$$athf.Abs(delta.x) > $$anonymous$$athf.Abs (delta.y))
                                 {
                                     if(delta.x > 0)
                                         swipeRight = true;
                                     else
                                         swipeLeft = true;
                                 }
                                 else{
                                     if(delta.y > 0)
                                         swipeUp = true;
                                     else
                                         swipeDown = true;
                                 }
                             }
                         }
                     }

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

21 People are following this question.

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

Related Questions

Recommendations on best way to begin implementing Swipe for iOS game? 1 Answer

Multiple Touch Drag iOS 0 Answers

Swipe horizontally and hold finger down to move character? 1 Answer

Detect swipe up gesture IOS 1 Answer

How can I swipe function that lets the player select buttons? 0 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