Unity3D——射线碰撞检测

单个碰撞对象检测,返回第一个碰撞的对象信息:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
RaycastHit hitInfo;
if(Physics.Raycast(ray,out hitInfo))
{
  Debug.DrawLine(ray.origin,hitInfo.point);//划出射线,只有在scene视图中才能看到
  GameObject gameObj = hitInfo.collider.gameObject;
  Debug.Log("click object name is " + gameObj.name);
  if(gameObj.tag == "boot")//当射线碰撞目标为boot类型的物品 ,执行拾取操作
  {
    Debug.Log("pick up!");
  }
}

 

返回所有碰撞对象信息,顺序从近到远。

RaycastHit[] RaycastAll(Ray ray)

posted @ 2017-12-05 17:12  naterliu  阅读(613)  评论(0)    收藏  举报