2015-11-20 65 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的結果,如果你打算這麼做的話。