给定一个平面游戏对象,我想反映该平面的一个对象
一个简单的例子是一个位于 0,0,0 的平面,没有与位于 0,-1,0 的物体旋转,这会导致反射位置位于 0,1,0
更复杂的情况是,平面位于 0,0,0,x 轴旋转 45`,物体位于 0,-1,-1,这会导致反射位置位于 0,1,1
我正在寻找一种解决方案,可以在任何位置进行任何旋转的飞机的任何情况。
您可以使用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)
结果(蓝球已附加此组件并反映白球位置)