here’s the relevant block of code:
float xDiff = enemy1.transform.position.x - transform.position.x;
float yDiff = enemy1.transform.position.y - transform.position.y;
float zval = transform.position.z;
Vector3 attackangle = new Vector3 (xDiff, yDiff, zval);
// animate
Debug.Log("AttackEnemy");
if (Vector3.Angle(attackangle, Vector3.left) >= 0 && Vector3.Angle(attackangle, Vector3.left) <= 45)
{
Debug.Log("AttackEnemy");
animator.Play("HALeft2");
}
else if (Vector3.Angle(attackangle, Vector3.right) >= 0 && Vector3.Angle(attackangle, Vector3.right) <= 45)
{
animator.Play("HARight2");
}
else if (Vector3.Angle(attackangle, Vector3.up) >= 0 && Vector3.Angle(attackangle, Vector3.up) < 45)
{
animator.Play("HAUp2");
}
else if (Vector3.Angle(attackangle, Vector3.down) >= 0 && Vector3.Angle(attackangle, Vector3.down) < 45)
{
animator.Play("HADown2");
}
sometimes the attack left animation will play but mainly it plays the apparent default animation, which is move left. i had it playing the attack left animation and not the move left animation when the logic was wrong.
any ideas what could be wrong? i’m a mediocre mathematician. i sketched it out in paint and i think i did attackangle correctly.
this script is on the hero object btw, so transform.position refers to the hero’s position.
edit: sometimes i can get the correct animations to play, but it’s very finicky. not sure why.
edit2: maybe the vector i made doesn’t give the angle between hero and enemy? that is one avenue to investigate.


