【问题标题】:Drag and drop in unity 2d在统一 2d 中拖放
【发布时间】:2014-08-02 02:25:20
【问题描述】:

我正在尝试在统一 2d 中为我的游戏实现拖放功能。我的屏幕中有多个相同对象的副本,它们仅在对撞机名称上有所不同。我将相同的脚本附加到他们身上。这是我的一段代码

function Start () {
    playerTouches = [-1, -1];
}

function resetPlayer(touchNumber: int) {
    for(var i = 0; i < playerTouches.length; ++i) {
        if(touchNumber == playerTouches[i]) {
            playerTouches[i] = -1;
        }
    }
}

function getCollider(vec: Vector2) {
    var ray : Ray = Camera.main.ScreenPointToRay(vec);
    var hit : RaycastHit2D = Physics2D.Raycast(ray.origin, ray.direction);

    if (hit) {
        if (hit.collider != null) {
            Debug.Log(hit.collider.name);
            return hit.collider.name;
        } else {
            Debug.Log("is null");
            return "null";
        }
    } else {
        Debug.Log("empty");
        return "";
    }
    return "";
}

function processTouch(touch: Touch, touchNumber: int) {

    if(touch.phase == TouchPhase.Began) {
        var colliderName: String = getCollider(touch.position);
        if(colliderName == "Object01" && playerTouches[0] == -1) {
            playerTouches[0] = touchNumber;
        } else if(colliderName == "Object02" && playerTouches[1] == -1) {
            playerTouches[1] = touchNumber;
        }
    } else if(touch.phase == TouchPhase.Moved) {

         // get object and change coords

    } else if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
        resetPlayer(touchNumber);
    }
}

function Update() {

    if(Input.touchCount > 0) {
        //Debug.Log("count = " + Input.touchCount);
        for(var i = 0; i < Input.touchCount; i++)
        {
            processTouch(Input.GetTouch(i), i);
            //Debug.Log("touch : " + i + "   " + Input.GetTouch(i).position);
        }
    }
}

现在我正在检测用户触摸了哪个对象。我需要能够得到那个对象并改变它的位置。

我还发现了这段代码 sn-p 允许移动刚体

var touchDeltaPosition: Vector2 = touch.deltaPosition;
var touchPosition: Vector2;
touchPosition.Set(touchDeltaPosition.x, touchDeltaPosition.y);
rigidbody2D.transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime * spd);

但无论我选择什么对象,它都会移动所有对象。

【问题讨论】:

    标签: drag-and-drop unity3d touch


    【解决方案1】:

    嗯,你可以这样做。如果您有 12 个相同对象的副本并且想要移动用户选择的对象。因此,当用户触摸对象时,将 GameObject tagName 更改为另一个标签。之后,您可以使用一些条件语句来处理您的代码。

    例子:

    if(Input.GetMouseButtonDown(0)) {
        Debug.Log("Mouse is down");
    
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo = new RaycastHit();
        //bool hit = Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo);
        if(Physics.Raycast(ray, out hitInfo, 30)) {
            Debug.Log("Hit " + hitInfo.transform.gameObject.name);
            if(hitInfo.transform.gameObject.tag == "Deselected") {
                //Debug.Log ("It's working! Attaching MoveCube Script to game :" + hitInfo.transform.gameObject.tag);
    
                findObject = GameObject.FindGameObjectsWithTag("Deselected");
                foreach(GameObject go in findObject) {
                    //go.gameObject.renderer.material.color = Color.white;
    
                    go.GetComponent<MoveCube>().enabled = false;
    
                    if(hitInfo.transform.gameObject.name.Equals(go.gameObject.name)) {
                        //hitInfo.transform.renderer.material.color = Color.white;
                        hitInfo.transform.gameObject.GetComponent<MoveCube>().enabled = true;
                        changeTAG = true;
                    } else {
                        hitInfo.transform.gameObject.tag = "Deselected"
                    }
                }
    
                playerObject = GameObject.FindGameObjectsWithTag("Player");
                foreach(GameObject game in playerObject) {
                    count++;
                    if(count == 1) {
                        hitInfo.transform.gameObject.tag = "Player";
                    }
                    if(count >= 1) {
                        game.gameObject.tag = "Deselected";
                        game.gameObject.GetComponent<MoveCube>().enabled = false;
                        //game.gameObject.renderer.material.color = Color.white;
                    }
                }
    
                if(changeTAG) {
                    hitInfo.transform.gameObject.tag = "Player";
                    /*if (hitInfo.transform.gameObject.GetComponent<Rigidbody> ()) {
                            Debug.Log ("RigidBody is already added Can't add another RigidBody");
                            hitInfo.transform.rigidbody.WakeUp ();
    
                    } else {
                            hitInfo.transform.gameObject.AddComponent<Rigidbody> ().useGravity = false;
                            //  hitInfo.transform.gameObject.GetComponent<Rigidbody> ().WakeUp ();
                    }*/
    
                    changeTAG = false;
                } else if(!changeTAG) {
                    hitInfo.transform.gameObject.tag = "Deselected";
                }
    
            } else {
                Debug.Log("Not Working");
            }
        } else {
            Debug.Log("No hit");
        }
        Debug.Log("Mouse is down");
    }   
    

    以上代码用于更改选中和取消选中的立方体的标签。之后,您可以轻松识别选定的游戏对象并将其移动到您想要的任何位置。

    您可以在更新函数中使用此代码。

    【讨论】:

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