2016-11-23 47 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

相关问题