Unity3d 实现鼠标左键点击地形让角 {MOD}移动到指定地点:
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && !UICamera.Raycast(Input.mousePosition))
{
if (hit.collider.gameObject.name == "Terrain")
{
Debug.Log("hit.point: " + hit.point);
Vector3 pos = new Vector3(hit.point.x * 100, hit.point.y * 100, hit.point.z * 100);
Debug.Log("hit.point:" + hit.point + ", pos: " + pos);
}
}
}
其中,UICamera.Raycast(Input.mousePosition)是为了解决点击NGUI的元素时会穿透(就是NGUI点击UI和人物行走冲突)的问题。
if (hit.collider.gameObject.name == "Terrain")这一句,如果地形的tag有分类,则可以使用:
if (hit.collider.gameObject.tag== "Terrain")