- Home /
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.
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.
what do you mean by a single time? try turning off the loop in the animation
@arain55, I know how to run animation single time but my question is I want to start playing my default animation again.
At present what is happening!! $$anonymous$$y swat animation place once and then stop. Not started default animation again.
I have written code to run my default animation that I don't want.
Not used Animator for a while but let me try something: can't you set the SWAT→$$anonymous$$AIN transition to a bool that would always be true ?
@$$anonymous$$dWaylander, Basically my main(default) animation is always playing because it is general player movement animation. Swat animation is just hitting animation so at button press event I want to play it just single time then move back to original one (main animation). Something like this kind of animation playing I want.
I have set bool to play set animation but again set false to bool to play main animation again. That I don't want because it is ugly thing I am doing.
I hope that some better way definitely exist.
Answer by ben-rasooli · Dec 05, 2017 at 09:45 AM
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.
Answer by KaushikRahul · Apr 11, 2016 at 02:37 PM
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.
Answer by unity_bpbhLVPIKVrCcQ · Aug 31, 2021 at 10:17 PM
Create trigger parameter instead of bool
Set on "BabyGraySwatAnim -> BabyGrayMainAnim" transition Has Exit Time = true
Remove any conditions on "BabyGraySwatAnim -> BabyGrayMainAnim" transition
Your answer
![](https://koobas.hobune.stream/wayback/20220612074547im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer
Animation created via script does not play 1 Answer
Unity 5 Controlling Animation with UI Button 0 Answers
How do I activate an animation when a gameobject enter the Collider of the other animated object 2 Answers
Why does my transition freeze the second animation on it's first frame? 1 Answer