TL;DR: Image and script should be pretty much self-explanatory.
I’m working on a 2D game and I’m using Blend trees to determine which direction to look to (and animation to play) via mouse/player position.
When attacking, if fast enough, the animation changes midway (attacking left and if I move the mouse up the it changes to attack up midway). Is there a way to make sure the animation plays out until the end (ignore mouse movements for the duration of the animation)?
Relevant script:
public class PlayerController : MonoBehaviour {
private Animator anim;
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
//Attacking
if (Input.GetButtonDown("Fire1")) {
anim.SetTrigger("Attack");
}
// Looking Direction
//Finding out the angle
Vector3 player = Camera.main.WorldToScreenPoint(gameObject.transform.position);
Vector2 playerPos = new Vector2 (player.x, player.y);
Vector2 mousePos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
Vector2 v = mousePos - playerPos;
var angleRadians = Mathf.Atan2(v.y, v.x);
var angleDegrees = angleRadians * Mathf.Rad2Deg;
if (angleDegrees < 0) {
angleDegrees += 360;
}
//Setting the direction
if (angleDegrees > 315) {
anim.SetFloat ("LookDir", 3);
} else if (angleDegrees < 45) {
anim.SetFloat ("LookDir", 3);
} else if (angleDegrees > 225) {
anim.SetFloat ("LookDir", 0);
} else if (angleDegrees > 135) {
anim.SetFloat ("LookDir", 1);
} else if (angleDegrees > 45) {
anim.SetFloat ("LookDir", 2);
}
}
}
Thanks!