2013-05-18 39 views
2

我想弄清楚一種方法,使用Unity3D面向相機的3D平面上移動物體。我希望這些物體被限制在飛機上,飛機本身可以在3D空間中移動(相機跟隨它)。在3D飛機上的積分

爲此,我想我會使用飛機的座標系移動物體。我認爲一個平面上的x,y座標在實際的3D空間中會有相應的x,y,z座標。問題是,我不知道如何訪問,使用或計算該平面上的(x,y)座標。

我已經做了一些研究,並且遇到了矢量和法線等,但我不明白它們與我的特定問題有何關係。如果我在正確的地方尋找,你能解釋一下嗎?如果我不是,你能指出我正確的方向嗎?非常感謝。

+1

在http://paulbourke.net/geometry/pointlineplane/看看 –

回答

1

我以爲我會在飛機上使用的座標系來移動 對象

這是要走的路。

我覺得一個平面上的x,y座標在實際的3D空間中會有相應的x,y,z座標。問題是,我找不到 如何訪問,使用或計算該平面上的(x,y)座標。

是的,這是真的。但是Unity3D可讓您訪問相當高級別的功能,因此您不必顯式地進行任何計算。

將您的GameObjects設置爲Plane的子項,然後使用本地座標系移動它們。

例如:

childObj.transform.parent = plane.transform; 
childObj.transform.localPosition += childObj.transform.forward * offset; 

上面的代碼使得childObj平面GameObject的子和向前移動它的在其本地座標系統偏移。

+0

由於一噸。我會稍微留意一下(剛從日常工作中回家,有點累了!)。 – user2048881

+0

@ HeisenbugOk,我已經給了這個公平的去,它不適合我。而且我也剛剛意識到按進入保存評論,並沒有輸入新的一行。 – user2048881

+0

我有這樣的東西:var playfield : Playfield; function Start() { transform.parent = playfield.transform; transform.localRotation = Quaternion.identity; transform.localPosition += moveVector;這個腳本附加到我的「玩家」對象,並且除非該對象已經編輯到編輯器中的遊戲區對象(我的飛機)中,否則它將無法正常工作。因此,這種方法不適用於運行時實例化的東西(也就是其他所有東西)。我究竟做錯了什麼? (除了張貼代碼塊...我很抱歉,我不明白!) – user2048881

0

@Heisenbug:

#pragma strict 

var myTransform : Transform; // for caching 
var playfield : GameObject; 
playfield = new GameObject("Playfield"); 
var gameManager : GameManager; // used to call all global game related functions 
var playerSpeed : float; 
var horizontalMovementLimit : float; // stops the player leaving the view 
var verticalMovementLimit : float; 
var fireRate : float = 0.1; // time between shots 
private var nextFire : float = 0; // used to time the next shot 
var playerBulletSpeed : float; 



function Start() 
{ 
    //playfield = Playfield; 
    myTransform = transform; // caching the transform is faster than accessing 'transform' directly 
    myTransform.parent = playfield.transform; 
    print(myTransform.parent); 
    myTransform.localRotation = Quaternion.identity; 

} 

function Update() 
{ 
    // read movement inputs 
    var horizontalMove = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime; 
    var verticalMove = (playerSpeed * Input.GetAxis("Vertical")) * Time.deltaTime; 
    var moveVector = new Vector3(horizontalMove, 0, verticalMove); 
    moveVector = Vector3.ClampMagnitude(moveVector, playerSpeed * Time.deltaTime); // prevents the player moving above its max speed on diagonals 

    // move the player 
    myTransform.localPosition += moveVector;