Unity 摄像机射线碰撞

需要对象挂有Collider组件才能计算射线,某些情况下不适用。

if (Input.GetMouseButtonDown(0))
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //从摄像机发出到点击坐标的射线
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit)) //需要有Collider组件才能碰撞到
    {
        Debug.Log(hit.point);//碰撞坐标
    }
}

标签: Unity

添加新评论