Question by 
               rico_tom · May 30, 2017 at 05:18 AM · 
                c#multitouchdraggingmultiple objectstouchphase  
              
 
              How to drag multiple object simultaneously???
i am trying to drag 2 to 3 cube simultaneously by multiple finger at one time, but i am not able to perform this process. Please suggest me to how to solve this. Any help is appreciated.
This is my TouchInput class
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TouchInput : MonoBehaviour {
  public LayerMask touchInputMask;
 private static List<GameObject> touchList = new List<GameObject>();
 private GameObject[] touchesOld;
 private RaycastHit hit;
 void Update () {
     #if UNITY_EDITOR
     if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)){
         touchesOld = new GameObject[touchList.Count];
         touchList.CopyTo(touchesOld);
         touchList.Clear();
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit, touchInputMask)){
             GameObject recipient = hit.transform.gameObject;
             touchList.Add(recipient);
             if (Input.GetMouseButtonDown(0)){
                 recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
             }
             if (Input.GetMouseButtonUp(0)){
                 recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
             }
             if (Input.GetMouseButton(0)){
                 recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
             }
         }
         foreach (GameObject g in touchesOld){
             if (!touchList.Contains(g)){
                 g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
             }
         }
     }
     #endif
     if (Input.touchCount > 0){
         touchesOld = new GameObject[touchList.Count];
         touchList.CopyTo(touchesOld);
         touchList.Clear();
         foreach (Touch touch in Input.touches){
             Ray ray = Camera.main.ScreenPointToRay (touch.position);
             //RaycastHit hit;
             if (Physics.Raycast(ray, out hit, touchInputMask)){
                 GameObject recipient = hit.transform.gameObject;
                 touchList.Add(recipient);
                 if (touch.phase == TouchPhase.Began){
                     recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
                 }
                 if (touch.phase == TouchPhase.Ended){
                     recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
                 }
                 if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved){
                     recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
                 }
                 if (touch.phase == TouchPhase.Canceled){
                     recipient.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
                 }
             }
         }
         foreach (GameObject g in touchesOld){
             if (!touchList.Contains(g)){
                 g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
             }
         }
     }
 }
 
               }
This is button class, i apply it on all cube. I tried every code for dragging object but it work only for single cube, i want to drag multiple cube by multiple fingers.
 public class Button : MonoBehaviour{
 
     public Color defaultColour;
     public Color selectedColour;
     private Material mat;
 
     public float speed;
     public Rigidbody rb;
 
 
 
     private Vector3 screenPoint;
     private Vector3 offset;
 
 
     void Start(){
         mat = GetComponent<Renderer> ().material;
         rb = GetComponent<Rigidbody> ();
 
     }
 
     void OnTouchDown(){
         mat.color = selectedColour;
     
         screenPoint = Camera.main.WorldToScreenPoint (transform.position);
         offset = transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,
             Input.mousePosition.y, screenPoint.z));
 
 
 
 
 
     }
 
     void OnTouchUp(){
         mat.color = defaultColour;
         Debug.Log ("Touch Up");
 
         float moveHorzontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         Vector3 movement = new Vector3 (moveHorzontal, 0.0f, moveVertical);
         rb.AddForce (movement * speed);
 
         /**
         Camera theCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
 
         rb.AddForce(theCam.transform.right * Input.GetAxis("Mouse X") * 10f, ForceMode.Impulse);
         rb.AddForce(theCam.transform.up * Input.GetAxis("Mouse Y") * 10f, ForceMode.Impulse);
         */
     }
 
     void OnTouchStay(){
         mat.color = selectedColour;
         Debug.Log ("Touch Stay");
         /*
         Vector3 pos;
         pos = new Vector3 (Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position).x, Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position).y, transform.position.z);
         transform.position = pos;
         */
 
 
         Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,
                                      screenPoint.z);
         Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
         transform.position = curPosition;
 
 
 
     }
 
     void OnTouchExit(){
         mat.color = defaultColour;
         Debug.Log ("Touch Exit");
         //rg.AddForce(rg.position + (delt
 
 
         /*Camera theCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
 
         rb.AddForce(theCam.transform.right * Input.GetAxis("Mouse X") * 10f, ForceMode.Impulse);
         rb.AddForce(theCam.transform.up * Input.GetAxis("Mouse Y") * 10f, ForceMode.Impulse);
         */
     }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Multitouch drag object for an Airhockey game 0 Answers
Pinball Flippers Control with multitouch 0 Answers
Help with Inverse Mouse Panning (Middle mouse drag 0 Answers
EventTrigger multitouch 0 Answers