function Update () { if (Input.GetMouseButton (0)) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { Debug.DrawLine (ray.origin, hit.point); print(hit.collider.gameObject.name); } } }
通过射线碰撞来实现,代码比较简单。