鼠标随意点在模型上射线会射在所点击处,并显示点击处的坐标。
代码:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButton(0))
{
// 以摄像机所在位置为起点,创建一条发射的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Ray ray = new Ray(Camera.main.transform.position, 100f);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
// 如果射线与平面碰撞,打印碰撞物体信息
Debug.Log("坐标: " + hit.point);
// 在场景视图中绘制射线
Debug.DrawLine(ray.origin, hit.point, Color.red);
}
}
}
}
运行结果: