2017-07-18 96 views
0

你好,我有這個腳本,並在無效更新無關緊要什麼是第一次倒退或跑起來,它總是讓動畫師播放動畫向後走或運行播放動畫的一半或完整的方式,它在沒有被調用的情況下播放閒置狀態的一部分,這意味着你的手指仍然在按鈕上,所以它仍然必須反覆向前/向後播放動畫。 這裏是代碼:Unity角色控制器腳本和動畫師

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class playerController : MonoBehaviour { 
    public float moveSpeed = 10f; 
    public float turnSpeed = 50f; 
    Animator anim; 

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

    } 

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


     if (Input.GetKey (KeyCode.S)) { 
      anim.SetBool ("isIdle", false); 
      anim.SetBool ("isWalkingBack", true); 
      transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime); 


     } 
     else 
     { 
      anim.SetBool ("isIdle", true); 
      anim.SetBool ("isWalkingBack", false); 

     } 

     if (Input.GetKey (KeyCode.W)) { 
      anim.SetBool ("isRunning", true); 
      anim.SetBool ("isIdle", false); 
      transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime); 




     } 
     else 
     { 

      anim.SetBool ("isRunning", false); 
      anim.SetBool ("isIdle", true); 

     } 



} 


} 
` 

回答

0

使用布爾字段可能會造成一些問題,例如,你如上所述。

我建議使用:anim.SetInteger("unitState", someIntValue);

配置的連接和過渡的動畫與現場「unitState」工作。

在你的代碼,它會是這個樣子:

void Update() { 
    // for example 
    anim.SetInteger("unitState", 0); // 0 is Idle 
    if (Input.GetKey (KeyCode.S)) { 
     anim.SetInteger("unitState", -1); // -1 is WalkBack 
     transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime); 
    } 

    if (Input.GetKey (KeyCode.W)) { 
     anim.SetInteger("unitState", 1); // 1 is run forward 
     transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime); 
    } 

    if (Input.GetKeyDown (KeyCode.Space)) { 
     anim.SetInteger("unitState", 2); // 2 is jump 
     //Jump code here. for example 
    } 
    .... 
} 
+0

它仍然做同樣的事情 – Ghigh

+0

http://imgur.com/a/AemiZ – Ghigh

+0

好的nvm thanks ^^ – Ghigh

0

請確保您有未經檢查的退出時間在每一個過渡。