如何在平面上反射物体?

gar*_*lla 1 unity-game-engine

给定一个平面游戏对象,我想反映该平面的一个对象

一个简单的例子是一个位于 0,0,0 的平面,没有与位于 0,-1,0 的物体旋转,这会导致反射位置位于 0,1,0

更复杂的情况是,平面位于 0,0,0,x 轴旋转 45`,物体位于 0,-1,-1,这会导致反射位置位于 0,1,1

我正在寻找一种解决方案,可以在任何位置进行任何旋转的飞机的任何情况。

der*_*ugo 5

您可以使用Plane.ClosestPointOnPlane来获取平面上的相应位置。

因此,您需要创建Plane第一个。因为inNormal您必须使用垂直于平面的矢量。

  • 对于Quad原语来说,这是负forward向量
  • 对于Plane原语来说,它是up向量。

您可以简单地使用原始对象和原始对象之间的向量ClosestPointOnPlane来移动到相同的相对位置,但使用反转的向量:

public class ReflectPosition : MonoBehaviour
{
    public Transform Source;
    public Transform Plane;

    // Update is called once per frame
    private void Update()
    {
        // create a plane object representing the Plane
        var plane = new Plane(-Plane.forward, Plane.position);

        // get the closest point on the plane for the Source position
        var mirrorPoint = plane.ClosestPointOnPlane(Source.position);

        // get the position of Source relative to the mirrorPoint
        var distance = Source.position - mirrorPoint;

        // Move from the mirrorPoint the same vector but inverted
        transform.position = mirrorPoint - distance;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果(蓝球已附加此组件并反映白球位置)

在此输入图像描述