Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by alone1992 · Dec 19, 2013 at 06:17 PM · androidtouchswipe

detect Swipe in four directions (android)

Hello I need to java script code that can detect four directions (up, down , right , left) swipe on screen and debug once when that is happen. I write some codes with delta position but never couldn't have result. thanks

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 sherryt400 · Jul 02, 2015 at 09:06 AM 0
Share

Hello Guys i want to detect tap also with swipes i.e if i tap player jumps and if i swipe down player slides.??

7 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by MrVerdoux · Dec 20, 2013 at 10:30 AM

Why don´t you check

 float deltaX = deltaPosition.x;
 float deltaY = deltaPosition.y;
 float biggerDelta = (deltaX > deltaY) ? deltaX : deltaY;
 int direction = Mathf.Sign (biggerDelta);

With that info you should be able to get it. At least that´s how I do it.

Edit: new script, same idea, it might need some tweak but I don´t see why it shoudn´t work.

 using System.Collections;
 
 public class MiniGestureRecognizer : MonoBehaviour {
 
     public enum SwipeDirection{
         Up,
         Down,
         Right,
         Left
     }
 
     public static event Action<SwipeDirection> Swipe;
     private bool swiping = false;
     private bool eventSent = false;
     private Vector2 lastPosition;
     
     void Update () {
         if (Input.touchCount == 0) 
             return;
 
         if (Input.GetTouch(0).deltaPosition.sqrMagnitude != 0){
             if (swiping == false){
                 swiping = true;
                 lastPosition = Input.GetTouch(0).position;
                 return;
             }
             else{
                 if (!eventSent) {
                     if (Swipe != null) {
                         Vector2 direction = Input.GetTouch(0).position - lastPosition;
 
                         if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)){
                             if (direction.x > 0) 
                                 Swipe(SwipeDirection.Right);
                             else
                                 Swipe(SwipeDirection.Left);
                         }
                         else{
                             if (direction.y > 0)
                                 Swipe(SwipeDirection.Up);
                             else
                                 Swipe(SwipeDirection.Down);
                         }
 
                         eventSent = true;
                     }
                 }
             }
         }
         else{
             swiping = false;
             eventSent = false;
         }
     }
 }

I am sorry about the amount of ifs, I did this quite fast...

Comment
Add comment · Show 7 · 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 ozturkcompany · Dec 23, 2013 at 09:48 AM 1
Share

Why wont this code work for you and looking for additional answers? Although this code will only work if only one finger is moving on the screen because delta position will get zero if more than one finger moving on the screen

avatar image MrVerdoux · Dec 23, 2013 at 01:49 PM 1
Share

@ozturkcompany are you sure that deltaPosition will be zero? Usually I just use the input from the first touch and it doesn´t become zero.

avatar image alone1992 · Jan 04, 2014 at 10:56 AM 0
Share

@ozturkcompany sağ ol

avatar image David_29 · Jan 29, 2014 at 02:41 AM 0
Share

Does it apply only on the camera as well also?

avatar image alone1992 · Jan 29, 2014 at 07:10 PM 0
Share

I think your question answer is "yes" but I not tested it in other objects yet

Show more comments
avatar image
6

Answer by Nomibuilder · Jan 24, 2015 at 01:53 PM

 using UnityEngine;
 using System.Collections;
 
 public class SwipeDetector : MonoBehaviour 
 {
     
     public float minSwipeDistY;
 
     public float minSwipeDistX;
         
     private Vector2 startPos;
 
     void Update()
     {
 //#if UNITY_ANDROID
         if (Input.touchCount > 0) 
             
         {
             
             Touch touch = Input.touches[0];
             
             
             
             switch (touch.phase) 
                 
             {
                 
             case TouchPhase.Began:
 
                 startPos = touch.position;
                 
                 break;
                 
                 
                 
             case TouchPhase.Ended:
 
                     float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
 
                     if (swipeDistVertical > minSwipeDistY) 
                         
                     {
                         
                         float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
                         
                         if (swipeValue > 0)//up swipe
 
                             //Jump ();
                         
                         else if (swipeValue < 0)//down swipe
 
                             //Shrink ();
                         
                     }
                     
                     float swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
                     
                     if (swipeDistHorizontal > minSwipeDistX) 
                         
                     {
                         
                         float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
                         
                         if (swipeValue > 0)//right swipe
                             
                             //MoveRight ();
                         
                         else if (swipeValue < 0)//left swipe
                             
                             //MoveLeft ();
                         
                     }
                 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 alone1992 · Jan 28, 2015 at 08:12 AM 2
Share

thanks @Nomibuilder , this question is solved

avatar image
5

Answer by pfonseca · Aug 13, 2014 at 01:07 AM

This works to me: http://pfonseca.com/swipe-detection-on-unity/

Comment
Add comment · Show 3 · 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 ScriptLab · Jun 20, 2016 at 12:59 PM 0
Share

works amazing

avatar image jdonline · Oct 11, 2018 at 03:22 PM 2
Share

This link appears to be malware, I would avoid clicking on it

avatar image JustNothink · Apr 26, 2020 at 07:58 PM 0
Share

Seems like the domain is sold, the site displays weird Chinese fap games. I had just stopped being so paranoid about the links I click but now I'm back on it.

avatar image
2

Answer by tvranjith · Feb 10, 2015 at 12:57 PM

I found this post http://rtvstudios.blogspot.com/2015/02/detecting-swipe-from-unity3d-c-scripts.html

It detects swipe in four direction and we can also control the swipe speed and angle for the swipe in four direction.

Comment
Add comment · 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
2

Answer by Max_power1965 · May 10, 2017 at 12:29 PM

Hu guys, I wrote this simple tutorial wich is basically 7 lines of code and works like a charm. Easy and simple. You just have to use te build in Unity event system instead to write long and useless code. No need to use update or fixed update.

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 Losteone · Apr 08, 2018 at 08:15 PM 0
Share

Hello. Can you reload script here

Because its not working for downloading (((

  • 1
  • 2
  • ›

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

33 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 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

2D platformer controller script. How to jump with touch? 0 Answers

Android "mouse" speed while GetMouseButtonDown(0) 0 Answers

Android Drag By Steps 0 Answers

IOS Swipe Gesture 0 Answers

Swipe menu, problem! 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