开发者

Error on using Raycast

I'm trying to use the Physics.Raycast method, but I get 开发者_开发技巧errors saying:

The best overloaded method match for 'UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float, int)' has some invalid arguments.

It's strange because both itellisense and the documentation tell me that this is permitted:

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);
}

Any idea?


I think you need the out keyword before "hit" in Physics.Raycast(ray, hit).

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);
}


In C# we must use a precursor out parameter before the variable hit in order to get the function to assign data to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜