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

Bizarre behaviour with Touch Inputs

Discussion in 'Scripting' started by UlfvonEschlauer, Dec 3, 2014.

  1. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Hey everyone!
    I am somewhat new to Unity and I am currently struggling with the implementation of touch screen controls for my camera which already is working fine with mouse inputs.
    To test what is going on, I wrote a very simple script and attached it to my camera:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TouchinputTest : MonoBehaviour
    5. {
    6.     void Start ()
    7.     {
    8.         if(Input.multiTouchEnabled)
    9.         {
    10.             // this happens in the editor, but not in the build?!
    11.             camera.backgroundColor = Color.magenta;
    12.             Debug.Log("No Touchscreen Device");
    13.         }
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         int fingerCount = 0;
    19.         foreach (Touch touch in Input.touches)
    20.         {
    21.             if (touch.phase != TouchPhase.Ended
    22.              && touch.phase != TouchPhase.Canceled)
    23.             {
    24.                 fingerCount++;
    25.             }
    26.         }
    27.         if (fingerCount > 0)
    28.         {
    29.             //this never happens
    30.             camera.backgroundColor = Color.red;
    31.         }
    32.         if (Input.GetMouseButtonDown (0))
    33.         {
    34.             //this works fine
    35.             camera.backgroundColor = Color.green;
    36.         }
    37.         if (Input.GetMouseButtonDown (1))
    38.         {
    39.             //this works fine as well
    40.             camera.backgroundColor = Color.blue;
    41.         }
    42.     }
    43. }
    So when I run the above code in the Unity Editor, the backdrop gets magenta, even though my development machine is a Windows 8 laptop that does not have a touch screen (but has a multi touch capable touch pad if that matters). When I run the code in the actual release (or development ) build, it never gets purple, not even a test Windows 8 device with (multi- ) touch screen.
    The color change in line 30 never happens on any of my two test devices, neither in the Editor nor the build.
    I added a color change for mouse button presses for a sanity check and that seems to work fine. Most interestingly, this mouse input color change seems to also get executed when I touch the screen on the touch screen laptop.
    But even though it seems to interpret the touch as a button press, touch- drag does not seem to work in the actual camera control code.
    Even more interestingly the default(?) pinch multi touch behavior for the zoom seems to be working somehow (I guess it somehow gets mapped to the mouse wheel?).
    Anyway, all this is super bizarre and I cant make any sense of it. Could it be that the touch device on my Windows 8 test machine is not configured correctly? Am I missing something somewhere? Is there a setting for build, input, etc that needs to be set?
    Thanks in advance for your help!
     
    Last edited: Dec 3, 2014
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    What happens if instead of:
    Code (CSharp):
    1. int fingerCount = 0;
    2.         foreach (Touch touch in Input.touches)
    3.         {
    4.             if (touch.phase != TouchPhase.Ended
    5.              && touch.phase != TouchPhase.Canceled)
    6.             {
    7.                 fingerCount++;
    8.             }
    9.         }
    10.         if (fingerCount > 0)
    11.         {
    12.             //this never happens
    13.             camera.backgroundColor = Color.red;
    14.         }
    You use:
    Code (CSharp):
    1.  
    2.         if (Input.touchCount > 0)
    3.         {
    4.             camera.backgroundColor = Color.red;
    5.         }
     
    UlfvonEschlauer likes this.
  3. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Thanks for the quick reply! Unfortunately your suggestion did not change anything. It still only turns green, when I touch the screen (as if this was a mouse input and not a touch input).
     
  4. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    What happens if you remove everything but the two mouse button checks? I believe you should get a blue screen with two touches when using mouse inputs on a touch screen, simulating a right-click.

    The code works fine when on my tests. The screen does go magenta in the editor, but the touch is detected on my Android device (red screen). If I do as I suggested above I get a green screen with one touch and a blue screen with two. I also get a magenta screen on startup.
     
    UlfvonEschlauer likes this.
  5. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Hmm, interesting. Maybe it just does not work on Windows8 touch screens? I can never get a red screen, when I touch the Windows 8 laptop!
    Removing everything but the mouse inputs would not help. I started implementing the touch controls, because my mouse controls would not work with a touch screen. Somehow, this is leading me around in circles and I am starting to wonder whether there is something fundamental that I am missing.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    As far as I know, multi-touch support only works on mobile devices.

    --Eric
     
    UlfvonEschlauer likes this.
  7. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    The reason I'd try the mouse controls is just to check whether or not the device is picking up the inputs as expected.

    If Eric is right, then I'd assume that having two active touches wouldn't trigger the right-click input as it usually does on mobile devices.
     
    UlfvonEschlauer likes this.
  8. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    I can get left and right click Mouse Button presses from the touch screen (blue and green screen changes). Right click works when I touch the screen and keep holding it for a few seconds. I can even do a pinch which somehow is interpreted as a scroll wheel movement that I set up for my camera zoom. But what does not work is left click plus drag. I get the "LMB press" to register when I touch the screen, but the drag does not seem to do anything. That is why I tried to implement real touch controls, but they don't seem to work either, because the device does not even seem to register as a touch screen (even though it clearly is).
     
  9. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    That would be quite disappointing :(
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I think computer touch screens are just seen as mice, basically, but I'm not 100% sure.

    --Eric
     
  11. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Hmmm, if that was the case, then I would be able to control my camera with the touch screen just fine with the very code I have been using for mouse controls, but it does not work :(
     
  12. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Alright, so I got it to work!
    The correct answer is: You have to build and deploy it as a Windows Store App.
    Then it works with the touch screen on the Windows 8 laptop. That whole process of building for Windows Store is a major pain, to say the least! It makes development (and deployment of limited, personal releases) a very slow and painful process. So if there was a way to make touch work with Windows Desktop applications, I would be much happier. Anyone here, who can add something to this?
    Thanks!
     
  13. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    So, another update. I am happy to report that I got the touch inputs to work with Valentin Simonov's awesome (and free) TouchScript
    https://github.com/TouchScript/TouchScript
    Much better than the native capabilities and it works with desktop applications for Windows 7 and Windows 8 (and all other devices).
    So for everyone who discovers this thread whilst looking for a solution for the same problem, this is the best solution, I have found so far.
    Thanks to everyone who tried to help! I think this thread can be closed now.
     
    GetUpKidAK likes this.