- Home /
Double Tap Android runs both single and double tap
Take a look at code below. If I have one touch which registers 1 tap, it runs only the the do_single_tap function, which Is what I expect. But when I do a doubletap, it runs both parts of the code. After many hours struggling with this I am assuming:
The touch stays in the TouchPhase.Began state. Becasue it gets continually called by update, it will detect both tapCount==1 and then tapCount==2 etc, so both code segments run. If this is the case, how do we break out or set a flag to say that touch has been processed and do nothing more with it on the next update. Or does it change states whilst tapping ? Or am I over thinking it ? Confused !!!
using UnityEngine;
using System.Collections;
public class monitortouches : MonoBehaviour {
public float tapSpeed=0.5f;
public GameObject e;
float buttonCooler=0.5f;
int touchCount;
int doubletap=0;
int singletap=0;
int total_taps=0;
int touches=0;
private float dt=0.3f;
// Use this for initialization
void Start () {
bool sm = Input.multiTouchEnabled;
Debug.Log ("multitouch is " + sm);
}
// Update is called once per frame
void Update () {
touchCount = Input.touchCount;
foreach (Touch touch in Input.touches) {
TouchPhase p = touch.phase;
touches++;
if (p == TouchPhase.Began)
{
if (touch.tapCount == 2)
{
print ("double tap");
doubletap++;
} else if (touch.tapCount == 1) {
do_single_tap ();
}
}
}
Debug.Log ("you just had " + touches);
touches = 0;
Debug.Log ("toal taps: " + total_taps);
Debug.Log ("single tpas "+singletap);
Debug.Log ("double tpas "+doubletap);
}
void do_single_tap()
{
singletap++;
RaycastHit hit;
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit, 10000.0f))
if (hit.transform.gameObject.name!="Plane")
{
//Instantiate (e,hit.transform.position,Quaternion.identity);
Destroy (hit.transform.gameObject);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612042928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Does android supports double tap? 1 Answer
Problem With touch and double touch 0 Answers
Press Back Button Twice to Quit Game 0 Answers
Not able to publish android application from unity. 4 Answers