Multi-Touch Drag Object

OK,

I’ve searched every answer, every thread, read all the documents and I still can’t figure this out.

All I want the script to do is allow the user to drag multiple spheres with individual fingers at the same time to different positions on the screen.

And it seems like it should be so easy.

I wrote this code for dragging objects in iOS with multi-touch.

var obj : GameObject; 
private var objposition : Transform = null; 
private var selected : boolean = false; 


function Awake() { 
Input.multiTouchEnabled = true;
if (obj) { 
   objposition = obj.transform; 
   }
}


function FixedUpdate () {
if (obj) { 
   
var cam : Camera = Camera.main; 
  for (var touch : Touch in Input.touches) {
       var ray = Camera.main.ScreenPointToRay(touch.position); 
       var hit : RaycastHit; 

  if (touch.phase == TouchPhase.Began) { 
     if (Physics.Raycast(ray, hit, 100)) { 
        if (hit.collider.gameObject == obj) { 
            selected = true; 
    } 
  } 
} 
   else if (touch.phase == TouchPhase.Moved) { 
      if (selected == true) { 
         var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0); 
         var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z)); 
         objposition.position.x = touchPosition.x; 
         objposition.position.y = touchPosition.y;
                       
  } 
} 
   else if (touch.phase == TouchPhase.Ended) {
      if (selected == true) {
         selected = false;      
        } 
      } 
    } 
  }
}

This code should work, but it doesn’t.

The objects move but Multi-touch doesn’t work so you can’t move more than one at a time.

Could someone please point out whats wrong with this or what I should do. I’m completely stumped.
Yes, I’m strait up just asking for help.
And of course thank you.

I also wanted to post this so there will be a Multi-touch drag object script out there for anyone who runs into the same problem and needs it. That’s why I put it on UForum instead of UAnswers.

I THINK (straining brain) that you should try to assign ids to each finger touch. I know there’s code out there on this – i’ve seen it ages back - but can’t recall right now where. If I find it i’ll post a link. Have you tried Googling “Multitouch Unity”? Google is your best friend.

You mean something like that? :wink:

I’ve recently released my FingerGestures scripting package on the asset store that will let you do just that. The sample scene (that I’ve linked above) shows you how to use the library to drag multiple spheres around (among other interactions). You can get more information on this scripting package in its dedicated forum thread. This works with both C# and Javascript.

As far as your code is concerned, you will need to add some logic to track which finger is dragging which object (with touch.fingerId) and managing each one individually. I think the issue with your code right now is that you assume the finger is still above the object you’re dragging when you get the TouchPhase.Moved, while this is not guaranteed at all. The finger might have moved quite far away from the original object and your raycast in TouchPhase.Moved will fail to find it.

The way I solve this in FingerGestures is to detect when the drag operation starts, and create a mapping between the finger and this object. Then in the Moved event for this finger, I look up which object it was dragging with this map (that is, i’m not doing a raycast) and then proceed to update this object’s position to follow the finger’s new position.

Thanks guys. I’ll look into what you said.

That touch.fingerId for tracking individual fingers is probably what I’m missing.

And if you do find or think of that script please post it.:slight_smile:

Of course if all else fails I’ll just buy that awesome looking unity package that Spk posted.

Bump

bump…