2016-06-13 61 views
1

我非常流利地使用Unity,但是對於Mechanim和動畫我目前並不是太大,所以請不要給我太多的困難時間。所以我有這個布爾這是在我的遊戲管理器的腳本:「3,2,1,GO」將布爾值設置爲True後進行動畫播放?

public bool countDownDone = false; 

這個布爾被設置爲true,一旦我的倒數計時器在我的比賽開始時結束。布爾值設置爲true後,我的遊戲中的所有內容都會啓動。例如:

我有這個爬行動畫,在遊戲的最初階段播放(即使在定時器結束之前)並永久循環。我的問題是,一旦布爾值設置爲「true」,我怎麼才能讓爬行動畫也啓動?或者我只是將它應用於CoRoutine並在3秒後播放?我對是否引用動畫或動畫師進行了廣泛的研究,我也對此有所疑惑。有什麼建議?如果您需要更多圖片或我的問題的詳細信息,請告訴我。謝謝! :)

animation state

NEW下面的代碼

using UnityEngine; 
using System.Collections; 

public class Crawling : MonoBehaviour { 

    Animator animator; 

    private GameManagerScript GMS; 

    void Start() 
    { 
     animator = GetComponent<Animator>(); 

     GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript>(); 

    } 

    void Update() 
    { 
     if (GMS.countDownDone == true) 
     { 
      animator.Play("Main Character Crawling", 1); 
     } 
    } 
} 

回答

0

我有這樣的爬行動畫在 遊戲的最開始玩(即使在計時器結束之前)和lo ops永遠。我的問題是, 我該如何讓爬行動畫也啓動一旦布爾值爲 設置爲「true」?

解決方案是從腳本播放動畫。 刪除當前的Animation

選擇您的PlaneMover腳本附加到的GameObject,將Animation和Animator組件附加到它。確保動畫的Play Automatically未被選中。

public class PlaneMover : MonoBehaviour { 
private GameManagerScript GMS; // Reference to my GameManager Script 

    public float scrollSpeed; 
    public float tileSizeZAxis; 
    public float acceleration; //This has to be a negative number in the inspector. Since our plane is coming down. 

    private Vector3 startPosition; 

    Animation animation; 
    public AnimationClip animationClip; //Assign from Editor 

    void Start() { 

     GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript>(); //Finds the Script at the first frame 

     // Transform position of the quad 
     startPosition = transform.position; 

     animation = GetComponent<Animation>(); 

     //Add crawing Animation 
     animation.AddClip(animationClip, "Crawling"); 
     //Add other animation clips here too if there are otheres 
    } 

    void Update() 
    { 

     if (GMS.countDownDone) //Everything starts once the countdown ends. 
     { 

      /* Every frame - time in the game times the assigned scroll speed 
       and never exceed the length of the tile that we assign */ 
      float newPosition = Mathf.Repeat (Time.time * scrollSpeed, tileSizeZAxis); 

      // New position equal to the start position plus vector3forward times new position 
      transform.position = startPosition + Vector3.forward * newPosition; // was vector3.forward 

      scrollSpeed += Time.deltaTime * acceleration; // This makes the plane increase in speed over time with 
                  // whatever our acceleration is set to. 

      //Play Animation 
      animation.PlayQueued("Crawling", QueueMode.CompleteOthers); 
     } 
    } 
} } 
+0

是安全的統一爲使用傳統的動畫呢?我讀到他們是如何試圖完成這一切並與Mechanim(Animator)完全一致的。爲了讓我使用你所提供給我的東西,我必須將我的動畫片段更改爲Legacy格式:) @Programmer –

+1

@StephenGeorge你是對的。這是舊的,但Unity不會很快刪除它,因爲許多項目仍在使用它。我只是檢查Unity的網站,它不會很快被刪除。建議您使用新的Animator API。 – Programmer

+1

@StephenGeorge我的回答幾乎與此相同。在我的答案中簡單地移除動畫API並將其替換爲Animator。使用'Animator.Play(「stateName」)'來播放它。 'stateName'是在Mechanim中創建的動畫狀態的名稱。 – Programmer

0

我解決了這個問題! 這是我的腳本:

using UnityEngine; 
using System.Collections; 

public class Crawling : MonoBehaviour { 

    public Animator animator; 

    private GameManagerScript GMS; 

    void Start() 
    { 
     animator = GetComponent<Animator>(); 

     GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript>(); 

    } 

    void Update() 
    { 
     if (GMS.countDownDone == true) { 
      animator.enabled = true; 
     } 
     else 
     { 
      animator.enabled = false; 
     } 
    } 
} 

我要做的只是讓我在每次「countDownDone」變成了「真正的」檢察附加動畫,並增加安全性,我增加了「其他」爲它被禁用。如果有人注意到我改進腳本的方法,請告訴我。謝謝:)

較短的版本:

if (GMS.countDownDone) 
animator.enabled = true; 
else 
animator.enabled = false; 
+1

因爲'countDownDone'是bool,所以你可以做(​​GMS.countDownDone)。另外,由於您只在if和else語句中運行一個命令,因此可以通過刪除兩個'{}'來縮短它。所以現在你有'if(GMS.countDownDone) animator.enabled = true; else animator.enabled = false;' – Programmer

+0

感謝您的改進:) @Programmer –

相關問題