using UnityEngine;
using System.Collections;
public class ScreenRayDemo : MonoBehaviour
{
Ray ray;
RaycastHit hit;
// 创建射线到屏幕上的参考点,像素坐标
Vector3 position = new Vector3(Screen.width / 2.0f, Screen.height / 2.0f, 0.0f);
void Update()
{
//鼠标点击
if (Input.GetMouseButtonDown(0))
{
//从摄像机发出到点击坐标的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//显示射线,只有在scene视图中才能看到
Debug.DrawLine(ray.origin, hit.point);
Debug.Log(hit.point);//碰撞坐标
Debug.Log(hit.ToString());//碰撞坐标
print("坐标" + hit.transform.position);
print("点" + hit.point);
print("重心坐标" + hit.barycentricCoordinate);
print("碰撞盒********************" + hit.collider.name);
print("距离" +
unity射线检测
最新推荐文章于 2025-05-15 20:38:04 发布