彼此独立移动对象,并能够在Unity 2D中同时移动

问题描述 投票:0回答:1

我有两个对象。关键是我需要上下移动对象(按住并拖动),同时我还需要独立移动其他对象(按住并拖动)。像这样:Example

搜索互联网后,我发现了一个小脚本,但一次只能触摸一次,并且只能拖动一个对象。另外,如果我用第二根手指触摸,则该对象将其位置更改为第二根手指的位置:


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;    
  }
}

我只是不明白如何使两个对象同时拖放。我是团结的新手,老实说我在控制器方面遇到问题

c# unity3d controller drag 2d-games
1个回答
0
投票

对于触摸屏,建议您使用Touch而不是鼠标事件。触摸类同时支持多个触摸以及一些有用的信息,例如Touch.phase(开始,移动,固定等)。我已经编写了一个脚本,您可以将其附加到带有“可拖动”标签的多个对象上,从而使对象独立移动。拖放使用一个手指来放置otherObj时应移动的另一个对象,它应该可以工作。此版本仅适用于2个obj。

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;
       }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.