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.