2013-02-15 44 views
1

我正在使用C#,我目前正在構建第三人稱視角遊戲。我有一個在幀的動畫一個3D人物模型,所以我必須通過幀削減動畫如何管理動畫?

的問題是我現在有5個動畫(空閒,跑步,走路,打孔,跳躍),這是我的代碼

void Update() { 
    if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){ 
     //play run animation 
    } else { 
     if (motion.x != 0 || motion.z != 0){ 
      motion.x = 0; 
      motion.z = 0; 
     } 
        //play idle anim 
    } 

    if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded){ 
    //play jump anim 
    } 

    if (Input.GetKeyDown(KeyCode.P)){ 
     //play punch anim 
    } 

    if (!controller.isGrounded){ 
     //play jump anim 
    } 

    vertVelo -= gravity * Time.deltaTime; 
    motion.y = vertVelo; 
    this.controller.Move(motion * Time.deltaTime); 
} 

當我按P鍵進行字符打擊時會出現問題。看起來更新函數中的空閒動畫正在被調用,所以拳擊動畫沒有時間播放。

那麼,有什麼解決方案?有沒有動畫管理技術,或者我應該使用延遲?

+0

這個if語句沒用:if(motion.x!= 0 || motion.z!= 0)。你可以總是將motion.x和motion.z設置爲0 – Toad 2013-02-15 18:41:19

回答

0

嘗試在Unity中對動畫圖層進行搜索並在動畫之間進行混合。

您可以在具有較高圖層編號的圖層上設置anim循環,覆蓋較低圖層。

這意味着你可以在第1層上有空閒循環,並且這種連續播放。然後說你的跳轉循環在第2層上。當跳轉邏輯觸發跳轉循環時,它會播放一次,然後空閒循環繼續。

在團結「AnimationState」這個文檔也可能是有用的> link <

1

而衝頭打你可能會阻止空閒動畫(但可能它不是最好的方法):

bool isPunchPlaying = false; 

void Update() { 
    //... 
    if (!isPunchPlaying) { 
     // Play idle anim 
    } 
    // ... 
    if (Input.GetKeyDown(KeyCode.P)){ 
     //play punch anim 
     isPunchPlaying = true; 
     StartCoroutine(WaitThenDoPunch(animation["punch"].length)); 
    } 
    // ... 
} 

IEnumerator WaitThenDoPunch(float time) { 
    yield return new WaitForSeconds(time); 
    isPunchPlaying = false; 
} 
+0

我喜歡這個,它基本上是一個[動畫狀態機]的精簡版本(http://docs.unity3d.com/Documentation/Manual/AnimationStateMachines .html),並添加到Unity 4.還有[社區腳本](http://wiki.unity3d.com/index.php?title=Finite_State_Machine),您可以使用它們來實現類似的結果。 – Jerdak 2013-02-18 14:54:54