示例一: 判断鼠标选中的物体
using UnityEngine; using System.Collections; public class ScreenPointToRayTest : MonoBehaviour { void Update() { if (Input.GetMouseButtonDown(0)) { //Camera.main为第一个启动的Tag为MainCamera的摄相机 //投射距离 float dist = Camera.main.farClipPlane - Camera.main.nearClipPlane; //通过光线投射判断鼠标点中的物体 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, dist)) { Debug.Log("鼠标选中的物体名字: " + hit.transform.name + ", 点击坐标: "+hit.point.ToString()); Debug.DrawLine(ray.origin, hit.point); } } } }