Raycast没有碰到一个物体

时间:2017-01-15 14:04:03

标签: c# unity3d unity5 unity3d-2dtools

为什么此代码在触摸后不会打印任何内容?

    void Update()
{
    if (Input.touchCount > 0)
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
        if (hit)
        {
            print(hit.transform.name);
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

试试这个:

if (Input.touchCount > 0)
    {
        RaycastHit2D hit;

        if (Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero, out hit))
        {
            print(hit.transform.name);
        }
    }