首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拖放一体化2d

拖放一体化2d
EN

Stack Overflow用户
提问于 2014-06-11 21:32:14
回答 1查看 5.7K关注 0票数 0

我试图实现拖拽功能,我的游戏在统一2d。我的屏幕上有多个相同对象的副本,它们仅通过对撞机名称不同。我把同样的剧本附在他们身上。这是我的一段代码

代码语言:javascript
复制
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);
        }
    }
}

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

我还找到了允许移动刚体的代码片段。

代码语言:javascript
复制
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);

但是它会移动所有的对象,不管我选择的对象是什么。

EN

回答 1

Stack Overflow用户

发布于 2014-06-12 10:28:31

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

例子:

代码语言:javascript
复制
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");
}   

以上代码用于更改选定和取消选择的多维数据集的标记。在此之后,您可以轻松地识别所选的gameObject并将其移动到任何您想要的位置。

可以在Update函数中使用此代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24172924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档