使用Raycast时出错

时间:2009-10-31 23:28:51

标签: c# unity3d

我正在尝试使用Physics.Raycast方法,但我收到错误说:

  

“UnityEngine.Physics.Raycast(UnityEngine.Vector3,UnityEngine.Vector3,float,int)”的最佳重载方法匹配包含一些无效参数。

这很奇怪,因为itellisense和文档告诉我这是允许的:

RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
    print(hit.point.ToString());
    selection.transform.Translate(hit.point - selection.transform.position);
}

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

我认为在Physics.Raycast(ray,hit)中“hit”之前你需要out关键字。

RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
    print(hit.point.ToString());
    selection.transform.Translate(hit.point - selection.transform.position);
}

答案 1 :(得分:-1)

在C#中,我们必须在变量命中之前使用前置输出参数 为了获得向其分配数据的功能。