Detecting a screen tap on mobile devices

I’m trying to detect if a user has tapped the screen on a mobile device by getting the mouse position when a tap starts and ends, then if the difference between the 2 values is within a threshold, I count it as a tap. Is there a better way to do this?

Setting the threshold to .5 works fine on my android phone, but on my ipod, the difference can be as high as 20 and I haven’t even move my finger! But I can slightly drag my finger on my android and get under 20, so that’s too large a threshold for my android.

The ipod has a slightly higher resolution (~13%), but that’s not nearly enough to require such a huge threshold difference. Is there a better way to detect tapping a screen?

Why reinvent the wheel? Unity already has a built in system for detecting taps.

If the numbers are really far off the scale it MAY be that the iphone is inconsistent in the order of touches, so you need to sort things by Touch.FingerId manually because the list of Input.touches gets shuffled all the time.

Doing this yourself is not too hard. Assuming you have something like this:

//default to android / any other platform
const float SOME_TOLERANCE = 5;
#if UNITY_IPHONE
const float SOME_TOLERANCE = 40;
#endif

List<Vector2> startVecs = new List<Vector2>();
List<Vector2> dragVecs = new List<Vector2>();

//iterate reverse so we can remove entries
foreach( int i = Input.touches.Length-1 ; i >= 0 ; --i ) 
{
    Touch touch = Input.touches*;*

//you track the phase of the touch, you can store the position at TouchPhase.Began as start position and while phase != TouchPhase.Ended you can consider the touch as held or dragging and track the position-startPosition as draggingVector.
if( touch.phase == TouchPhase.Began )
{
if( i >= startVecs.Count )
{
startVecs.Add(touch.position);
dragVecs.Add(Vector2.zero);
}
else
{
startVecs = touch.position;
dragVecs = Vector2.zero;
}
}

dragVecs = touch.position - startVecs*;*

if( touch.phase == TouchPhase.End )
{
if( draggingVector.sqrMagnitude < SOME_TOLERANCE )
{
//it was a release without dragging, hence a tap (which could’ve been held for ages if the user wants to, he just pressed and released on the same place)
}
else
{
//it was a dragging operation
}
}
if( touch.phase == TouchPhase.End || touch.phase == TouchPhase.Canceled )
{
startVecs.RemoveAt(i);
dragVecs.RemoveAt(i);
}
}