获取光线投射的位置,即使它没有碰到任何东西

Get position of raycast even when it doesn't hit anything

我正在开发一个简单的建筑系统,这是我所拥有的东西的小 gif。现在,当光线投射没有击中任何东西时,预览对象就会消失。我希望它在我抬头时改变它的 Y 位置,但光线投射没有 return 任何东西,我该如何实现?

https://gfycat.com/ifr/ShallowTepidAnkolewatusi

这是负责预览位置的代码

        if (deployPreview)
        {
            if (Physics.Raycast(cam.position, cam.forward, out deployableHit, 5, deployableMask))
            {
                if (deployPreview.gameObject.activeSelf)
                {
                    if (deployableHit.transform != transform)
                    {
                        if (heldDeployable.deployable.DepType == EDeployableType.Buildable)
                        {
                            HandleBuilding();
                        }
                        else
                        {
                            deployPos = deployableHit.point;
                            deployPreview.rotation = transform.rotation;
                            deployPreview.position = deployPos;
                        }
                    }
                }
            }
        }

您可以找到光线结束的位置并将预览对象移到那里。

if (deployPreview)
{
        if (Physics.Raycast(cam.position, cam.forward, out deployableHit, 5, deployableMask))
        {
           //your code
        }
        else
        {
           //not hit code
           var position = cam.position + cam.forward * 5;
           //position is where ray ends
        }
}