So far I've tried to run a for loop while detecting a touch, and call it a tap if under X loops and a touch for over. However, that did not work nearly as well as expected. Is there a built in way to detect a tap versus a sliding touch?
Answer by Bampf
Aug 31, 2010 at 02:45 AM
In my app I assume it's a tap unless the phase becomes Moved. So when the phase is Ended, if it never became Moved, then it was a tap. This works well for (say) a scrolling list that they can either drag, or tap on one of the lines.
You could add a time limit to the above logic by adding up all the deltaTime values after the Began phase. Once the time limit is exceeded you'd treat it the same as if they moved their finger.
Remember to reset your timer and other variables when the phases Ended or Cancelled occur.
An alternative I haven't tried is using iPhoneTouch.tapCount. By reading it you might be able to detect single-taps (when the count goes to 1, get the position of the tap.) The documentation is vague on the behavior but it sounds like it has some logic for deciding when the user tapped one or more times.
Answer by jtbentley
Aug 30, 2010 at 11:54 PM
A touch has several phases (TouchPhase)...
Began, Moved, Stationary, Ended, Canceled.
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) doStuff();
Answer by geo
Jan 18, 2011 at 02:12 PM
Unity does most of the work for you here, you need to look at the tapCount of the "Ended" touch (touch.phase = TouchPhase.Ended).
In my testing, I found that if the user pressed and released on the same spot (in under ~1 second) then the tapCount of the "Ended" touch is 1. If they have dragged at all in between pressing and releasing, the tapCount is always 0. Also if they press but hold on the spot for over a second and then release, the tapCount is also 0.
If the maximum tap duration of around 1 second is too long, you'd probably need write your own version of this functionality.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users post good questions and use the site effectively we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
What is the difference between iPhoneInput & Input ??
I'm trying to translate this code to work for touch devices
How do you map touch to one of the input axes?
Input.touchCount won't return 2 or more on iOS.
Why can't I press both my touch controls at the same time?