2015-11-20 62 views
1

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

基本上我基于三点创建了一架飞机。但是,出于测试的原因,我需要知道飞机在哪里。所以,我的Hierarchy中有一个Plane GameObject(带有网格,纹理和其他所有东西)。我想将平面游戏对象移动到平面对象的位置和法线上。

下面是一些代码:

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

plane1Go.gameObject.GetComponent<Mesh>().vertices= new Vector3[4]{Point1,Point2,Point3,Point4}; 

正如你可以看到我用三个点,用来做面对象移动的游戏对象平面。

首先,Plane GameObject不动,我不知道为什么。

其次更重要的是,我想使用Plane对象实际上成为Plane GameObject的参考点。

回答

1

除非它改变了,否则你应该修改gameobject的Transform元素。

GameObject.FindWithTag("PlaneTop").transform.position = new Vector3(10, 0, 0); 

这会保持网格,但会移动物体。

GameObject本身的参考点取决于导出网格时放置网格的局部空间原点的位置。我不完全确定你想要达到的目标,但它可能是重新定位网格本身的情况,而不是游戏对象。

+0

我的代码可能会引起误解。我对移动游戏对象的网格物体并不感兴趣,但对整个对象本身并不感兴趣。另外,不要强加,但对于第二个问题你有什么想法? – user3795555

0

我会提出一个两步走的方法:

  • 上的平面位置一个游戏对象
  • 做的方向是游戏物体的脸等于飞机的正常

我认为你有一个连接到GameObject的网格,也许是某种大四边形。

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.Find的结果,如果你打算这么做的话。