- Home /
Tap detection on Android
Hi
I'm trying to get a "tap anywhere to drop the cargo" behavior while at the same time allowing for camera movement using swipes.
What I have works perfectly on some devices, on and off on some others, and not at all on a few devices, which is very annoying. Am I not considering something here? Here's what I've got so far:
void Update()
{
if (1 == Input.touchCount)
{
switch (Input.GetTouch(0).phase)
{
case TouchPhase.Began:
{
buttons[0].wasDownLastFrame = true;
buttons[0].origin = Input.GetTouch(0).position;
clickWindow[0] = Time.realtimeSinceStartup + clickTimeThreshold;
dragWindow[0] = Time.realtimeSinceStartup + dragTimeThreshold;
mayClick = true;
break;
}
case TouchPhase.Ended:
{
if (mayClick && !hasDraggedTouch && !InputUtilities.IsPointerOverUIObject() /* && Time.realtimeSinceStartup >= clickWindow[0]*/)
{
MessageDispatcher.Instance.DispatchEvent(new MouseClickEvent(0));
}
MessageDispatcher.Instance.DispatchEvent(new TouchDoneEvent());
hasDraggedTouch = false;
mayClick = true;
break;
}
case TouchPhase.Moved:
{
if (Mathf.Abs (Input.GetTouch(0).deltaPosition.x) > dragPixelThreshold && Time.realtimeSinceStartup >= dragWindow[0])
{
TouchDelta = Input.GetTouch(0).deltaPosition;
MessageDispatcher.Instance.DispatchEvent(new TouchSwipingEvent());
hasDraggedTouch = true;
}
mayClick = false;
break;
}
default:
{
break;
}
}
lastTouchCount = 1;
}
else if (2 == Input.touchCount)
{
mayClick = false;
if (TouchPhase.Moved == Input.GetTouch(0).phase || TouchPhase.Moved == Input.GetTouch(1).phase)
{
MessageDispatcher.Instance.DispatchEvent(new TouchPinchingEvent());
}
else if (TouchPhase.Ended == Input.GetTouch(0).phase ^ TouchPhase.Ended == Input.GetTouch(1).phase)
{
MessageDispatcher.Instance.DispatchEvent(new TouchDoneEvent());
}
lastTouchCount = 2;
}
else if (0 == Input.touchCount && lastTouchCount != 0)
{
mayClick = false;
MessageDispatcher.Instance.DispatchEvent(new TouchDoneEvent());
lastTouchCount = 0;
}
}
I think removing the mayClick
variable will fix your problem, it's overcomplicating the code. Or if you really need it call it something that relates to why the click shouldn't be processed like multipleTouchReceived
(in case of the second if statement). You are already doing it by having hasDraggedTouch
around.
That's one of my big problems - checking / changing the same bool in 10 different if statements and than wondering were does the logic fail.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072722im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Differentiating between a tap from a swipe on Android 2 Answers
UI Buttons not always working on Android? 0 Answers
Swipe menu, problem! 0 Answers
Sliding Finger(touch) gesture? 1 Answer
Detect touches on the top right corner of the screen 1 Answer