2014-09-04 191 views
2

我試圖找到一種方法來暫停我的遊戲在Unity中,而不使用「Time.timeScale = 0;」。我想改變這一點的原因是我想讓角色在遊戲暫停時播放動畫。有沒有辦法改變這個腳本,以便在玩家點擊空間之後,重力和前進速度只能「設置」?還是有更好的方法來解決這個問題?暫停Unity遊戲沒有「Time.timeScale = 0」

這是腳本:

public class PlayerMove : MonoBehaviour { 

    Vector3 velocity = Vector3.zero; 
    public Vector3 gravity; 
    public Vector3 FlyVelocity; 
    public float maxSpeed = 5f; 
    public float forwardSpeed = 1f; 

    bool didFly = false; 
    bool dead = false; 
    float deathCooldown; 
    Animator animator; 

    // Use this for initialization 
    void Start() { 
     animator = GetComponentInChildren<Animator>(); 
    } 

    // Do Graphic & Input updates here 
    void Update(){ 
     if (dead) { 
      deathCooldown -= Time.deltaTime; 

      if (deathCooldown <= 0) { 
       if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) { 
       Application.LoadLevel(Application.loadedLevel); 
        } 
       } 
      } 
     if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) { 
      didFly = true; 
      animator.SetTrigger ("DoFly"); 
     } 
    } 

    void OnCollisionEnter2D(Collision2D collision) { 
     animator.SetTrigger ("Death"); 
     dead = true; 
    } 

    // Do physics engine updates here 
    void FixedUpdate() { 
     if (dead) 
      return; 

     velocity += gravity * Time.deltaTime; 
     velocity.x = forwardSpeed; 

     if(didFly == true) { 
      didFly = false; 
      velocity += FlyVelocity; 
     } 

     velocity = Vector3.ClampMagnitude(velocity, maxSpeed); 

     transform.position += velocity * Time.deltaTime; 

     deathCooldown = 0.5f; 
    } 
} 
+0

您可以在gamedev.stackexchange.com找到更多有關統一的幫助 – Sayse 2014-09-04 08:27:21

+0

沒有任何內容可以爲此而統一。但是我建議看一看狀態機,因爲當你有不同的狀態時(例如暫停/播放)以及當進入/退出狀態時需要完成的事情時,它們會提供幫助。 – Imapler 2014-09-04 08:28:07

+0

我知道這個線程有點舊,但你可以在這裏試試我的答案:http://stackoverflow.com/questions/31718168/pausing-in-unity/31718377#31718377 – NeverHopeless 2015-07-31 11:57:56

回答

0

如果您正在使用Mechanim你可以使用Animator.Update即使實際時間標度爲零更新從腳本動畫狀態。

不確定傳統動畫是否可以使用該技巧。