2016-11-23 25 views
0

當遊戲正在運行時,我在關鍵字p上按任意鍵停止,我看到飛船越來越快地移動。但變量推力的價值永遠不會改變。那麼什麼樣的價值顯示和改變使得加速?什麼主意呢?如果我將推力從5改爲10?我如何顯示我的飛船的加速度值?

我添加到我的太空船的一個Rigidbody組件。

我這樣做的方式是正確的方式來加速我的飛船?

我想在OnGUI中顯示每次按下p鍵時加速度的值。

using UnityEngine; 
using System.Collections; 

public class ControlShip : MonoBehaviour { 

    public int rotationSpeed = 75; 
    public int movementspeed = 10; 
    private int thrust = 5; 

    Rigidbody _rigidbody; 

    void Start() { 

     _rigidbody = GetComponent<Rigidbody>(); 
     Debug.Log("Acc Speed: " + thrust); 
    } 

    void Update() { 

     var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f); 
     transform.Rotate(v3 * rotationSpeed * Time.deltaTime); 
     transform.position += transform.forward * Time.deltaTime * movementspeed; 

     if (Input.GetKey(KeyCode.Z)) 
      transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime); 

     if (Input.GetKey("p")) 
     { 
      _rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration); 
     } 
    } 

    void OnGUI() 
    { 
     GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + thrust); 
    } 
} 

更新

我現在修改後的腳本。當我按下P鍵飛船消失快,加速度的值是0的OnGUI所有的時間:

using UnityEngine; 
using System.Collections; 

public class ControlShip : MonoBehaviour { 

    public int rotationSpeed = 75; 
    public int movementspeed = 10; 
    private int thrust = 5; 

    bool isPKeyDown = false; 
    float acceleration = .0f; 

    Vector3 previousPosition = Vector3.zero; 

    Rigidbody _rigidbody; 

    // Use this for initialization 
    void Start() { 

     _rigidbody = GetComponent<Rigidbody>(); 
     Debug.Log("Acc Speed: " + thrust); 
    } 

    // Update is called once per frame 
    void Update() { 

     var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f); 
     transform.Rotate(v3 * rotationSpeed * Time.deltaTime); 
     transform.position += transform.forward * Time.deltaTime * movementspeed; 

     if (Input.GetKey(KeyCode.Z)) 
      transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime); 

     if (Input.GetKey("p")) 
     { 
      isPKeyDown = Input.GetKey("p"); 
      float distance = Vector3.Distance(previousPosition, transform.position); 
      float acceleration = distance/Mathf.Pow(Time.deltaTime, 2); 

      previousPosition = transform.position; 
      _rigidbody.AddRelativeForce(0f, 0f, acceleration, ForceMode.Acceleration); 
     } 
    } 

    void OnGUI() 
    { 
     if (isPKeyDown) 
     { 
      GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration); 
     } 
    } 
} 
+0

http://answers.unity3d.com/questions/48179/rigidbody-acceleration.html –

+0

你爲什麼想到'thrust'改變,當你從來沒有分配一個新的值它? – TheDjentleman

+0

簡單的童年數學應該告訴你在做什麼(或不做)。 – BugFinder

回答

1

從beginnig開始,重點是實際的力(該值的推動力量)爲你的星艦。所以價值越大,「推動」你的飛船越強大。

如果你的腳本作品則是的,它是將加速的一個很好的方式(這取決於你怎麼看)

爲了能夠表現出加速度值,當用戶按下「P」鍵,您應該計算出加速度值(how to calculate,或只是使用@Dan Wilson回答/評論)。

那麼你應該修改更新方法:

public void Update(){ 
    isPKeyDown = Input.GetKey("p"); 
    // ... rest of your code 
} 

更新您的類成員:

bool isPKeyDown = false; 
float acceleration = .0f; 

更新您的OnGUI方法:

public void OnGUI(){ 
    if (isPKeyDown) { 
     GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration); 
    } 
} 

,並計算你的加速和播放與剛體,我會建議使用固定更新:

public void FixedUpdate(){ 
    // calculate acceleration here... 
    acceleration = ... ; 
    if (isPKeyDown) { 
     _rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration); 
    } 
} 

編輯: 最容易找出加速的方式:

// as a member field 
Vector3 previousPosition = Vector3.Zero; 

// in update 
float distance = Vector3.Distance(previousPosition, transform.Position); 
float acceleration = distance/Mathf.Pow(Time.deltaTime, 2); 

previousPosition = transform.Position; 

EDIT2: updated code on pastebin

+0

你的答案和第二個答案有什麼區別?我的意思是你的計算和使用rigidbody.velocity一樣?我在問,因爲我不確定兩個答案之間有什麼區別,我的意思是學習我想知道是否存在以及有什麼區別? –

+0

在計算加速度的問題上幾乎沒有區別。但總的來說,這個答案的所有問題,而不僅僅是如何計算加速度值。 –

+0

請你看看我的問題,我根據你的答案用我的腳本更新了它。但是,當我按下p按鈕時,船快速消失,加速度值始終爲0.我想我在那裏做了很多錯誤的事情。我在我的問題底部添加了我的腳本。 –

1

我想你要尋找的是速度。要查看特定時間間隔內的加速度,您需要比較速度差異除以它們之間的時間。您打印的值5是您添加的值,而不是太空船的值。

acceleration = (rigidbody.velocity - lastVelocity)/Time.fixedDeltaTime; 
lastVelocity = rigidbody.velocity; 

Further reading with this example here

相關問題