Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android and iOS Multi-Touch

Discussion in 'Scripting' started by danBuonocore, Apr 25, 2016.

  1. danBuonocore

    danBuonocore

    Joined:
    Aug 26, 2015
    Posts:
    12
    I apologize if this isn't the best place to be posting this... I'm developing a game for Android and iOS devices that requires the player to tap individual objects on the screen as they appear. Missing the object results in losing the game.

    Now, I have one feature I'd like to include where multiple objects appear at once, and the player must tap them both simultaneously. I've written some basic 2D Raycasting code to detect this, and it seems to work perfectly... sometimes...

    It seems some times the multi-tap is registered and all goes well, other times it seems a third tap is registered halfway between the two touch points, and since this point does not collide with the two objects, this results in the player losing.

    To be more specific, here is a generic version of the code I am using to detect multiple taps.

    Code (CSharp):
    1.  
    2. if (Input.touchCount == 2) {
    3.  
    4.     if (Input.GetTouch(0).phase != TouchPhase.Stationary ||
    5.         Input.GetTouch(1).phase != TouchPhase.Stationary)
    6.         return;
    7.  
    8.     Vector2 v1 = Camera.main.ScreenToWorldPoint(new Vector3(
    9.         Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
    10.  
    11.     Vector2 v2 = Camera.main.ScreenToWorldPoint(new Vector3(
    12.         Input.GetTouch(1).position.x, Input.GetTouch(1).position.y));
    13.  
    14.     RaycastHit2D h1 = Physics2D.Raycast(v1, Vector2.zero);
    15.     RaycastHit2D h2 = Physics2D.Raycast(v2, Vector2.zero);
    16.  
    17.     if (h1.transform != null && h2.transform != null) {
    18.  
    19.         // TODO: do stuff
    20.  
    21.     }
    22.  
    23. }
    24.  

    Single taps are handled in the individual object's OnMouseDown functions, while multiple taps are handled in a single Update method for all objects.

    Looking back, I think I can fix the issue of the player losing when this third tap is registered by ensuring there is only one touch point in the OnMouseDown function, or moving the code to this Update function and doing it all with Raycasting. But I guess my real question is, how reliable is this? Is there a way to ensure the game is only available for download from Android and iOS devices that support two distinct touch points? I remember reading somewhere that the hardware design of certain mobile touchscreens don't account for multiple touches if they are on the same row or column, and I obviously need this to work 100% of the time.

    Could you point me to some resources on this? Or am I better off leaving out this feature? Thanks!

    EDIT: The game has been testing on a Samsung Galaxy S3 API level 19 and an iPad Mini iOS 9.2. They have identical results.
     
    Last edited: Apr 25, 2016
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    What evidence leads you to the "third tap" conclusion here? If the code snippet posted is all that controls this system, how could a "third tap" do anything at all, given that it won't run if the number of taps is anything except exactly two?

    Might be able to get some better information with some Debug.Logs like:
    Code (csharp):
    1. if (Input.touchCount > 0) {
    2. string output = "Touches ("+Input.touchCount+"): ";
    3. foreach (Touch t in Input.touches) {
    4. output += t.phase.ToString() + " "+ t.position.ToString()+"  .....  ";
    5. }
    6. Debug.Log(output);
    7. }
    The output lines will appear in your phone's console, and this could give you somewhat better information regarding what's really going on.
     
  3. danBuonocore

    danBuonocore

    Joined:
    Aug 26, 2015
    Posts:
    12
    Sorry that probably wasn't the best way to explain it. What I mean is that instead of registering the two taps, only one is registered. And the location of the one registered is directly between where the two physical taps actually are.

    As I stated above, single taps are handled elsewhere. The fact that the the single-tap code is being called and not the double-tap code I posted above already proves that only one is being registered.