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

Why does this touch test not work?

Discussion in 'Scripting' started by elpuerco63, Mar 20, 2017.

  1. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I cannot see why this touch test does not work to ensure it only processes a touch not over an UI object.

    Code (csharp):
    1.  
    2. if (Input.touchCount > 0) {
    3.  
    4.     Touch touch = Input.GetTouch (0);
    5.  
    6.     if (touch.phase == TouchPhase.Began) {
    7.  
    8.         Debug.Log("touch began");
    9.  
    10.         tappedUI = false;
    11.  
    12.         if (EventSystem.current.IsPointerOverGameObject (touch.fingerId)) {
    13.  
    14.             tappedUI = true;
    15.         }
    16.      
    17.         tapComplete = false;
    18.     }
    19.     else if (touch.phase == TouchPhase.Ended) {
    20.  
    21.        Debug.Log("touch ended");
    22.  
    23.         tapComplete = true;
    24.     }
    25.  
    26.     if (tapComplete && !tappedUI) {
    27.  
    28.       // this code will only run if tap complete and not over UI
    29.     }
    30.     else {
    31.  
    32.        Debug.Log("tapComplete and tappedUI tested false");
    33.     }
    34. }
    35.  
    When I run and tap I get this in the console:

    got touch
    touch began
    tapComplete and tappedUI tested false
    got touch
    tapComplete and tappedUI tested false
    got touch
    tapComplete and tappedUI tested false
    got touch
    tapComplete and tappedUI tested false
    got touch
    tapComplete and tappedUI tested false
    got touch
    touch ended

    so basically the touch is never processed, even though the touch is not over a UI?
     
    Last edited: Mar 20, 2017
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Last edited: Mar 20, 2017
    elpuerco63 likes this.
  3. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    So, your results seem to be from holding down the "touch". But when your touch begins, you set tapComplete to false, so while holding down, the result is always false. It's only true when you lift your finger off.

    Seems to be working. After touch ended, you don't get a print out because you don't have anything in the if part of your bool check.
     
  5. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Sorry, that was me clipping the code for clarity, in my project I do have code that tests with raycast for touch on object. You have directed me to where the actual problem is, thanks, but now I have that real problem as I do not see why my mouse code that uses the same raycast method works on the objects but when using touch it passes through the game object below?
     
  6. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Turns out it was a Unity upgrade issue! Found it was working fine in 5.5.1 but in 5.5.2p3 even though none of that code had changed did not?

    I opened the project and recompile to 5.5.1 and all works :)