Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by fiersking_unity · May 28, 2018 at 06:36 AM · androidswipe

swipe manager sometime ignore swipe ?

Hi everyone! i have a little problem with a pretty good swipe manager i have found here : https://forum.unity.com/threads/swipe-in-all-directions-touch-and-mouse.165416/page-2

it take tap and swipe on both mouse and finger.

sometime (maybe 1 on 30 swipe or tap) unity ignore my swipe or my tap for 1 or two second maybe it's realy strange ... the game is realy playable (it's a plateformer) but sometime i want to do something i tap i swipe and ... nothings happend.

heres the code of the swipemanager i use :

 using UnityEngine;
     using System.Collections.Generic;
     using System.Linq;
     using UnityEngine.EventSystems;
     
     class CardinalDirection
     {
         public static readonly Vector2 Up = new Vector2(0, 1);
         public static readonly Vector2 Down = new Vector2(0, -1);
         public static readonly Vector2 Right = new Vector2(1, 0);
         public static readonly Vector2 Left = new Vector2(-1, 0);
         public static readonly Vector2 UpRight = new Vector2(1, 1);
         public static readonly Vector2 UpLeft = new Vector2(-1, 1);
         public static readonly Vector2 DownRight = new Vector2(1, -1);
         public static readonly Vector2 DownLeft = new Vector2(-1, -1);
         public static readonly Vector2 Tap = new Vector2(0, 0);
     }
     
     public enum Swipe
     {
         None,
         Up,
         Down,
         Left,
         Right,
         UpLeft,
         UpRight,
         DownLeft,
         DownRight,
         Tap
     };
     
     public class SwipeManager : MonoBehaviour
     {
         #region Inspector Variables
     
         [Tooltip("Min swipe distance (inches) to register as swipe")]
         [SerializeField]
         float minSwipeLength = 0.5f;
     
     
         [Tooltip("If true, a swipe is counted when the min swipe length is reached. If false, a swipe is counted when the touch/click ends.")]
         [SerializeField]
         bool triggerSwipeAtMinLength = false;
     
         [Tooltip("Whether to detect eight or four cardinal directions")]
         [SerializeField]
         bool useEightDirections = false;
     
         #endregion
     
         const float eightDirAngle = 0.906f;
         const float fourDirAngle = 0.5f;
         const float defaultDPI = 72f;
         const float dpcmFactor = 2.54f;
     
         static Dictionary<Swipe, Vector2> cardinalDirections = new Dictionary<Swipe, Vector2>()
         {
             { Swipe.Up,         CardinalDirection.Up                 },
             { Swipe.Down,         CardinalDirection.Down             },
             { Swipe.Right,         CardinalDirection.Right             },
             { Swipe.Left,         CardinalDirection.Left             },
             { Swipe.UpRight,     CardinalDirection.UpRight             },
             { Swipe.UpLeft,     CardinalDirection.UpLeft             },
             { Swipe.DownRight,     CardinalDirection.DownRight         },
             { Swipe.DownLeft,     CardinalDirection.DownLeft         },
             { Swipe.Tap,     CardinalDirection.Tap         }
     
     
         };
     
         public delegate void OnSwipeDetectedHandler(Swipe swipeDirection, Vector2 swipeVelocity);
     
         static OnSwipeDetectedHandler _OnSwipeDetected;
         public static event OnSwipeDetectedHandler OnSwipeDetected
         {
             add
             {
     
                 _OnSwipeDetected += value;
                 autoDetectSwipes = true;
             }
             remove
             {
     
                 _OnSwipeDetected -= value;
             }
         }
     
         public static Vector2 swipeVelocity;
     
         static float dpcm;
         static float swipeStartTime;
         static float swipeEndTime;
         static bool autoDetectSwipes;
         static bool swipeEnded;
         static Swipe swipeDirection;
         static Vector2 firstPressPos;
         static Vector2 secondPressPos;
         static SwipeManager instance;
     
     
         void Awake()
         {
             instance = this;
             float dpi = (Screen.dpi == 0) ? defaultDPI : Screen.dpi;
             dpcm = dpi / dpcmFactor;
         }
     
         void Update()
         {
             if (autoDetectSwipes)
             {
     
                 DetectSwipe();
             }
         }
     
         /// <summary>
         /// Attempts to detect the current swipe direction.
         /// Should be called over multiple frames in an Update-like loop.
         /// </summary>
         static void DetectSwipe()
         {
     
             if (IsPointerOverGameObject() ||
                Time.timeScale == 0)
             { return; }
     
             if (GetTouchInput() || GetMouseInput())
             {
                 // Swipe already ended, don't detect until a new swipe has begun
                 if (swipeEnded)
                 {
                     return;
                 }
     
                 Vector2 currentSwipe = secondPressPos - firstPressPos;
                 float swipeCm = currentSwipe.magnitude / dpcm;
     
                 // Check the swipe is long enough to count as a swipe (not a touch, etc)
                 if (swipeCm < instance.minSwipeLength)
                 {
                     // Swipe was not long enough, abort
                     if (!instance.triggerSwipeAtMinLength)
                     {
                         if (Application.isEditor)
                         {
     
                             // print("[SwipeManager] Swipe was not long enough, can be detected as a Tap.");
                         }
                         swipeDirection = Swipe.Tap;
     
                         //trying to send back simple tap
                         if (_OnSwipeDetected != null)
                         {
     
                             _OnSwipeDetected(swipeDirection, swipeVelocity);
                         }
                     }
     
                     return;
                 }
     
                 swipeEndTime = Time.time;
                 swipeVelocity = currentSwipe * (swipeEndTime - swipeStartTime);
                 swipeDirection = GetSwipeDirByTouch(currentSwipe);
                 swipeEnded = true;
     
                 if (_OnSwipeDetected != null)
                 {
     
                     _OnSwipeDetected(swipeDirection, swipeVelocity);
                 }
             }
             else
             {
                 swipeDirection = Swipe.None;
             }
         }
     
         public static bool IsSwipingRight() { return IsSwipingDirection(Swipe.Right); }
         public static bool IsSwipingLeft() { return IsSwipingDirection(Swipe.Left); }
         public static bool IsSwipingUp() { return IsSwipingDirection(Swipe.Up); }
         public static bool IsSwipingDown() { return IsSwipingDirection(Swipe.Down); }
         public static bool IsSwipingDownLeft() { return IsSwipingDirection(Swipe.DownLeft); }
         public static bool IsSwipingDownRight() { return IsSwipingDirection(Swipe.DownRight); }
         public static bool IsSwipingUpLeft() { return IsSwipingDirection(Swipe.UpLeft); }
         public static bool IsSwipingUpRight() { return IsSwipingDirection(Swipe.UpRight); }
         public static bool IsTapping()
         {
     
             return IsSwipingDirection(Swipe.Tap);
         }
     
         public static bool IsPointerOverGameObject()
         {
             //check mouse
             if (EventSystem.current.IsPointerOverGameObject())
                 return true;
     
             //check touch
             if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
             {
                 if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
                     return true;
             }
     
             return false;
         }
     
         #region Helper Functions
     
         static bool GetTouchInput()
         {
             if (Input.touches.Length > 0)
             {
                 Touch t = Input.GetTouch(0);
     
                 // Swipe/Touch started
                 if (t.phase == TouchPhase.Began)
                 {
                     firstPressPos = t.position;
                     swipeStartTime = Time.time;
                     swipeEnded = false;
                     // Swipe/Touch ended
                 }
                 else if (t.phase == TouchPhase.Ended)
                 {
                     secondPressPos = t.position;
                     return true;
                     // Still swiping/touching
                 }
                 else
                 {
                     // Could count as a swipe if length is long enough
                     if (instance.triggerSwipeAtMinLength)
                     {
                         return true;
                     }
                 }
 
             }
     
             return false;
         }
     
         static bool GetMouseInput()
         {
             // print("Swipe/Click started");
             if (Input.GetMouseButtonDown(0))
             {
                 firstPressPos = (Vector2)Input.mousePosition;
                 swipeStartTime = Time.time;
                 swipeEnded = false;
                 // Swipe/Click ended
             }
             else if (Input.GetMouseButtonUp(0))
             {
                 secondPressPos = (Vector2)Input.mousePosition;
                 return true;
                 // Still swiping/clicking
             }
             else
             {
                 //// Could count as a swipe if length is long enough
                 //if (instance.triggerSwipeAtMinLength)
                 //{
                 //    return true;
                 //}
             }
     
             return false;
         }
     
         static bool IsDirection(Vector2 direction, Vector2 cardinalDirection)
         {
             var angle = instance.useEightDirections ? eightDirAngle : fourDirAngle;
             return Vector2.Dot(direction, cardinalDirection) > angle;
         }
     
         static Swipe GetSwipeDirByTouch(Vector2 currentSwipe)
         {
             currentSwipe.Normalize();
             var swipeDir = cardinalDirections.FirstOrDefault(dir => IsDirection(currentSwipe, dir.Value));
             return swipeDir.Key;
         }
     
         static bool IsSwipingDirection(Swipe swipeDir)
         {
             DetectSwipe();
      
             return swipeDirection == swipeDir;
         }
     
         #endregion
     }



this code is a component of my player and i implement it inside my player's script like that :

  public void Start()
         {
     
             SwipeManager.OnSwipeDetected += OnSwipeDetected;
     }
     
     void OnSwipeDetected(Swipe direction, Vector2 swipeVelocity)
         {
        
                 currentSwipeDir = direction;
            
         }
     
      public void Update()
         {
     
                 if (SwipeManager.IsSwipingUp() )
                 {
     
     /// do something
     }
     }




I know this could be difficult to answer but if someone know were i made a mistake it would be great to help because i have search by myself and i dont have any clue.

thanks for all !

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 fiersking_unity · May 28, 2018 at 01:12 PM

hi ! No idea ? :)

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

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

242 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect a swipe in ANY direction? 1 Answer

Unity 5 Movement swipe to left and right and up and down 0 Answers

How to change this script so that I can swipe up to jump and swipe down to come down on a touch device? 0 Answers

How can i move just one object ? 0 Answers

Problem with swipe and player jump 2 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