Unity 5.1 Animator Controller未转换

时间:2015-07-22 13:08:02

标签: animation unity3d

我创建了一个Animator Controller(称为Player),并将其分配到我的人形头像的Animator字段,以及具有合适过渡的简单动画状态。请参阅附带的两张图片。

我已经将一个包含以下代码的脚本附加到我的头像游戏对象中,但我想知道我遗失或错误的是从IdleWalk的转换没有发生,即使按下speed我可以看到W变量增加。

有人可以帮我理解这个问题吗?

using UnityEngine;
using System.Collections;

public class CharAnim : MonoBehaviour
{
    private Animator animator;
    float speed;

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

    void Update()
    {    
      animator.SetFloat( "speed", Input.GetAxis("Vertical") );

      if ( Input.GetKeyDown( KeyCode.W ) && ( speed > 0.5f ) ) 
      {
          animator.SetTrigger("Walk");
      }
      else 
      {
          animator.SetTrigger("Idle");
      }
    }
}

X

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:3)

代码中的问题是,当您按下键时,animator.SetTrigger("Walk");会在一个帧中被调用,而其余帧会调用animator.SetTrigger("Idle");

尝试将Input.GetKeyDown( KeyCode.W )更改为Input.GetKey( KeyCode.W )。前者在按下键时只返回一次,而后者在您释放键之前返回true。类似的东西:

void Update ()
{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}

在旁注中,您不需要speed中的Animator变量来触发漫步动画,因为您已经使用W进行了此操作。

Animator setup

空闲 - &gt;步行 enter image description here

步行 - &gt;闲 enter image description here

答案 1 :(得分:0)

{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");
    }
    else            
        animator.SetTrigger("Idle");
}{
    if(Input.GetKey(KeyCode.W))
    {
        animator.SetTrigger("Walk");