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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by hulahoolgames · Jun 25, 2015 at 01:00 PM · uiraycasttouches

Avoid button touches to be registered as swipe.

 using UnityEngine;
 using System.Collections;
 
 public class TouchEventManager : MonoBehaviour {
 
     public float MinimumSwipeDist;
     public float ThresholdAngle;
     public delegate void GestureEventHandler();
     public static event GestureEventHandler OnSwipeRight;
     public static event GestureEventHandler OnSwipeLeft;
     public static event GestureEventHandler OnSwipeUp;
     public static event GestureEventHandler OnSwipeDown;
 
     private Vector2 mStartPos;
     
     // Update is called once per frame
     void Update () {
         if (Input.touchCount > 0) {
             Touch touch = Input.touches[0];
             
             switch(touch.phase) {
             case TouchPhase.Began:
                 mStartPos = touch.position;
                 break;
                 
             case TouchPhase.Ended:
                 float swipeDist = (touch.position - mStartPos).magnitude;
                 // check if legal swipe
                 if(swipeDist > MinimumSwipeDist) {
                     determineSwipeType(touch.position);
                 }
                 break;
             }
         }
     }
 
     void determineSwipeType(Vector2 endPosition) {
         float angle = Vector2.Angle (Vector2.right, (endPosition - mStartPos));
 
         if (angle < ThresholdAngle) {
             // right swipe
             if(OnSwipeRight != null) {
                 OnSwipeRight();
             }
         } else if(angle >= ThresholdAngle && angle <= 180 - ThresholdAngle) {
             float swipeValue = Mathf.Sign(endPosition.y - mStartPos.y);
             if(swipeValue > 0) {
                 // up swipe
                 if(OnSwipeUp != null) {
                     OnSwipeUp();
                 }
             } else {
                 // down swipe
                 if(OnSwipeDown != null) {
                     OnSwipeDown();
                 }
             }
         } else if(angle >= 180 - ThresholdAngle){
             // left swipe
             if(OnSwipeLeft != null) {
                 OnSwipeLeft();
             }
         }
     }
 }

I have the above code to detect swipe gesture in my game. I recently implemented some UI using the new UI system introduced in v4.6. I have a pause/resume button during gameplay and when I click on the buttons and make a small swipe motion while clicking, my swipe gesture code registers it and affects the gameplay. I have read, that I can do a raycast in my swipe gesture code to check if the touch hits any UI elements and if so not consider it as a swipe but I wanted to know if thats the standard way of doing it or I can somehow block events to my swipe code. I use Input.Touches in my swipe recognition which makes me believe that I will always have the touches in there even on UI buttons and might have to do some custom checking. If anyone knows of a better solution let me know. Also, feel free to use this code for your projects if anyone wants to. Thank you.

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 hulahoolgames · Aug 28, 2015 at 06:26 PM

I solved this by casting a ray from the start position of the swipe and checking if its above any UI element. If the swipe starts above a UI element its discarded. Here is the piece of code that does this

 private bool shouldDiscardSwipe(Vector2 touchPos) {
             PointerEventData touch = new    PointerEventData(EventSystem.current);
             touch.position = touchPos;
             List<RaycastResult> hits = new List<RaycastResult>();
             EventSystem.current.RaycastAll(touch, hits);
             return (hits.Count > 0); // discard swipe if an UI element is beneath
         }

   

Hope this helps someone.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Click event on GameObject to open GUI also fires a click event on the GUI 0 Answers

Worldspace Scroll View with Buttons in VR (Gear) 0 Answers

GraphicRaycaster not hitting TextMeshPro Dropdown popup 2 Answers

IsPointOverGameObject() not working as intended!!! 2 Answers

How to prevent clicking Gameobjects behind a Canvas/Panel? 4 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