2015-12-02 97 views
1

嗨我工作在一個迷你2D遊戲統一,我只是創建一個2D角色和一些動畫(Up_Idle,Down_Idle,Right_Idle,Left_Idle/Up_run,Down_run,Right_run,Left_run) 問題在這些動畫不能正常工作(例如當我按下UPARROW的characte不玩Up_run動畫,但他在同一時間播放Up_run和left_run動畫)角色動畫師問題動畫不能正常工作

這裏的截圖:

enter image description here

這裏是代碼:

using UnityEngine; 
using System.Collections; 

public class movement : MonoBehaviour 
{ 

    public float speedX =1f; 

    public float speedY =1f; 

    Animator animator; 

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

    void Update() 
    { 
     if (Input.GetKey (KeyCode.UpArrow)) { 
        transform.Translate (new Vector2 (speedX, speedY) * Time.deltaTime); 
        animator.SetFloat ("Up", 1); 
        return; 
     } else { 
      animator.SetFloat ("Up", 0); 
     } 

     if (Input.GetKey (KeyCode.DownArrow)) { 
      transform.Translate (new Vector2 (-speedX, -speedY) * Time.deltaTime); 
      animator.SetFloat ("Down", 1); 
      return; 

     } else { 
      animator.SetFloat ("Down", 0); 
     } 

     if (Input.GetKey (KeyCode.RightArrow)) { 
      transform.Translate (new Vector2 (speedX, -speedY) * Time.deltaTime); 
      animator.SetFloat ("Right", 1); 
      return; 
     } else { 
      animator.SetFloat ("Right", 0); 
     } 

     if (Input.GetKey (KeyCode.LeftArrow)) { 
      transform.Translate (new Vector2 (-speedX, speedY) * Time.deltaTime); 
      animator.SetFloat ("Left", 1); 
      return; 
     } else { 
      animator.SetFloat ("Left", 0); 
     } 
    } 
} 
+0

嗯..我對Unity也不太瞭解......但我沒有看到所有的轉換。我不知道是否有一種方法可以在沒有過渡的情況下更改動畫......但是您說左側的動畫在某種程度上起作用。我的提示將檢查轉換。但我可能是錯的。 :O –

+0

我讀屏幕截圖的方式是:閒置是默認的。只有動畫師.SetFloat(「Up」,1);會觸發一些run_up。其他動畫看起來不可訪問。我的動畫看起來像一個有很多過渡的地獄哈哈 –

回答

0

私人動畫師動畫師;

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

} 

// Update is called once per frame 
void Update() 
{ 

    var vertical = Input.GetAxis("Vertical"); 
    var horizontal = Input.GetAxis("Horizontal"); 

    if (vertical > 0) 
    { 
     animator.SetInteger("Direction", 1); 
     transform.Translate(Vector2.up* Time.deltaTime); 
     //animator.SetInteger("Direction", 2); 
     animator.SetFloat("Speed", 1.0f); 
    } 
    else if (vertical < 0) 
    { 
     animator.SetInteger("Direction", 3); 
     transform.Translate(Vector3.down* Time.deltaTime); 
     //animator.SetInteger("Direction", 0); 
     animator.SetFloat("Speed", 1.0f); 
    } 
    else if (horizontal < 0) 
    { 
     animator.SetInteger("Direction", 4); 

      transform.Translate(Vector3.left* Time.deltaTime); 

     //transform.Translate(Vector2.right* Time.deltaTime); 
     animator.SetFloat("Speed", 1.0f); 
    } 
    else if (horizontal > 0) 
    { 

    animator.SetInteger("Direction", 2); 
    transform.Translate(Vector2.right* Time.deltaTime); 
    // animator.SetInteger("Direction", 4); 
    animator.SetFloat("Speed", 1.0f); 
    } 
    else 
    { 
     //animator.SetInteger("Direction", 6); 
     animator.SetFloat("Speed", 0.0f); 

    } 
} 
0

如果兩個動畫同時播放,則表示該條件用作2個圖的轉換。例如,您的idle_left - > run_left和idle_up - > run_up肯定具有相同的轉換條件(Up == 1.0,我認爲)。如果你想要動畫是獨立的,它們不應該有相同的轉換條件。

希望它有幫助。

+0

謝謝你,所以我想我得到它:) –

+0

歡迎:) – Lizlotte