0

I have two objects. The point is that I need to move the object up and down (hold and drag), and at the same time I need to move the other object up and down (hold and drag) independently. Something like this: Example

After searching the Internet, I found a small script, but it only works for one touch at a time, and dragging only one object. Plus, if I touch with second finger the object changes his position to second finger position:


public class Move : MonoBehaviour {
    private Vector3 offset; 

  void OnMouseDown() {
      offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(-2.527f, Input.mousePosition.y));
  }

  void OnMouseDrag() {    
      Vector3 curScreenPoint = new Vector3(-2.527f, Input.mousePosition.y);
      Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
      transform.position = curPosition;    
  }
}

I just can’t understand how I can make two objects drag and drop at the same time. I am new to unity and honestly i have problems with controllers

1 Answers1

0

For touch screens I suggest you use Touch instead of mouse events. Touch class supports multiple touches at the same time and some useful information such as Touch.phase (Began, moving, stationary etc). I've written a script which you can attach to multiple objects with tag "Draggable", that makes objects move independently. Drag and drop the other object that should move when you use one finger to field otherObj and it should work. It only works for 2 obj in this version.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

public class PlayerActionHandler : MonoBehaviour
{
private Camera _cam;

public bool beingDragged;
public Vector3 offset;

public Vector3 currPos;

public int fingerIndex;
public GameObject otherObj;
// Start is called before the first frame update
void Start()
{
    _cam = Camera.main;
}

// Update is called once per frame
void Update()
{
    //ONLY WORKS FOR 2 OBJECTS
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        var raycast = _cam.ScreenPointToRay(Input.GetTouch(0).position);
        RaycastHit hit;
        if (Physics.Raycast(raycast, out hit))
        {
            //use this tag if you want only chosen objects to be draggable 
            if (hit.transform.CompareTag("Draggable"))
            {
                if(hit.transform.name == name)
                {
                    //set being dragged and finger index so we can move the object 
                    beingDragged = true;
                    offset = gameObject.transform.position - _cam.ScreenToWorldPoint(Input.GetTouch(0).position);
                    offset = new Vector3(offset.x, offset.y, 0);
                    fingerIndex = 0;
                }
            }
        }
    }else if (Input.touchCount == 1 && beingDragged)
    {
        otherObj.transform.SetParent(transform);
        if (Input.GetTouch(fingerIndex).phase == TouchPhase.Moved ){
            Vector3 pos = _cam.ScreenToWorldPoint(Input.GetTouch(fingerIndex).position);
            currPos = new Vector3(pos.x, pos.y,0) + offset;
            transform.position = currPos;

        }
    }
    //ONLY WORKS FOR 2 OBJECTS_END
    else if (beingDragged && Input.touchCount > 1)
     {
         //We tie each finger to an object so the object only moves when tied finger moves
         if (Input.GetTouch(fingerIndex).phase == TouchPhase.Moved ){
             Vector3 pos = _cam.ScreenToWorldPoint(Input.GetTouch(fingerIndex).position);
             currPos = new Vector3(pos.x, pos.y,0) + offset;
             transform.position = currPos;

         }
     }else if (Input.touchCount > 0)
       {
           for (var i = 0; i < Input.touchCount; i++)
           {
               //We find the fingers which just touched the screen
               if(Input.GetTouch(i).phase == TouchPhase.Began)
               {
                   var raycast = _cam.ScreenPointToRay(Input.GetTouch(i).position);
                   RaycastHit hit;
                   if (Physics.Raycast(raycast, out hit))
                   {
                       //use this tag if you want only chosen objects to be draggable 
                       if (hit.transform.CompareTag("Draggable"))
                       {
                           if(hit.transform.name == name)
                           {
                               //set being dragged and finger index so we can move the object 
                               beingDragged = true;
                               offset = gameObject.transform.position - _cam.ScreenToWorldPoint(Input.GetTouch(i).position);
                               offset = new Vector3(offset.x, offset.y, 0);
                               fingerIndex = i;
                           }
                       }
                   }
               }
           }

       }else if (Input.touchCount == 0)
       {
           //if all fingers are lifted no object is being dragged
           beingDragged = false;
       }
}

}

Chuckiee3
  • 91
  • 1
  • 4