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 nketc · Aug 20, 2014 at 06:57 AM · androidtouchswipefinger

Do On swipe finger?

I am new in Unity, and i want to add 8 different functions on 8 type of swipe on the screen by finger. I found the code for 4 swipes (left right up and down) but i want to add 8 swipes this shown in image (just for example) .alt text

 #pragma strict

 private var fp : Vector2;  // first finger position
 private var lp : Vector2;  // last finger position

 function Update () {
 for (var touch : Touch in Input.touches)
 {
       if (touch.phase == TouchPhase.Began)
       {
             fp = touch.position;
             lp = touch.position;
       }
       if (touch.phase == TouchPhase.Moved )
       {
             lp = touch.position;
       }
       if(touch.phase == TouchPhase.Ended)
       { 
             if((fp.x - lp.x) > 80) // left swipe
       {
       transform.Rotate(0,180,0);
       } 
       else if((fp.x - lp.x) < -80) // right swipe
       {
             transform.Rotate(0,40,0);
       }
       else if((fp.y - lp.y) < -80 ) // up swipe
       {
              // add your jumping code here
       }
       else if((fp.y - lp.y) < 80 ) // down swipe
       {
              // add your slip code here
       }
   }
  }
 }


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
Best Answer

Answer by robertbu · Aug 20, 2014 at 07:36 AM

Here is a bit of untested code. The value of 'which' passed to the switch statement start at 0 for directly right and walk around counter clockwise.

 #pragma strict
 
 private var fp : Vector2;  // first finger position
  
 function Update () {
     for (var touch : Touch in Input.touches) {
         if (touch.phase == TouchPhase.Began) {
             fp = touch.position;
         }
         if(touch.phase == TouchPhase.Ended) {
             if (fp == Input.mousePosition) return; 
             var dir = Input.mousePosition - fp;
             var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
             if (angle < 0.0)
                 angle = 360.0 + angle;
             var which = Mathf.RoundToInt((angle) / 45.0);
             which = which % 8;    
             switch(which) {
                 case 0: 
                 Debug.Log("0");
                 break;
                 case 1: 
                 Debug.Log("1");
                 break;
                 case 2: 
                 Debug.Log("2");
                 break;
                 case 3: 
                 Debug.Log("3");
                 break;
                 case 4: 
                 Debug.Log("4");
                 break;
                 case 5: 
                 Debug.Log("5");
                 break;                
                 case 6: 
                 Debug.Log("6");
                 break;
                 case 7: 
                 Debug.Log("7");
                 break;                
                 
            }
         }
     }
 }


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 nketc · Aug 20, 2014 at 09:39 AM 0
Share

That's exactly i want... awesome dude... Thanks

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

2 People are following this question.

avatar image avatar image

Related Questions

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

Diagonal swipe issues on Android 1 Answer

detect Swipe in four directions (android) 7 Answers

Swipe and Joystick Together on Mobile 0 Answers

Android "mouse" speed while GetMouseButtonDown(0) 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