- Home /
Mecanim animation events not working properly
Hi,
I have made bunch of animations and transitions between them. I'm trying to trigger other animations from the middle of the first fired animation. To clear this a little bit, first I have 2D sprite animation for the character to kick a football. This works fine and I'm using animator parameter trigger *KickTheBall" to start it from the script.
Now I should trigger a second animation for the ball to fly according to premade animation. This ball animation should be triggered in the middle of the kick animation, on a point where the characters leg is at right position. So I have put an animation event on a desired frame in kick animations timeline and for this I'm calling ballAnimation() function. The ballAnimation() function is just simple thing like this:
public void ballAnimation() {
ballAnimator.SetTrigger("BallKickedGoalRightUp");
Debug.Log ("BallAnimation Start");
}
So it just triggers some appropriate animation on a ball's animator and for the easy debugging purposes I have added an print out to the console. Now, usually this gets fired as I expect, but after a few runs within Unity's play mode the ball animation or some other animations triggered at the same manner don't fire anymore. I'm thinking that the problem lies somewhere in those animator's trigger parameters. Since the ballAnimation() function gets called in the right place. Where do I know this? Because the debug message of "BallAnimation Start" from within the ballAnimation() function is shown on the console, so the function has been called. Even so, the "BallKickedGoalRightUp" animation isn't triggered, and as I said before I'm using these animator trigger parameters to fire the appropriate animations. Or can it be that the “BallKickedGoalRightUp” trigger has also been called but it already got back to it’s initial state before the animator could fire the event related to that trigger? (Just a thought that came into my mind in the middle of writing this question). These problems seem quite random since everything works as expected usually at first, but then gets worse after a moment.
So, any suggestions why this might happen? Or should I try to do the animation and firing them in a completely different manner? I just thought that it would be an easy way to sequence appropriate animations from the initial animations timeline in this way. Also, I like the idea of setting the different animations on a state machine and setting transitions between them according to met conditions. I like the facts that I don't have to script everything manually and can manage the animations on a visual manner in the animation state machine. Because what's the purpose of having this kind of nice animation tool available if I still have to do all the hard work in the script, right?
Please point me to right direction if you think I should do the triggering in some other manner. And if there is a "right" way to do these animation sequences.
And I know there is already a question related closely to this topic (http://answers.unity3d.com/questions/806949/animation-events-not-firing.html) , but I thought that making my question as a comment below that original post wouldn't do much good. Or even not get answered :)
I'm running Unity version 4.6.0f3 on a Mac.
Thanks in advance.
It sounds like you may have a complicated Animator set up—try to create the simplest possible case where the problem still happens.
And watch the state-machine diagram while the trigger fires, successfully and unsuccessfully. What's happening in each case?
Thanks for the suggestion... I have tried to looking into those state diagrams when running but can't seem to find any pattern why different things will not fire anymore.
But I bumped into another strange thing, which could solve the problem. I was trying to edit the script attached to the ball object and tried to do something with it's 2D collider component. And suddenly Unity throws an exception in my face "$$anonymous$$issingComponentException: There is no 'Collider2D' attached to the "Ball(Clone)" game object, but a script is trying to access it." Well there is a collider component attached to the one and only ball object at the scene, but somehow it seems that the reference points at some Cloned ball object. I don't explicitly create any other ball objects to the scene. Does Unity some how keep the old objects in the memory from the previous runs? Or how in the hell is it referencing to some Ball(Clone) object... So can it be that the animations aren't fired because this cloned ball object wouldn't then either have it's Animator component attached to it, same way that it didn't have the collider anymore? Strange... I'll take a look at the scripts again to be sure that there isn't anything odd going on
I think I'll just try to delete the animations from within the animator and do the transitions again... maybe that will correct something. And I think in this way I can also test the simplest case at the same go as Victory Dan Greene suggested. Fingers crossed :)
Well, removing those animations and then redoing all the transitions and stuff didn't work. BUT... next thing I tried was to set the last animation event trigger one or two frames earlier... and what do you know, it seems to be working quite fine now! I'll report back if things take a bad turn again for some reason :) I hope that this now solved the problem.
So maybe it was just some ti$$anonymous$$g issue with the animations, but I really don't know what happened with those unexpected exceptions surfacing out of the blue :o Well, if it's not throwing them at me anymore, I'll just take those as oddities somehow related to the other issues
Your answer
Follow this Question
Related Questions
Why isn't my animation being played? 0 Answers
Why animation events do not change sprite position? 1 Answer
on trigger enter - play mecanim animation 0 Answers
Mecanim Triggers Not Deactivating 0 Answers