Detecting a "Tap" versus "Drag/Swipe"?

Hi guys.

Ok, question for the gurus.

I have a scene set up where I simply want to be able to select objects in a 3D scene and show the name of that object at the top of the screen. Not a problem. I can raycast to the object to identify it.

BUT, I also have a script which allows me to rotate the camera around by dragging your finger around the screen. The problem is, that my other script (that does the raycast, and shows the name of the ‘touched’ object) will keep detecting any object that are touched along the way.

I’d like the objects text to NOT show up when DRAGGED past. I’d only like the name to show up when TAPPED.

This might explain it better:

Is such a thing possible?

Thanks!

I’d guess check the distance between the finger down position and the finger up position, then decide whether or not to raycast at the finger up(to allow a slight bit of error).

Unity’s probably already got this covered, but you know me. Well, you don’t know me. I’m Joe. And I’m a bit ignorant. :stuck_out_tongue:

1 Like

Have you looked at iPhoneTouch.tapCount ?

Actually never mind that because for swipes, tapcount is always == 1.

Coincidentally i am working on the same problem for my game. A swipe will be “shuffle” and a single tap will be “pick”. The concept described in the Dev Center apply to Unity iPhone too, because the functions and structures for iPhoneTouch wrappers (or at least are analagous for CocoaTouch)

Sorry for triple posting! :wink:

Dev Center says "By looking for the tap count in the touch-up phase (UITouchPhaseEnded), you ensure that the finger is really tapping and not, for instance, touching down and then dragging. "

for (var event : iPhoneTouch in iPhoneInput.touches)
{			
	if(event.phase == iPhoneTouchPhase.Ended)
	{
		if(event.tapCount == 1)
		{
					Debug.Log("Single Tap!");
		}
}

to follow on from mindlubes post, a touch has 4 possible states (at least, can’t remember if there are more, and don’t have ref guide handy to check).

the states are-
iPhoneTouchPhase.Began
iPhoneTouchPhase.Stationary
iPhoneTouchPhase.Moving
iPhoneTouchPhase.Ended

So you can easily check the status of each touch, and get it to do something different depending on what’s happening.

mindlube - You the man! Works beautifully. I can now drag across the objects and NOT get them selected, but it still selects with a tap. Here is the code I used FYI:

static var currentObject:GameObject;

function Update() 
{ 
	var hit : RaycastHit; 
	
	for (var event : iPhoneTouch in iPhoneInput.touches) 
	{          
	   if(event.phase == iPhoneTouchPhase.Ended) 
	   { 
	      if(event.tapCount == 1) 
	      { 
	            var ray = Camera.main.ScreenPointToRay (event.position); 
	            if (Physics.Raycast (ray, hit, 500)) 
	            { 
	               Debug.DrawLine (ray.origin, hit.point, Color.red); 
	 		       currentObject = hit.collider.gameObject;
	            }                
	      } 
	   }
	}
}
for (var event : iPhoneTouch in iPhoneInput.touches)
{         
   if(event.phase == iPhoneTouchPhase.Ended)
   {
      if(event.tapCount == 1)
      {
          Debug.Log("Single Tap!");
      }
}

I cannot get event.tapCount == 1 for one finger. It returns 0 for one finger and 1 for two fingers. Why is it happening so :?:

I tried to move an object with your script, but nothing happens, I tried to put the script in the main camera, in a cube , but nothing … you can give me some help?

Hi, guys,

Does anyone have a good example of a swipe to move script? I’m looking to move my camera in the direction of the swipe gesture.

Thanks,

Greg

This script only detects which objects was ‘tapped’, it doesn’t do any movement.

After you get the game object you tapped on you can do whatever you want/need with it.