- Home /
Is it possible to turn off/suppress event calls in an animation at runtime?
Okay so I'm trying to make my animations as fluid as possible. I have a 2D Prefab consisting of various sprites to form one sprite where the parts can be moved around during animation. It also uses animation events to call a script that swaps the textures of the individual sprites during the animations, such as a hand opening and closing. The problem arises when I transition from one animation state to another. During the transition, it runs animation events from both animations, which sometimes causes the sprite to "spasm", IE it starts to flicker between the sprites that the first animation wants, and the sprites that the second animation wants.
I could fix this by making the transition happen instantly, but that would make the animation less fluid. I'd just prefer that the first animation stops triggering events altogether to let the second animation's events take precedence.
I don't care if the solution is rather roundabout, it's just that I've been wracking my brain for a while over this. Some new input could make me spring some new ideas.
Answer by Pyrian · Apr 13, 2014 at 04:39 PM
If roundabout is okay, just trigger a script at the beginning of each animation that sets a flag, which you use to decide which script calls to honor or ignore, inside those scripts themselves.
@Pyrian That sounds like something that could work in theory. But in an animation that loops, wouldn't that flag get set each time? At least, I haven't found away to enter a script into an animation on startup outside of the timeline loop, so to speak. Is there a way to do that? Again, roundabout is fine.
Well, if the flag is always be referring to the most recent animation that begun, then setting it each loop is still correct and not enough processor power to worry about. If you're doing more than setting a flag and you don't want it to repeat in loops, just check said flag, and only do your process if you're actually changing it.
If there's an event call upon an animation transition beginning, I couldn't find it.
Well, the thing is, during a transition, the events for both animations will get fired. So the flag would keep swapping between the two.
That being said, a solution has been for$$anonymous$$g in the back of my $$anonymous$$d. It involves changing the function calls in the animation events to send an AnimationEvent object. If I'm right about my hunch, I can use that to identify the animation state that is sending the object. Then I can easily rig the called method to only do something when the call comes from either the animation that's currently running, or the animation that is being transitioned to.
$$anonymous$$aybe you should use IsInTransition, possibly combined with GetAnimatorTransitionInfo and its nameHash, to make absolutely sure whether you should run your script given the current transition state. That should be bulletproof, I think.
For some reason, the AnimationEvent.animationState property gives me a NullReferenceException error. I am not sure why it does that. I mean, I -could- just add a tag to the String parameter and identify the animation that sends the object that way, but that's really no fun. :(
Answer by Mr-Jun · Dec 12, 2018 at 01:08 AM
Seems you can set the Animator fireEvents attribute manually.
GetComponent<Animator>().fireEvents = false;