Unity - RayCast 中的 GameObject 定位没有剪裁

Unity - GameObject positioning at RayCast hit without clipping

因此,我正在开发一款具有基础构建功能的 VR 游戏,并使用玩家手 Raycast 击中的位置作为预览 GameObject 必须到达的位置。但很明显,当我这样做时,游戏对象会穿过 Hit 网格。我正试图找到一种方法来避免这种情况。

我想出了一个解决方案,但没有以任何方式让它发挥作用。也许这是我的想法,也许是我的编程知识,不知道,但是很好。就这样吧。

因此 Preview GameObject Transform 设置为 HitPoint 变换并远离碰撞点,直到不再发生碰撞。我猜?我只提出性能要求高的解决方案,但它们甚至无法正常工作。有人知道好的解决方案吗?

提前致谢!

这听起来像是您在寻找 Physics.ComputePenetration

Compute the minimal translation required to separate the given colliders apart at specified poses.

所以你需要在你放置的物体上放置一个 Collider(命中的物体已经有一个)然后做例如

// Wherever you get this from
Collider objectToPlace;

if(Physics.Raycast(yourRay, out var hit))
{
    objectToPlace.trabsform.position = hit.point;

    if(Physics.ComputePenetration(objectToPlace, objectToPlace.transform.position, objectToPlace.transform.rotation, hit.collider, hit.transform.position, hit.transform.rotation, out var direction, out var distance))
    {
        objectToPlace.position += direction * distance;
    }
}