- Home /
Triggering an animation once in mecanim
Hi there,
Within one of my animators I am tracking whether or not to transition into another state using a bool and the SetBool() function.
However I seem to have the problem of if I turn the bool back to being false mid animation, the transition goes back to my idle too quickly and doesn't play the animation all the way through, even if the conditions-to-transition-back-to-idle are set to ExitTime&bool=false.
Can anyone tell me what I need to do to set the bool back to being false at the right time / how I can get mecanim to play an animation once?
If possible as well I'd also like to have the ability to interrupt the animation and get it to transition to its beginning (in my case 'shoot a gun animation with a little recoil afterwards getting interrupted by firing the gun again').
I'm very new w/ $$anonymous$$ecanim, but I didn't think you could have multiple transition conditions?
I would set the condition to start animation be bool = true, and the condition to end animation be exit time, so that it goes back to idle as soon as it's done playing your animation, and it doesn't matter that bool only was true for one frame.
you click the plus button in the transition window to get a new AND logic-gate transition. The problem there is that the moment you set it the bool to being false again the animation goes back too early, it doesn't wait until the end of the anim. And if you don't set the bool to being false again the character will never play idle again, it'll keep seeing the bool is true and jump right back into the other animation.
Can you set the bool to false via code? That's what I've been doing. Basically I said, "if bool = true && timePast > 0.5" set bool = false. The time is arbitrary, but long enough to be more than one frame, but not longer than the animation. As long as the transition out of the animation isn't when bool=false, it should be fine I think.
$$anonymous$$ake sure that on the state in mecanim/animator tab that Has Exit Time is checked.
Answer by TonyLi · Jun 24, 2013 at 01:28 PM
The example scripts provided by Unity use GetCurrentAnimatorStateInfo(). In Update(), it checks if you're in the "shoot gun" state. If so, it clears the bool. Check the bazooka script in the IK scene (http://u3d.as/content/unity-technologies/mecanim-example-scenes/3Bs).
It sounds like you've maybe marked the shoot-->idle transition as non-Atomic. Try ticking the Atomic checkbox. Also, is the exit time correct?
Otherwise, I'd guess it's in the logic of how/when you're setting the bool. You should be able to clear the bool as soon as you're in the shoot state (actually, as soon as the transition to shoot has started).
If you clear it at this point, then you have two choices for interrupting the animation and starting it over right away:
Create a transition from Any State-->shoot, or
Create a null state. Create a transition from shoot-->null (if bool is true) and then null--> shoot (again if bool is true).
It's a shame there isn't a a nicer interface for forcing transitions...
Answer by s_guy · Nov 20, 2013 at 01:44 AM
The easiest way is to use the "Trigger" parameter type for the transition in the Animator state editor. It's a bool that sets itself to false immediately after the transition.
I was using animation events and scripted the bool reset before I came across this.
Yeah, this is new for Unity 4.3, so not something that wasn't available until this past week.
The trigger feature is $$anonymous$$ILLER.
Lucky me that I found it by accident. I don't think it's even documented yet.
Would you $$anonymous$$d taking a look at this animation question I had?
http://answers.unity3d.com/questions/579213/how-do-you-hold-an-animation-on-the-last-frame-wit.html
This trigger very help (i think about anim events) but i found this post and... my two pistol system easy simple on mecanim i have blend tree (PistolState(idle/run)) and than two transitions PistolL and PistolR(and some name trigger)
if(WeaponState.IsName("Weapon.Pistol-shotR")){ secondShot = true;}
if(WeaponState.IsName("Weapon.Pistol-shotL")){ secondShot = false;}
// if one pistol only this
if(Input.GetButtonDown("Fire") && secondShot && WeaponState.IsName("Pistol.PistolState")) {
animator.SetTrigger("PistolShotR");
Shoot(PistolR);
}
if(Input.GetButtonDown("Fire") && !secondShot && WeaponState.IsName("Pistol.PistolState")) {
animator.SetTrigger("PistolShotR");
Shoot(PistolL);
}
"The trigger feature is $$anonymous$$ILLER." I am agree)
P.S. But 1 update you must configure Transitions to PistolShot state had time to bummed 1 times
Answer by beit · Jul 04, 2015 at 12:38 PM
A bit from here a bit from there I get it solved, here is the how to:
Create an animation by dragging X sprites onto the scene
Open the animation controller that is created (you can find it on the sprite asset folder)
Create a trigger shouldAnimate (on top left you find parameters, click on +)
Create an empty state
Set the default transition to the empty state
From the new state add a transition to the animation state
Under the transition condition add the shouldAnimate trigger
Create an empty transition from the animation to the new state
From code call myAnimator.setTrigger("shouldAnimate")
ENJOY :D
Cheers
Thank you! I have been searching all over the internet, but this is the first simple explanation this I have found!
Great! Weird that it was so hard to find a simple description on how to wire up the trigger. I was missing the empty transition and things got all buggy.
Answer by m4a44 · Sep 21, 2013 at 06:25 PM
Click on your transition and set it to "Solo". That should force the animation to play fully before it goes back (even if you set the bool to false before the full playthrough).
Good point. But you still need a reliable way to set the bool to false after the transition.
I've just set them false in a late update. Not the best, but it works... Hopefully Unity will add a feature to modify parameters soon...
Nice, but what if you're moving from a state that is also in solo and not yet able to transition? The flag would be reset before the state changes.
Have a check in the late update to see if you are in the desired state? I haven't needed to play around with that yet...