2017-07-18 97 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

请确保您有未经检查的退出时间在每一个过渡。