android - Unity 2D 与预制件的碰撞无法在手机上运行

标签 android unity-game-engine touch collision-detection

当我将手机用作测试设备时,OnTriggerEnter 有点问题。 我有一些触摸代码可以成功地让我在屏幕上拖动对象。 然后我让对象与屏幕上的其他对象发生碰撞。

在我将对象变成预制件之前,这一直运行良好。 (我需要这样做,因为对象是在运行时随机生成的)

现在,我仍然可以在屏幕上移动对象,但它们不再与其他对象发生碰撞,这些对象也是预制件。然而,当它在我的笔记本电脑上的 unity 编辑器中运行时,它仍然可以正常工作。

我所有的物体上都有对撞机并检查了触发器,并且移动物体有刚体。

触发输入代码

public void OnTriggerEnter(Collider other)
{
    Debug.Log ("here");
    Debug.Log(this.gameObject.tag +"is this");
    Debug.Log(other.gameObject.tag + "is other");
    if (this.gameObject.tag == other.gameObject.tag) 
    {
        Debug.Log("here2)");
        Reftomanager.miniGameScore++;
        Reftomanager.updateScore();
        Destroy(this.gameObject);
    }
}

触摸代码

    if (Input.touchCount > 0) 
    {


        Touch touch = Input.GetTouch(0);

        switch(touch.phase)
        {

        case TouchPhase.Began:
                Ray ray = Camera.main.ScreenPointToRay (touch.position);

                if (Physics.Raycast(ray,out hit)) 
                {
                    thisObject = hit.collider.gameObject;
                    touchPos = Camera.main.ScreenToWorldPoint (touch.position);
                    if(thisObject.name!="circle")
                    {
                    draggingMode = true;
                    }
                }


                break;

        case TouchPhase.Moved: 
            if (draggingMode) 
            {
                touchPos = Camera.main.ScreenToWorldPoint (touch.position);
                newCentre = touchPos;
                thisObject.transform.position = touchPos;
            }
            break;


        case TouchPhase.Ended: 

            draggingMode = false;

        break;
        }
    }


}

我完全被难住了,所以任何帮助都会很棒。

谢谢

最佳答案

最近刚遇到同样的错误。我建议使用

If(other.gameObject.CompareTag ("YourTagName"))

此外,如果您最近添加了一个标签或编辑了任何标签,我发现 unity 有一个错误,除非您重新启动 unity,否则您的标签将不会在您的 android 构建中注册。

GL.

关于android - Unity 2D 与预制件的碰撞无法在手机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36432116/

相关文章:

android - 在android中创建并写入一个xml文件

c# - 在 Unity 中使用事件触发器 SetGazedAt 进行 Lerping 颜色

c# - 如何正确创建对 Unity 对象的引用?

c# - 有没有办法根据平台向 C#/Unity 中的属性添加逻辑?

unity-game-engine - Unity Touch 阶段返回不正确的结果

javascript - jack 摩尔缩放 1.7.15 : Toggle zoom on touch device with double tap

java - Android Studio - 锁定手机时不要关闭应用程序

android - 使用 onActivityResult 将数据从一个应用程序传递到另一个应用程序

android自定义键盘: touch area of space key not completely covered

android - 如果我恢复 Activity ,如何重新启动倒计时?