拖放2d统一

时间:2014-06-11 21:32:15

标签: drag-and-drop unity3d touch

我正在尝试在团结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);
        }
    }
}

目前我正在检测用户触摸的对象。我需要能够获得该对象并改变它的位置。

我还发现了这个允许移动刚体的代码片段

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

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

1 个答案:

答案 0 :(得分:0)

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

示例:

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

以上代码用于更改选定和取消选择的多维数据集的标记。之后,您可以轻松识别Selected游戏对象,并可以随意移动它。

您可以在更新功能中使用此代码。