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 santosh810666 · Nov 29, 2013 at 01:55 PM · swipe

How to make swipe function?

i am trying to do swipe functionality for android phone and the code is working but i am facing a problem when i am swiping and removing my finger then it is doing action or else with out removing my finger on device the action can not be done what i want to do can any one help me with this here is my code

 if(Input.touches.Length > 0)
 
      {
 
          Touch t = Input.GetTouch(0);
 
          if(t.phase == TouchPhase.Began)
 
          {
 
               //save began touch 2d point
 
              firstPressPos = new Vector2(t.position.x,t.position.y);
 
          }
 
          if(t.phase == TouchPhase.Ended)
 
          {
 
               //save ended touch 2d point
 
              secondPressPos = new Vector2(t.position.x,t.position.y);
 
                             
 
               //create vector from the two points
 
              currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
 
                 
 
              //normalize the 2d vector
 
              currentSwipe.Normalize();
 
  
 
              //swipe upwards
 
              if(currentSwipe.y > 0 && currentSwipe.x > -0.8f && currentSwipe.x < 0.8f)
 
              {
 
                   lastJumpButtonTime = Time.time;
 
              }
 
  
 
          }
 
      }

   
Comment
Add comment · Show 1
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 PhilRousse · Nov 29, 2013 at 02:43 PM 0
Share

Your question is not really clear, I will assume that you want the swipe to be activated without releasing the touch, see my answer below.

Also you should remove some blank line in your code for better readability.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by PhilRousse · Nov 29, 2013 at 02:42 PM

You need to track the swipe on the TouchPhase.Moved to acheive what you want. Here your code rewritten:

 //Constant use in code. Use these to tweak the result
 float MinSwipeDistance = 35f;
 float SwipeAllowedVariance = 0.8f;
 bool trackSwipe = false;
 
 if(Input.touches.Length > 0)
 {
     Touch t = Input.GetTouch(0);
 
     if(t.phase == TouchPhase.Began)
     {
         //save began touch 2d point
         firstPressPos = new Vector2(t.position.x,t.position.y);
         trackSwipe = true;
     }
 
     if(t.phase == TouchPhase.Moved)
     {
         //save ended touch 2d point
         secondPressPos = new Vector2(t.position.x,t.position.y);
 
         //create vector from the two points
         currentSwipe = secondPressPos - firstPressPos;
 
         //swipe upwards
         //If the swipe is long enough and in the right direction.
         if( (currentSwipe.magnitude > MinSwipeDistance) && (currentSwipe.y > 0) && (Math.Abs(currentSwipe.x) < SwipeAllowedVariance))
         {
             lastJumpButtonTime = Time.time;
             //We've reconized the swipe. Stop tracking it.
             trackSwipe = false;
         }
     }
 
     if(t.phase == TouchPhace.Ended)
     {
         trackSwipe = false;
     }
 }

For vector math, you don't have to substact each Vector element to create a delta vector, you can do it by subtracting a vector by another.

If you need more explanation, please comment.

Comment
Add comment · Show 4 · 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 santosh810666 · Nov 29, 2013 at 05:41 PM 0
Share

hi thanks for your reply your code is working. Can you create more flexible like daring dash game

avatar image PhilRousse · Nov 29, 2013 at 06:47 PM 0
Share

What do you mean by "daring dash game"?

That code is for Swipe Up. It's base on the code that you've write.

 if( (currentSwipe.magnitude > $$anonymous$$inSwipeDistance) && (currentSwipe.y > 0) && ($$anonymous$$ath.Abs(currentSwipe.x) < SwipeAllowedVariance))

At line 27:

  • If you change (currentSwipe.y > 0) by (currentSwipe.y < 0) it will be trigger to swipe down.

  • If you switch `currentSwipe.y` and `currentSwipe.x`, it will trigger to horizontal swipe. x > 0 = Right and x < 0 = left.

avatar image santosh810666 · Nov 30, 2013 at 01:44 PM 0
Share

ya i know sorry it is danger dash game. Now it is working fine like that game thanks for correcting me

avatar image santosh810666 · Dec 07, 2013 at 04:05 AM 0
Share

hi there is a slight problem in this code swipe is working with out releasing my finger but it is working more then once when i swipe my finger and stop with out releasing and swipe again with out releasing my finger then action is repeating twice can you fix this problem action should be once per swipe with out releasing my finger here is the following code that i used

 if(Input.touches.Length > 0)
         {
             Touch t = Input.GetTouch(0);
             if(t.phase == TouchPhase.Began)
             {
                 //save began touch 2d point
                 firstPressPos = new Vector2(t.position.x,t.position.y);
             }
             if(t.phase == TouchPhase.$$anonymous$$oved)
             {
                 //save ended touch 2d point
                 secondPressPos = new Vector2(t.position.x,t.position.y);
                 //create vector from the two points
                 currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
                 //normalize the 2d vector
                 currentSwipe.Normalize();
                 //swipe upwards
                 if(currentSwipe.y > 0 && currentSwipe.x > -0.8f && currentSwipe.x < 0.8f)
                 {
                     lastJumpButtonTime = Time.time;
                 }
             }
         }

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

19 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

Related Questions

Swipe Gesture iOS 1 Answer

Controlling Animation ON TOUCH 0 Answers

Detect if finger lifted off screen 1 Answer

Android how to use swipe to make certain part of an object fade out? 0 Answers

swipe camera movement wont stay between limitations 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