Search Unity

Multitouch drag object

Discussion in 'Scripting' started by NicoloTrapasso, Oct 21, 2016.

  1. NicoloTrapasso

    NicoloTrapasso

    Joined:
    Sep 23, 2016
    Posts:
    13
    Hi all,

    this is my script that allows to touch an object 2d and drag it on the screen, i want to ability multitouch because i need to touch 2 object 2d simultaneously and drag on the screen. Please i need your help, can you help me?

    Sorry for my bad english

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Drag : MonoBehaviour {
    5.  
    6.     //This code is for 2D click/drag gameobject
    7.  
    8.     public GameObject gameObjectTodrag; //refer to GO that being dragged
    9.  
    10.     public Vector3 GOcenter; //gameobjectcenter
    11.     public Vector3 touchPosition; //touch or click position
    12.     public Vector3 offset;//vector between touchpoint/mouseclick to object center
    13.     public Vector3 newGOCenter; //new center of gameObject
    14.  
    15.     RaycastHit hit; //store hit object information
    16.  
    17.     public bool draggingMode = false;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.  
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.  
    30.         //***********************
    31.         // *** CLICK TO DRAG ****
    32.         //***********************
    33.  
    34. #if UNITY_EDITOR
    35.         //first frame when user click left mouse
    36.         if (Input.GetMouseButtonDown(0))
    37.         {
    38.             //convert mouse click position to a ray
    39.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    40.  
    41.             //if ray hit a Collider ( not 2DCollider)
    42.             if (Physics.Raycast(ray, out hit))
    43.             {
    44.                 gameObjectTodrag = hit.collider.gameObject;
    45.                 GOcenter = gameObjectTodrag.transform.position;
    46.                 touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    47.                 offset = touchPosition - GOcenter;
    48.                 draggingMode = true;
    49.             }
    50.         }
    51.  
    52.         //every frame when user hold on left mouse
    53.         if (Input.GetMouseButton(0))
    54.         {
    55.             if (draggingMode)
    56.             {
    57.                 touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    58.                 newGOCenter = touchPosition - offset;
    59.                 gameObjectTodrag.transform.position = new Vector3(newGOCenter.x, newGOCenter.y, GOcenter.z);
    60.             }
    61.         }
    62.  
    63.         //when mouse is released
    64.         if (Input.GetMouseButtonUp(0))
    65.         {
    66.             draggingMode = false;
    67.         }
    68. #endif
    69.  
    70.         //***********************
    71.         // *** TOUCH TO DRAG ****
    72.         //***********************
    73.         foreach (Touch touch in Input.touches)
    74.         {
    75.             switch (touch.phase)
    76.             {
    77.                 //When just touch
    78.                 case TouchPhase.Began:
    79.                     //convert mouse click position to a ray
    80.                     Ray ray = Camera.main.ScreenPointToRay(touch.position);
    81.  
    82.                     //if ray hit a Collider
    83.                     // if (Physics.Raycast(ray, out hit))
    84.                     if (Physics.SphereCast(ray, 0.3f, out hit))
    85.                     {
    86.                         gameObjectTodrag = hit.collider.gameObject;
    87.                         GOcenter = gameObjectTodrag.transform.position;
    88.                         touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    89.                         offset = touchPosition - GOcenter;
    90.                         draggingMode = true;
    91.                     }
    92.                     break;
    93.  
    94.                 case TouchPhase.Moved:
    95.                     if (draggingMode)
    96.                     {
    97.                         touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    98.                         newGOCenter = touchPosition - offset;
    99.                         gameObjectTodrag.transform.position = new Vector3(newGOCenter.x, newGOCenter.y, GOcenter.z);
    100.                     }
    101.                     break;
    102.  
    103.                 case TouchPhase.Ended:
    104.                     draggingMode = false;
    105.                     break;
    106.             }
    107.         }
    108.     }
    109. }
     
  2. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    gameObjectToDrag is set in TouchPhase.Begin, and then overwritten by the second touch, so both touches will try to move the same object. You would either have to find the correct object again in TouchPhase.Moved, or have separate references.