Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top