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

When i double Tap single Tap function also Executing..

Discussion in 'Scripting' started by babji3, Jul 15, 2015.

  1. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi all,
    I need help in this , I write jump function for Single touch, and Double touch for other function,
    here the problem is when i double touch the single touch function also executing , here is my code.

    Code (CSharp):
    1.  
    2. void Update ()
    3.     {
    4.         //
    5.         for(int i = 0; i < Input.touchCount; i++)
    6.             {
    7.                 if(Input.GetTouch(i).phase == TouchPhase.Began)
    8.                 {
    9.                    
    10.                 if(Input.GetTouch(i).tapCount == 1)
    11.                 {
    12.                     if(isGameStart==true)
    13.                     {
    14.                         Jump();
    15.                         _animator.enabled=true;
    16.                        
    17.                        
    18.                         if (!this._animator.GetCurrentAnimatorStateInfo (0).IsName ("jumping1"))
    19.                         {
    20.                             _animator.SetBool("jumping",false);
    21.                         }
    22.                         else
    23.                         {
    24.                             _animator.SetBool("jumping",true);
    25.                         }
    26.                     }
    27.                 }
    28.                 if(Input.GetTouch(i).tapCount == 2)
    29.                     {
    30.                         print("Double Touch");
    31.                         StartCoroutine(SlowMotion());
    32.                     }
    33.                 }
    34.             }
    35.         }
    36.  
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Move the tapcount 2 if-statement above the tapcount 1, and change the tapcount 1 to an else-if.
     
  3. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
  4. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    i think it's better like this
    Code (CSharp):
    1. if(Input.GetTouch(i).tapCount == 2)
    2. {
    3. //Double Tap
    4. }
    5. else if(Input.GetTouch(i).tapCount == 1)
    6. {
    7. //Tap
    8. }
    9.  
     
  5. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    ya , ill try this.
     
  6. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    No this one also not working.
    Same problem. Any other solution?
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    check how many touches you have... I would kinda expect the first tap to make one touch (tapcount 1) and the second tap to make another touch (with tapcount 2). Since you're looping through all touches you are triggering both functions. One on the first touch, the other on the second. It's not logically possible for @kemar 's code to execute both branches with the same i value afterall.
     
  8. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Ya thats why i post this question here, i dont know how to loop?
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you wrote the code in the OP you already are, the for loop goes through each touch...
     
  10. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Ya i know, What is the solution plz, or any other way for single tap and double tap?
     
  11. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Here's how i would do it, not saying that it's good, but it could work (probably)

    Code (CSharp):
    1.  
    2. private float doubleTapTimer = 0.3;   //How much time we allow for the double tap
    3. void Update ()
    4. {
    5.   for(int i = 0; i < Input.touchCount; i++)
    6.   {
    7.       if(Input.GetTouch(i).phase == TouchPhase.Began)
    8.       {
    9.         if(Input.GetTouch(i).tapCount == 1)
    10.         {
    11.           Invoke("singleTapAction", doubleTapTimer);
    12.         }
    13.         if(Input.GetTouch(i).tapCount == 2)
    14.         {
    15.             print("Double Touch");
    16.             CancelInvoke("singleTapAction");
    17.             StartCoroutine(SlowMotion());
    18.         }
    19.       }
    20.   }
    21. }
    22.        
    23. void singleTapAction(){
    24.   if(isGameStart==true)
    25.   {
    26.       Jump();
    27.       _animator.enabled=true;
    28.    
    29.    
    30.       if (!this._animator.GetCurrentAnimatorStateInfo (0).IsName ("jumping1"))
    31.       {
    32.           _animator.SetBool("jumping",false);
    33.       }
    34.       else
    35.       {
    36.           _animator.SetBool("jumping",true);
    37.       }
    38.   }
    39. }
    40.  
     
  12. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    ok ill try this.