How do I let an animation state finish playing before I can change the animation state?

So I am basically working on a tactics turn-based strategy game where I want to play the “attack” animation state only once and have it automatically gracefully transition to the “neutral” animation state that I built once the “attack” animation state finishes playing.

if (tokens[0].Equals(“attack”))

			gameManager.units[Int32.Parse (tokens[1])].GetComponent<Animator>().SetBool("attackLocked", false);

			gameManager.pMana -= gameManager.units[Int32.Parse (tokens[1])].atkCost;
			print ("attacker position x:" + gameManager.units[Int32.Parse (tokens[1])].transform.position.x);
			print ("attacker position z:" + gameManager.units[Int32.Parse (tokens[1])].transform.position.z);
			print ("attacked position x:" + targetTileX);
			print ("attacked position z:" + targetTileZ);
			print ("attacker animation state #:" + gameManager.units[Int32.Parse (tokens[1])].GetComponent<Animator>().GetInteger("mode_and_dir"));
			//Use the values assigned to targetTileX and targetTileZ from TileScript.cs:
			//Attack animation based on the position of the tile that is going to be attacked
			if(gameManager.units[Int32.Parse (tokens[1])].transform.position.z > (targetTileZ*10)){
				gameManager.units[Int32.Parse (tokens[1])].GetComponent<Animator>().SetInteger("mode_and_dir", 8);
			else if(gameManager.units[Int32.Parse (tokens[1])].transform.position.z < (targetTileZ*10)){
				gameManager.units[Int32.Parse (tokens[1])].GetComponent<Animator>().SetInteger("mode_and_dir", 9);
			else if(gameManager.units[Int32.Parse (tokens[1])].transform.position.x > (targetTileX*10)){
				gameManager.units[Int32.Parse (tokens[1])].GetComponent<Animator>().SetInteger("mode_and_dir", 10);
			else if(gameManager.units[Int32.Parse (tokens[1])].transform.position.x < (targetTileX*10)){
				gameManager.units[Int32.Parse (tokens[1])].GetComponent<Animator>().SetInteger("mode_and_dir", 11);


Once the integer “mode_and_dir” is set, the attack animation state then plays as soon as the value is passed into the variable. In this function, how do I let the animation finish playing afterwards, before I change the value of “mode_and_dir” again?

I cannot understand exactly what you want to do, can you explain more? maybe use a flow:
state finish >> do something >> state xxx…something like that :frowning: I am stupid sorry.

Anyway, if you want to get the status of the current animation (as you have mentioned you want to do something after the animation finish?), maybe what you need is stateinfo…try this:

AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
		float animPercentage = stateInfo.normalizedTime - Mathf.Floor(stateInfo.normalizedTime);

animator is the Animator of your gameobject, while animPercentage is the current Percentage of the current animation.
Put it in update() to monitor the progress. You may also need to get the current animatiion name by this:

static int someState = Animator.StringToHash("xxx.yyyyyyyyyy");


if(stateInfo.nameHash == someState){
// something when playing someState animation

where xxx is the LAYER NAME of your state inside the Animator and yyyyyyyyyyy is the name of your state.

Hope it helps.