C# 统一-在屏幕边缘上获取对象指向的点?

C# 统一-在屏幕边缘上获取对象指向的点?,c#,unity3d,screen,C#,Unity3d,Screen,这就是我的问题: 我在屏幕中有一个指向特定方向的对象。出于演示目的,我添加了一个LineRenderer来显示这个方向 我的最终目标是找到对象和世界点之间的距离,紫色光线与屏幕边界相交(白色轮廓) 但我需要明白这一点,我不知道该怎么做 Vector2 PointOnScreenEdge = ??? 因此,问题是: 我怎样才能得到“屏幕上的点”? 请注意,我始终知道对象面对的方向及其世界坐标 在互联网上,我读到要找到相对于屏幕的世界点,可以使用Camera.ScreenToWorldPoin

这就是我的问题:

我在屏幕中有一个指向特定方向的对象。出于演示目的,我添加了一个
LineRenderer
来显示这个方向

我的最终目标是找到对象和世界点之间的距离,紫色光线与屏幕边界相交(白色轮廓)

但我需要明白这一点,我不知道该怎么做

Vector2 PointOnScreenEdge = ???

因此,问题是: 我怎样才能得到“屏幕上的点”?

请注意,我始终知道对象面对的方向及其世界坐标

在互联网上,我读到要找到相对于屏幕的世界点,可以使用
Camera.ScreenToWorldPoint()
Camera.ViewportToWorldPoint()
,但我不知道在这种情况下如何正确使用它们

感谢您的帮助。

其中有一个返回组成相机平截体的六个平面的阵列

排序:[0]=左,[1]=右,[2]=下,[3]=上,[4]=近,[5]=远

你对那些
0
3
感兴趣,然后可以像这样对它们做一个测试

void Update()
{
    // Wherever you get these to from
    Vector3 origin;
    Vector3 direction;

    var ray = new Ray(origin, direction);

    var currentMinDistance = float.Max;
    var hitPoint = Vector3.zero;
    var planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
    for(var i = 0; i < 4; i++)
    {
        // Raycast against the plane
        if(planes[i].Raycast(ray, out var distance))
        {
            // Since a plane is mathematical infinite
            // what you would want is the one that hits with the shortest ray distance
            if(distance < currentMinDistance)
            {
                hitPoint = ray.GetPoint(distance);
                currentMinDistance = distance;
            } 
        }
    }  

    // Now the hitPoint should contain the point where your ray hits the screen frustrum/"border"
    lineRenderer.SetPosition(1, hitPoint);
}
void Update()
{
//无论你从哪里得到这些
矢量3原点;
矢量3方向;
var射线=新射线(原点、方向);
var currentMinDistance=float.Max;
var生命点=向量3.0;
var planes=几何有效性。CalculateFrustumPlanes(Camera.main);
对于(变量i=0;i<4;i++)
{
//对飞机进行光线投射
if(平面[i]。光线投射(光线,向外距离))
{
//因为平面在数学上是无限的
//你想要的是射线距离最短的一个
if(距离<当前距离)
{
hitPoint=ray.GetPoint(距离);
当前距离=距离;
} 
}
}  
//现在,命中点应该包含光线击中屏幕平截体/边框的点
lineRenderer.SetPosition(1,生命点);
}
取决于您的相机是否在实际移动/旋转/缩放,您可能希望在开始时只获取一次平锥面

您还应该在开始时将
Camera.main
reference缓存一次

void Update()
{
    // Wherever you get these to from
    Vector3 origin;
    Vector3 direction;

    var ray = new Ray(origin, direction);

    var currentMinDistance = float.Max;
    var hitPoint = Vector3.zero;
    var planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
    for(var i = 0; i < 4; i++)
    {
        // Raycast against the plane
        if(planes[i].Raycast(ray, out var distance))
        {
            // Since a plane is mathematical infinite
            // what you would want is the one that hits with the shortest ray distance
            if(distance < currentMinDistance)
            {
                hitPoint = ray.GetPoint(distance);
                currentMinDistance = distance;
            } 
        }
    }  

    // Now the hitPoint should contain the point where your ray hits the screen frustrum/"border"
    lineRenderer.SetPosition(1, hitPoint);
}