【问题标题】:RaycastHit2D doesn't detect touch on gameobjectRaycastHit2D 未检测到对游戏对象的触摸
【发布时间】:2019-06-16 06:34:54
【问题描述】:

我正在尝试使用 RaycastHit2D 检测选定游戏对象上的触摸。游戏是2D的。下面是我的代码:

 void Update()
{
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
    {
        Debug.Log("touch");
        Vector3 touchPosWorld = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position);
        Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
        RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
        Debug.Log(hitInformation.collider);
    }
}

我已将该脚本附加到空游戏对象。然后创建一个带有盒子碰撞器的预制件。但是当我触摸带有 boxcollider2d 的预制件时,它仍然显示为 Null。我的代码有什么问题

【问题讨论】:

    标签: c# unity3d game-development


    【解决方案1】:

    我发现问题现在改一下

    Vector3 touchPosWorld = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position);
    

    到这里

    Vector3 touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      相关资源
      最近更新 更多