【问题标题】:Moving objects independently of each other, with the ability to move at the same time in Unity 2D相互独立移动对象,能够在 Unity 2D 中同时移动
【发布时间】:2019-09-26 06:02:34
【问题描述】:

我有两个对象。关键是我需要上下移动对象(按住并拖动),同时我需要独立地上下移动另一个对象(按住并拖动)。像这样的东西: 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;    
  }
}

我只是不明白如何让两个对象同时拖放。我是 Unity 新手,老实说,我对控制器有疑问

【问题讨论】:

    标签: c# unity3d controller drag 2d-games


    【解决方案1】:

    对于触摸屏,我建议您使用Touch 而不是鼠标事件。 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;
           }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多