unity中2D与3D物体的线性检测

2D:

Vector2 mousePoint2D = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
 RaycastHit2D hitInfo2D = Physics2D.Raycast(mousePoint2D, Vector2.zero);
if (hitInfo2D.collider != null)
{
    Debug.Log(hitInfo2D.collider.name); //点击到的物体
}

3D:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hitInfo;
 if (Physics.Raycast(ray, out hitInfo))
{
    //点击到物体
}

  

 

posted @ 2017-12-09 14:23  游戏鼻祖  阅读(300)  评论(0编辑  收藏  举报