Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Button Registers Click When Scrolling

Discussion in 'Scripting' started by Frostbite23, Jun 18, 2015.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Hello, I asked this similar question a while back and the person provided a timer solution to measure the duration of a tap. I have this system in but that didn't fix anything what so ever. (This is on mobile, so consider the clicking as tapping)

    What my issue is, is that I have a 3d carousel. No I'm not using Unity's UI, though I tried implementing it into my system but I had no luck. Now somehow unity manages to solve this button clicking issue with there UI where when you move your finger to scroll, and your finger is over a button, the button isn't registering as a click. Then when you stop scrolling and tap on a button the button will register that tap as a click then it does its process. Now I'm struggling so much with this issue because Ive been stuck on it for a while now. If you need a simplified question then here it is.

    When I scroll up or down with my finger on my carousel, the button which I scroll my finger on will register that tap and then call the function. This is something I don't want. How can I fix this? (Without using unitys UI).

    Now the solution which I have in mind right now and Im trying is instead of one script handling the button scrolling and tap checker. I think ill have the code in the buttons instead of one script handling everything. Also having a movement checker to where if the finger moves over a certain threshold then it won't register as a click, Im implementing this solution right now but Im not sure if it will work and I have high doubts it won't work (Yes, I doubt myself everytime when I try to fix something and it ends up working. Probably a habit that is created because of such errors when scripting LOL :p )

    But please, can you help me? Providing a script, line of code, example code, or a detailed suggestion will help me out tremendously. Referring to an asset or thread is not helpful. (No I'm not lazy)
     
    Last edited: Jun 18, 2015
  2. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Only call the function on the finger being released (like a mouse up event) and if the carousel didn't scroll by a significant margin? Just like an operating system, when you click and hold on a button and move the mouse off, the button isn't clicked.
     
    Frostbite23 likes this.
  4. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Ok Ill try that.
     
  5. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Yes!!!! It finally works!!! Thank you @GroZZIeR . Id never thought about that on Finger Release. I kept using Finger Down but thank you! :)

    For those of you who are stuck on a similar problem, what I did is have two checkers, a duration and a distance checker. Get two threshold variables for the touch duration and the distance checker. In the update code, put something like this (not guaranteed to work if you copy paste).

    Code (JavaScript):
    1.         var distanceCheckY : float = Mathf.Abs(lastTouch.y) - Mathf.Abs(firstTouch.y);
    2.         var tapTimer : float = lastTouchedTime - firstTouchedTime;
    3.         distanceCheckY = Mathf.Abs(distanceCheckY);
    4.      
    5.         if(distanceCheckY <= distThreshold){
    6.             tapped = true;
    7.         }
    8.         else {
    9.             tapped = false;
    10.         }
    11.      
    12.         if(tapTimer <= tapThreshold){
    13.             tapperBool = true;
    14.         }
    15.         else {
    16.             tapperBool = false;
    17.         }
    18.      
    19.         if(tapperBool && tapped){
    20.             callFunction = true;
    21.         }
    22.         else {
    23.             callFunction = false;
    24.         }
    25.  
    and for your touch phases you would do
    Code (JavaScript):
    1.         if(touch.phase == TouchPhase.Began){
    2.             firstTouch = touch.position;
    3.             lastTouch = touch.position;
    4.          
    5.             lastTouchedTime = Time.time;
    6.             firstTouchedTime = firstTouchedTime;
    7.             firstBool = false;
    8.         }
    9.      
    10.         if(touch.phase == TouchPhase.Moved){ //if u uncomment touchphase begin, put else before if on this line (REMINDER)      
    11.             firstTouch = firstTouch;
    12.             lastTouch = touch.position;
    13.          
    14.             lastTouchedTime = Time.time;
    15.             firstTouchedTime = firstTouchedTime;
    16.         }
    17.      
    18.         else if(touch.phase == TouchPhase.Stationary){
    19.             firstTouch = firstTouch;
    20.             lastTouch = touch.position;
    21.          
    22.             lastTouchedTime = Time.time;
    23.             firstTouchedTime = firstTouchedTime;
    24.         }
    25.      
    26.         else if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
    27.             firstTouch = touch.position;
    28.             lastTouch = lastTouch;
    29.          
    30.             lastTouchedTime = lastTouchedTime;
    31.             firstTouchedTime = firstTouchedTime;
    32.             firstBool = true;
    33.         }
     
    GroZZleR likes this.