2014-01-21 31 views
2

我想在Unity中構建一個小行星風格的遊戲,真的可以使用一些幫助。我相信我所有的數學運算都是正確的,但我無法在Unity內部使用它。我有幾個不同的問題。Unity2D小行星風格遊戲

  1. 船不與速度更新(如果你開始移動,然後鬆手,它仍然會站)

  2. 我不確定在Unity如何設置船舶旋轉到我的特定角度。

任何幫助將不勝感激。

public class playerController : MonoBehaviour { 

    public static float timer; 
    public static bool timeStarted = false; 

    Vector2 accel; 
    Vector2 velocity; 

    float direction; 
    float angle; 
    float shotCooldown; 
    float speed; 

    const float pi = 3.141592f; 
    const float maxSpeed = 300; 
    const float maxAccel = 500; 

    void Start() { 
     timeStarted = true; 
    } 

    void Update() { 

     if (timeStarted == true) { 
      timer += Time.deltaTime; 
     } 

     shotCooldown -= (timer%60); 

     angle = direction * pi/180; 

     if (Input.GetKey(KeyCode.W)) { 
      accel.y = -Mathf.Cos(angle) * maxAccel; 
      accel.x = Mathf.Sin(angle) * maxAccel; 
      velocity += accel * Time.deltaTime; 
     } 

     if (Input.GetKey(KeyCode.S)) { 
      accel.y = -Mathf.Cos(angle) * maxAccel; 
      accel.x = Mathf.Sin(angle) * maxAccel; 
      velocity -= accel * Time.deltaTime; 
     } 

     if (Input.GetKey(KeyCode.Space)) { 
      if (shotCooldown <= 0) 
      { 
       // Create new shot by instantiating a bullet with current position and angle 
       shotCooldown = .25f; 
      } 
     } 

     if (Input.GetKey(KeyCode.D)) { 
      direction += 360 * Time.deltaTime; 
     } 

     if (Input.GetKey(KeyCode.A)) { 
      direction -= 360 * Time.deltaTime; 
     } 
     /* 
     if (this.transform.position.x >= Screen.width && velocity.x > 0) { 
      this.transform.position.x = 0; 
     }*/ 

     while (direction < 0) { 
       direction += 360; 
     } 
     while (direction > 360) { 
       direction -= 360; 
     } 

     speed = Mathf.Sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y)); 

     if (speed > maxSpeed) { 
       Vector2 normalizedVector = velocity; 
       normalizedVector.x = normalizedVector.x/speed; 
       normalizedVector.y = normalizedVector.y/speed; 
       velocity = normalizedVector * maxSpeed; 
     } 

     this.transform.position = velocity * Time.deltaTime; 

     transform.rotation = Quaternion.AngleAxis(0, new Vector3(0,0,angle)); 

    } 
} 

回答

1

它通常是一個壞主意,設置的位置你的方式,因爲你沒有實際使用任何物理。你這樣做的方式,velocity是船的新位置,而不是速度。一旦你放開了鑰匙,它將停止計算新的位置,從而停止移動。

有幾個選擇這將使一個更好的結果:

1)的一種方法可以做到這一點是通過調用:transform.Translate(Vector3.forward * speed * Time.deltaTime) Vector3.forward應該符合你認爲是「向前」的方向,但如果沒有,您可以將其更改爲任何作品(例如Vector3.up)。這意味着你只需要計算一個速度,讓其他人統一。 2)如果你在你的船上使用了一個剛體,你可以簡單地做: rigidbody.AddForce(Vector3.forward * speed * Time.deltaTime)這會自動加速船在給定的方向上,無論你給它的速度。

至於輪換,也許嘗試這樣的事情:

if (Input.GetKey(KeyCode.D)) 
{ 
    Vector3 newRotation = transform.rotation.eulerAngles; 
    newRotation.z += 10; 
    transform.rotation = Quaternion.Euler (newRotation); 
}  
else if (Input.GetKey(KeyCode.A)) 
{ 
    Vector3 newRotation = transform.rotation.eulerAngles; 
    newRotation.z -= 10; 
    transform.rotation = Quaternion.Euler (newRotation); 
}