2014-02-11 96 views
1

我當前已得到了我的角色在空中飛行與下面的腳本飛行角色

#pragma strict 

var cMotor: CharacterMotor; // reference to the CharacterMotor script 

function Start(){ // get the CharacterMotor script at Start: 
cMotor = GetComponent(CharacterMotor); 
} 

function Update(){ // move player upwards while F is pressed 
if (Input.GetKey("f")){ 
cMotor.SetVelocity(Vector3.up*10.5);} 
else if (Input.GetKey("up")){ 
cMotor.SetVelocity(Vector3.forward*10.5); 
} 
else if (Input.GetKey("left")){ 
cMotor.SetVelocity(Vector3.left*10.5); 
} 
else if (Input.GetKey("down")){ 
cMotor.SetVelocity(Vector3.back*10.5); 
} 
else if (Input.GetKey("right")){ 
cMotor.SetVelocity(Vector3.right*10.5); 
} 
else if (Input.GetKey("g")){ 
cMotor.SetVelocity(Vector3.down*10.5); 
} 
} 

// This do-nothing function is included just to avoid error messages 
// because SetVelocity tries to call it 

function OnExternalVelocity(){ 
} 

但是我希望能夠凍結角色在空中,使他們不落,但如果我設置maxFallSpeed爲0,那麼我不能再讓角色降落。有任何想法嗎?

+0

我相信這是java腳本,值得將該標記添加到您的問題。 – 2014-02-11 09:19:35

回答

0

我不熟悉CharacterMotor,因爲它不是默認的unity3d腳本,而是您自己編寫或從教程中編寫的東西。 但是考慮到這一點,我可能會建議你在沒有按鍵時禁用剛性體。

function Update() { 
    if(!Input.anyKey) { 
     Rigidbody.mass = 0.0f; 
     Rigidbody.drag = 50; 
    } else { 
     Rigidbody.mass = 1.0f; // or the stored variabled of the initial mass 
     Rigidbody.drag = 0.7f; 
    } 
} 

它是一個粗略的方法,但它會工作。問題在於,如果你的角色馬達使用了任何charactercontroller.move或simplemove,那麼也會拖動角色。

如果是這樣,給你的字符馬達添加一個布爾值,當做重力檢查時,只需禁用帶有Input.anyKey的bool,如果任何鍵被按下,將返回true,如果沒有按下按鈕,則返回false。

編輯:代碼更改如下面的註釋中所定義。

+0

剛體沒有'enabled'字段。它是一個'Component',但它不是'MonoBehavior'。您需要改爲調用'rigidBody.Sleep()'和'rigidBody.WakeUp()'。 –

+0

對不起,我的錯誤,其實也是如此,你也可以改變它的質量,將它設置爲0會讓玩家停留在空中,但仍然能夠處理任何其他環境變化。 –

+0

這會阻止重力加速它,但不會阻止它...... –

1

試試這個,使用默認的「w,s,a,d」來移動,「f,g」用於上下。

#pragma strict 

var cMotor: CharacterMotor; // reference to the CharacterMotor script 

function Start(){ // get the CharacterMotor script at Start: 
    cMotor = GetComponent(CharacterMotor); 
} 

function Update(){ // move player upwards while F is pressed 

if (Input.GetKey("f")){ 
    cMotor.SetVelocity(Vector3.up*6); 
}else if(Input.GetKeyUp("f")){ 
    cMotor.SetVelocity(Vector3.up*0); 
} 


if(Input.GetKey("g")){ 
    cMotor.SetVelocity(Vector3.down*6); 
}else if(Input.GetKeyUp("g")) { 
    cMotor.SetVelocity(Vector3.down*0); 
} 
} 

// This do-nothing function is included just to avoid error messages 
// because SetVelocity tries to call it 

function OnExternalVelocity(){ 
}