Hello, my team and I are using animations triggers to handle the animation transition in a top-down 2d game.
We have a problem with the animation transitions, sometimes the tranitions between directions work fine but sometimes the wrong animations are triggered, even though when we have debugged the code, (for example Debug.Log(“WalkRight”) where it should be triggering this particular animation), the code seem to work as it should be. So I’m pretty sure the bug is in the transitions but we couldn’t find it.
This is the animation controller we made for the enemy:
This is an example of a transition:
[170176-animations1.png|170176]
And this is the code we use to decide which animation to trigger:
void ManageAnimation()
{
int Q = FindQuarter(moveDirection);
if (Q == 1)
{
if (moveDirection.x > (moveDirection.y + 0.5f))
{
anim.SetTrigger("EnemyWalkRight");
}
else
{
anim.SetTrigger("EnemyWalkUp");
}
}
else if (Q == 2)
{
if (Mathf.Abs(moveDirection.x) > moveDirection.y + 0.5f)
{
anim.SetTrigger("EnemyWalkLeft");
}
else
{
anim.SetTrigger("EnemyWalkUp");
}
}
else if (Q == 3)
{
if (Mathf.Abs(moveDirection.x) > Mathf.Abs(moveDirection.y) + 0.5f)
{
anim.SetTrigger("EnemyWalkLeft");
}
else
anim.SetTrigger("EnemyWalkFront");
}
else if (Q == 4)
{
if (moveDirection.x > Mathf.Abs(moveDirection.y) + 0.5f)
{
anim.SetTrigger("EnemyWalkRight");
}
else
anim.SetTrigger("EnemyWalkFront");
}
}
int FindQuarter(Vector2 direction)
{ // Finds the quarter that the end of the direction is in.
if (direction.x > 0 && direction.y > 0)
return 1;
else if (direction.x < 0 && direction.y > 0)
return 2;
else if (direction.x < 0 && direction.y < 0)
return 3;
else if (direction.x > 0 && direction.y < 0)
return 4;
return 0; // it is on one of the axis.
}
Can anyone tell what can be the problem?