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)) { //点击到物体 }