Play animation single time and move back to default animation

As per my title suggest I want to play my other animation single time and then move back to default animation. Here I give my actual example so that you will become more clear.

67744-screen-shot-2016-04-10-at-91711-pm.png

Here in above image, “BabyGrayMainAnim” is my default animation and “BabyGraySwatAnim” is my other animation that I want to play. In this, “BabyGraySwatAnim” I want to play single then my default animation play as normal. At present I have set bool condition for this and written following code:

    public void HitFlyingBirds ()
	{
		if (myAnimator.GetCurrentAnimatorStateInfo (0).IsName ("BabyGraySwatAnim"))
			return;

		myAnimator.SetBool ("IsSwat", true);
		StartCoroutine (RunBabyGrayMainAnimation ());
	}

	IEnumerator RunBabyGrayMainAnimation ()
	{
		yield return new WaitForSeconds (1f);
		myAnimator.SetBool ("IsSwat", false);
	}

But using Enumerator for this purpose is not that much good as per my understanding.
I want some better way to implement same thing. If you want more information then ask me for it.
Please give some suggestion for this.

Use myAnimator.SetTrigger (“swat”); And of course you need to have that parameter “swat” set in your Animator tab.

When you wanna play an animation only once and then transition to previous state, you need to use trigger parameter, not bool. And then on the triggered animation, you set what should happen when it’s finished. For your case, you can simply leave the transition condition empty and it’ll go back to previous state when the animation is finished.

  1. Create trigger parameter instead of bool
  2. Set on “BabyGraySwatAnim → BabyGrayMainAnim” transition Has Exit Time = true
  3. Remove any conditions on “BabyGraySwatAnim → BabyGrayMainAnim” transition

Okey just to tell you that is not the Ugly way to do it. It is how its done for the most part if we don’t want to use a lot of parameters to control out animator.

if you still don’t want to do it, the other way around is make separate parameters for separate animations then from the “Any State” state make a transition to your animation and set the parameter to true of what animation you want to run.

But do make sure, every time you want to play any other animation (the parameter you are going to create for your main animation as mentioned above), you have to set it false or the any state will keep calling your main animation and won’t let you play other animations.