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 ExcitedMormon · Nov 27, 2014 at 08:25 PM · c#touchdetectionswipe

Make Swipe Detection Longer?

Hello Unity Answers. I know this might be sort of a do this for me question, but I just want to know something about this swipe code I was lucky enough to find after searching the web for a while. You see everything works good, however there are some problems. The swipe script has to work with another script of mine that allows me to jump when I tap anywhere on the screen. So when I sometimes tap, it detects it as a swipe and does not jump. This leads me to die many times on my game. What I want to know, is how to make the swipe the detection longer before it finally detects it as a swipe? So like I would have to swipe for a longer time. Right now just the slightest swipe down, it detects it. So how can I make it longer? Here's the script:

 public class SwipeDetector : MonoBehaviour {
     
     // Values to set:
     public float comfortZone = 70.0f; //was 70.0f
     public float minSwipeDist = 14.0f; //was 14.0f //17.0f seemed to do nothing //25.0f
     public float maxSwipeTime = 0.12f; //was 0.5f sucks //2 one was 0.8f ok but still not //3 was 0.10f good but not without problems //0.12f feels the same //0.15f seems worse //0.17f ok //0.13f
 
 
     public GameObject Test;
 
 
     private float startTime;
     private Vector2 startPos;
     private bool couldBeSwipe;
     
     public enum SwipeDirection {
         None,
         Up,
         Down
     }
     
     public SwipeDirection lastSwipe = SwipeDetector.SwipeDirection.None;
     public float lastSwipeTime;
     
     
     void  Update()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.touches[0];
             
             switch (touch.phase)
             {
             case TouchPhase.Began:
                 lastSwipe = SwipeDetector.SwipeDirection.None;
                 lastSwipeTime = 0;
                 couldBeSwipe = true;
                 startPos = touch.position;
                 startTime = Time.time;
                 break;
                 
             case TouchPhase.Moved:
                 if (Mathf.Abs(touch.position.x - startPos.x) > comfortZone)
                 {
                     Debug.Log("Not a swipe. Swipe strayed " + (int)Mathf.Abs(touch.position.x - startPos.x) +
                               "px which is " + (int)(Mathf.Abs(touch.position.x - startPos.x) - comfortZone) +
                               "px outside the comfort zone.");
                     couldBeSwipe = false;
                 }
                 break;
             case TouchPhase.Ended:
                 if (couldBeSwipe)
                 {
                     float swipeTime = Time.time - startTime;
                     float swipeDist = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
                     
                     if ((swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist)) //was &&
                     {
                         // It's a swiiiiiiiiiiiipe!
                         float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
                         
                         // If the swipe direction is positive, it was an upward swipe.
                         // If the swipe direction is negative, it was a downward swipe.
                         if (swipeValue > 0)
                             lastSwipe = SwipeDetector.SwipeDirection.Up;
                         else if (swipeValue < 0)
                             lastSwipe = SwipeDetector.SwipeDirection.Down;
                         
                         // Set the time the last swipe occured, useful for other scripts to check:
                             
                             lastSwipeTime = Time.time;
 Test.setActive(true);
                             
                     }
                 }
                 break;
             }
         }
     }
 }


As you can tell by the // notes. I already messed around with the values. You can read the notes to see my results.

Comment
Add comment · Show 2
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 koray1396 · Nov 27, 2014 at 08:43 PM 0
Share

Wouldn't it be better, if you combined both scripts, since swipe and tap can not coexist, and you could easily differ them by conditions? That's really my opinion.

In your situation, conditions for swipe and tap must be conflicting, the only difference between tap and swipe can be $$anonymous$$SwipeDist, but i guess you are not checking the same value for tap.

Also you can put a $$anonymous$$SwipeTime, since swipes are not usually that fast.

avatar image ExcitedMormon · Nov 28, 2014 at 01:45 AM 0
Share

"Wouldn't it be better, if you combined both scripts, since swipe and tap can not coexist, and you could easily differ them by conditions?" Possibly yes, not quite sure on how to go about that. The script that I use for the tap is part of the Character Controller which is already big enough. I'm guessing you mean like checking if($$anonymous$$SwipeTime == 10) Swipe and else, jump. Little confusing to me but I see what you're saying.

By the way, what value do you think would be good for the $$anonymous$$SwipeTime? Originally the maxSwipeTime was 0.5f. now it's 0.12f since it seems to work better.

0 Replies

· Add your reply
  • Sort: 

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

Detect the swipe and add force respective to it? 3 Answers

Detect swipe up gesture IOS 1 Answer

Swipe and Hold to move character . 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Reproducing the "spin" effect of Flick Kick Football 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