Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity 5.1 Animator Controller not transitioning

I have created an Animator Controller (called Player) and assigned it to the Animator field of my humanoid avatar, as well as simple animation states with suitable transitions. Please see the two attached images.

I have attached a script, containing the following code, to my avatar game object, but I wonder what I am missing or doing wrong that the transition from Idle to Walk does not take place, even though I can see that the speed variable increases when I press W.

Could someone please help me understand the problem?

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


1 Answers

The problem in your code is, animator.SetTrigger("Walk"); gets called in a single frame when you pressed the key and animator.SetTrigger("Idle"); gets called for the rest of the frames.

Try changing Input.GetKeyDown( KeyCode.W ) to Input.GetKey( KeyCode.W ). The former returns true only once, the instant when you press down the key, whereas the latter returns true until you release the key. Something like :

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

On a side note, you don't need the speed variable in the Animator to trigger walk animation, since you are already doing that using W.

Animator setup

Idle -> Walk enter image description here

Walk -> Idle enter image description here

like image 184
Nimesh Avatar answered Dec 04 '25 03:12

Nimesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!