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

Question Touch controls: dragging finger interpreted as double tap of screen?

Discussion in 'Scripting' started by wileyjerkins, Jul 29, 2021.

  1. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Using touch controls for the first time, and wow I really miss joysticks! But kids these days use phones, and I making a 2D game to entertain a friend's kid ... so ...

    The problem I have is I added a "double tap the screen" to pause the game function, but when the player is dragging his/her finger around to otherwise play the game, it randomly pauses the game.

    Anyone have any tips for preventing the two actions from getting confused? Here is my code ... and before you mock me understand I have not fiddled with touch controls before! Or certainly not lately.

    Thanks for any assistance!

    Tap twice to pause (sometimes fires when a finger is dragging the screen)
    Code (CSharp):
    1.  
    2. private void Taps()
    3.     {
    4.         for (var i = 0; i < Input.touchCount; i++)
    5.         {
    6.             if (Input.GetTouch(i).phase == TouchPhase.Began)
    7.             {
    8.                 if (Input.GetTouch(i).tapCount == 2)
    9.                 {
    10.                     PauseToggle();
    11.                 }
    12.             }
    13.         }
    14.     }
    15.  
    Drag to control game (works fine until it randomly pauses the game)
    Code (CSharp):
    1. private void TouchMove() //touchscreen
    2.     {
    3.         if (Input.touchCount > 0)
    4.         {
    5.             touch = Input.GetTouch(0);
    6.             touchPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, startY + 100f, 10));
    7.             transform.position = touchPos;
    8.         }
    9.     }
     
    Last edited: Jul 29, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    After nearly a decade of using Unity on touch, today I learned that there is a
    Touch.tapCount
    field. Wow. That's why I play in the forums.

    So clearly, I never use tapCount !

    For a double-tap situation like what you're doing, you kinda want to detect two releases that are close to each other in time, not so much two taps. If you think through it you will sorta see how it is generally better.

    For that I always use my own timer.

    - make a float timer class variable

    Each frame (eg, in Update(); NEVER ask for touches in FixedUpdate!!!):

    - if the timer is greater than zero, count it down by Time.deltaTime

    - when a release is detected:
    ---> if timer greater than zero, do the pause
    ---> else reset the timer back to its "desired tap interval" (usually 0.6f seconds works nicely)

    That's it.
     
    wileyjerkins likes this.
  3. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Thanks! I had thought of that but considered it "hacky" ... but if it works, it is GENIUS!
     
    Kurt-Dekker likes this.