问题是没有重载函数
Physics.Raycast(origin.position , RaycastHit hitinfo);
这意味着您可以将许多东西放入 Raycast 函数中,但 a(Vector3, RaycastHit)不是组合之一。我经常使用以下方法:
/* Define the point of origin for the raycast */
var origin = transform.position; // From the player
/* Define the direction the raycast will be going in from the origin */
var dir = transform.forward; // Infront of the player
/* Define a ray */
Ray ray = new Ray(origin, dir);
/* Create a storage for the results */
HitInfo hit;
/* Check to see if we hit something */
if (Physics.Raycast(ray, out hit)){
// Yes we did hit something
print("We hit an object");
}
如果我们把它放到一个 propper 函数中来获取相机正在查看的标签:
public string GetTagOfLookingAt () {
RaycastHit hit;
Ray ray = Camera.Main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
return hit.gameobject.tag;
} else {
return string.Empty;
}
}