C# 平面对象位置到平面游戏对象位置

C# 平面对象位置到平面游戏对象位置,c#,opengl,math,unity3d,3d,C#,Opengl,Math,Unity3d,3d,平面对象位置到平面游戏对象位置 基本上我已经基于三个点创建了一个平面。但是,出于测试的原因,我需要知道飞机在哪里。所以,我在我的层次结构中有一个平面游戏对象(包括网格、纹理和所有其他东西)。我想将平面游戏对象移动到平面对象的位置,使法线成为平面对象的法线 下面是一些代码: plane1Go = GameObject.Find("Plane1"); Plane plane1 = new Plane(Point1,Point2,Point3); plane1Go.gameObject.GetCom

平面对象位置到平面游戏对象位置

基本上我已经基于三个点创建了一个平面。但是,出于测试的原因,我需要知道飞机在哪里。所以,我在我的层次结构中有一个平面游戏对象(包括网格、纹理和所有其他东西)。我想将平面游戏对象移动到平面对象的位置,使法线成为平面对象的法线

下面是一些代码:

plane1Go = GameObject.Find("Plane1");
Plane plane1 = new Plane(Point1,Point2,Point3);

plane1Go.gameObject.GetComponent<Mesh>().vertices= new Vector3[4]{Point1,Point2,Point3,Point4}; 
plane1Go=GameObject.Find(“Plane1”);
平面1=新平面(点1、点2、点3);
plane1Go.gameObject.GetComponent().vertices=newvector3[4]{Point1,Point2,Point3,Point4};
如你所见,我使用了三个点,用来使平面对象移动平面对象

首先,飞机游戏物体不动,我不知道为什么


第二,也是更重要的一点,我想使用平面对象作为平面游戏对象的实际参考点。

除非它已更改,否则您应该修改游戏对象的变换元素

GameObject.FindWithTag("PlaneTop").transform.position = new Vector3(10, 0, 0);
这将保持网格,但移动对象


游戏对象本身的参考点取决于导出网格时网格的局部空间原点的位置。我不完全确定您想要实现什么,但可能是重新定位网格本身,而不是游戏对象。

我建议采用两步方法:

  • 将游戏对象放置在平面上
  • 使游戏对象面向与平面法线相等的方向
我假设你有一个网格连接到游戏对象上,也许是某种大的四边形

Plane plane = new Plane(point1, point2, point3);

//technically, any point on the plane will work
//for our purposes, I'll take the average of your three inputs
Vector3 center = (point1 + point2 + point3) / 3f;

//find the plane's transform
Transform planeTransform = GameObject.Find("Plane1").transform;

//position the plane
//then make it face toward its normal
planeTransform.position = center;
planeTransform.LookAt(planeTransform.position + plane.normal);
你问题的第二部分不太清楚:

我想使用平面对象作为平面游戏对象的参考点

您可以根据需要经常重新定位
planeTransform
。您可以在每次平面更改时重新定位它,或在
Update
函数中每帧重新定位一次


我确实建议缓存
GameObject的结果。如果你经常调用它,请查找

我的代码可能有误导性。我对移动游戏对象的网格不感兴趣,而是对整个对象本身感兴趣。还有,不是要强加于人,但是你对第二个问题有什么想法?