Best method for controlling 2D sprite with transition/in-between animations?

I’ve got a pretty basic setup:

Player can only move left or right.
I have looping animations for Idle, Left and Right.

I also have animations that play between all of them:
“Idle to Left,” “Left to Idle,” “Idle to Right,” “Right to Idle,” “Left to Right” and “Right to Left”

I did originally set this all up using triggers, the state machine looks like an insane web, and for the most part it works, but it’s buggy. The player will get stuck in certain animations sometimes, or won’t continue through the transition animation, etc.

I’m sure my code is the culprit, but I’m looking for a way to redo the whole thing properly.

Are there any tutorials out there for this? All I can find are controlling basic sprites: Idle to Run, Idle to Jump, etc… nothing with transition animations.

I’m not sure if I should set this up using triggers, having a “Speed” float, bools, etc, or a combination of them.

I just feel like I am making this way more complicated than it has to be.

Here is the main part of my code that controls the animation:

if (moveHorizontal < 0) { 
	if (GlobalControl.direction == "right") {
		resetAllTriggers ();		
		animator.SetTrigger ("playerRightToLeft");
	} else {
		resetAllTriggers ();
		animator.SetTrigger ("playerStraightToLeft");
		GlobalControl.direction = "left";
	}

}

if (moveHorizontal > 0) {

	if (GlobalControl.direction == "left") {
		resetAllTriggers ();
		animator.SetTrigger ("playerLeftToRight");
	} else {
		resetAllTriggers ();
		animator.SetTrigger ("playerStraightToRight");
		GlobalControl.direction = "right";	
	}
}

if (moveHorizontal == 0 
	&& (animator.GetCurrentAnimatorStateInfo (0).IsName ("Player-LeftToRight") == false) 
	&& (animator.GetCurrentAnimatorStateInfo (0).IsName ("Player-RightToLeft") == false)) { 

	if (GlobalControl.direction == "left") {
		resetAllTriggers ();
		animator.SetTrigger ("playerLeftToStraight");	

	} else if (GlobalControl.direction == "right") {
		resetAllTriggers ();
		animator.SetTrigger ("playerRightToStraight");
	}
}

The GlobalControl.direction is also being set in the “Left to Right,” “Right to Left,” and “Right/Left to Straight” onStateExit() callback.

It all just seems so convoluted and I feel there must be a better way!

Any suggestions on how to go about this would be great!
Thanks so much.

This tutorial should help you get the basics of what you need to do:

Here's the script
     Animator anim;
     
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
         
     }
     
     // Update is called once per frame
     void Update () {
 
     
         float input_x = Input.GetAxisRaw("Horizontal");
         float input_y = Input.GetAxisRaw("Vertical");
         
         bool isWalking = (Mathf.Abs(input_x) + Mathf.Abs(input_y)) > 0;
         
         anim.SetBool("isWalking", isWalking);
         if(isWalking)
         {
             anim.SetFloat("x", input_x);
             anim.SetFloat("y", input_y);
             
             transform.position += new Vector3(input_x, input_y, 0).normalized * Time.deltaTime;
             
             
         }
     }
 }

Hello man i suggest making a sort type of fsm to movement/animation logic, actually having all this in code its a lot more usefull. Use blend trees and just call animator.play(“AnimName”);