My Animation Controller doesn't go from Idle to Moving animations

Hey there

I’m still fairly new at Unity and Animation is a mystery to me most of the time. Or rather, doing it well is a mystery to me.

I followed a tutorial on how to set up a Link to the Past sprite of Link with idle animations and moving animations in 4 directions. I have two Parameters:

  • Direction - An Integer

  • IsMoving - A Boolean

Direction goes from 0-3 and IsMoving is simply to determine whether I Should switch to an Idle Animation (which is just one frame of standing still) or if I should cycle through a set of animations to give the illusion of movement. But whenever I press any of the directional keys (WASD) it does move me across the playing field because I am moving a rigidbody, but the movement animations don’t play. I just slide around in an idle pose, though it does face the right direction at least.

The tutorial instructed me to remove the Transition Duration, setting it to 0 on all animations, so I have done that but putting the transition time back on those animations make them work again, albeit with a noticeable delay between me letting go of a button and the moving animation turning back to idle.

Here’s the controller:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    private Animator animator;
    private Rigidbody2D rBody;
    void Start()
    {
        animator = this.GetComponent<Animator>();
        rBody = this.GetComponent<Rigidbody2D>();
        animator.SetInteger("Direction", 2);
    }

    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        ManageMovement(h, v);
    }

    void ManageMovement(float horizontal, float vertical)
    {
        if (horizontal != 0f || vertical != 0f)
        {
            animator.SetBool("IsMoving", true);
            AnimateWalk(horizontal, vertical);
        }
        else
        {
            animator.SetBool("IsMoving", false);
        }

        Vector3 movement = new Vector3(horizontal, vertical, 0);
        rBody.velocity = movement;
    }

    void AnimateWalk(float h, float v)
    {
        if (animator)
        {
            if ((v > 0) && (v > h))
            {
                // Up
                animator.SetInteger("Direction", 0);
            }

            if ((h > 0) && (v < h))
            {
                // Right
                animator.SetInteger("Direction", 1);
            }

            if ((v < 0) && (v < h))
            {
                // Down
                animator.SetInteger("Direction", 2);
            }

            if ((h < 0) && (v > h))
            {
                // Left
                animator.SetInteger("Direction", 3);
            }
        }
    }
}



If you need anything else let me know.

It’s 2D or 3D project?

It’s a 2D project.

Any help?

The code seems ok. The transitions aren’t shown in any screenshots so we can’t tell you if something is amiss. At a glance, I’m guessing that the transition from ‘any state’ to the ‘Moving’ sub-state might be the culprit. I’ve never tried to set something up like this so I’m not entirely sure. Try watching the animator live as you play and see what parameters and transitions happen when.

You might consider a different approach that uses four layers, one for each direction. You’d only need two states and two transitions then. Or a blend tree would work as well.

As a follow-up example, here is how I have our four-way character animator setup. Each layer is synced to ‘South’ so I only need to make one change in any layer and all four directions will match.

2617136--183662--Capture1.PNG 2617136--183663--Capture2.PNG 2617136--183664--Capture3.PNG

1 Like