无法隐式转换类型`bool'到'UnityEngine.RaycastHit'

时间:2015-03-28 22:26:59

标签: c# unity3d

您好我尝试从gameObject旁边投了一行到gameObject

bool Valid(Vector3 direction)
{
    Vector3 position = transform.position;
    RaycastHit hit = Physics.Linecast (position + direction, direction);
    return (hit.collider == GetComponent<Collider>());
}

错误:

  

无法隐式转换类型bool' to UnityEngine.RaycastHit'

1 个答案:

答案 0 :(得分:4)

如果有任何碰撞器与Physics.Linecaststart之间的线相交,则

end会返回true,但它不会返回RaycastHit

您必须将hit变量添加为第三个参数:

RaycastHit hit;
Physics.Linecast(position + direction, direction, out hit);

有关详细信息,请参阅API