if (Input.GetMouseButton(0))
{
Debug.Log("xxxxxxxxxxxxxxxxxxxxxx");
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;
if (gameObj.name.StartsWith("Cube") == true)//当射线碰撞目标的name包含Cube,执行拾取操作
{
Debug.Log(gameObj.name);
}
}
}
{
Debug.Log("xxxxxxxxxxxxxxxxxxxxxx");
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;
if (gameObj.name.StartsWith("Cube") == true)//当射线碰撞目标的name包含Cube,执行拾取操作
{
Debug.Log(gameObj.name);
}
}
}
本文介绍了一种使用Unity实现鼠标拾取场景中特定对象的方法。通过获取鼠标点击位置的射线,并利用Physics.Raycast进行碰撞检测,可以判断鼠标是否点击到了名称以Cube开头的游戏对象上。
1万+

被折叠的 条评论
为什么被折叠?



