2014-03-03 37 views
1

我有一個簡單的腳本,應該讓我的播放器動起來,但它不起作用。我讀了一段時間的論壇,一些有關於動畫遺留選項的問題,我修正了它,但是我的角色仍然沒有動畫,並且沒有任何編譯錯誤。這裏是我的腳本:unity animation.play()不起作用

using UnityEngine; 
using System.Collections; 

public class maincharacter : MonoBehaviour { 

    void Start() { 
    } 

    float xSpeed = 10f; 
    float zSpeed = 10f; 
    public float playerMovementSpeed = 10f; 

    void Update() { 
     float deltaX = Input.GetAxis ("Horizontal") * xSpeed; 
     float deltaZ = Input.GetAxis ("Vertical") * zSpeed; 

     Vector3 trans = new Vector3 (deltaX + deltaZ ,0f,deltaZ - deltaX); 
     transform.Translate (trans.normalized * Time.deltaTime * playerMovementSpeed, Space.World); 

     animation.Play ("mcrunsw"); 

     /*if (deltaX != 0 || deltaZ != 0) { 
       animation.Play ("mcrunsw"); 
      }*/ 
    } 
} 

這是我的遊戲對象和動畫: enter image description here

任何幫助,將不勝感激。提前致謝。

回答

6

manual

播放()將開始屏幕動畫,動畫的名稱,或播放默認 動畫。動畫將突然播放而不進行任何混合。

既然你每調用一幀,我都會假設它會顯示動畫的第一幀,然後在下一個動畫中停止播放。試試這個:

if (!animation.isPlaying) { 
    animation.Play(); 
} 

一般來說,我會建議使用Mechanim進行角色動畫。

+0

使用Mechanim所做的工作,再加上其更易於使用多個動畫和狀態的使用。非常感謝。 –

1

另一個可行的選擇以及上面的解決方案是CrossFadeQueued。

你只需簡單地使用下面的功能,它會無縫CrossFade。

使用率PlayAnimation("mcrunsw");

function PlayAnimation(AnimName : String) { 
    if (!animation.IsPlaying(AnimName)) 
     animation.CrossFadeQueued(AnimName, 0.3, QueueMode.PlayNow); 
} 
相關問題