- Home /
Are the Animation Events the only way to go?
Hello everyone.
So, I'm working with Unity for some time now and most of the things are nice, but some of them seem quite strange for me. Maybe this will make you laugh and all, but I would love to hear your point of view.
My question is about unity Animation Events. From what I understand, they're the main, if not the only, way to execute some code at the specific moment of an animation. Maybe I'm wrong, but, honestly, that bothers me. I worked on multiple projects involving improving code of other people who utilized animation events, and in most cases the thought "Oh, that must be executed from the animation clip" was far from obvious. The necessity of searching the project for the right animation clip to edit some behaviour still looks like a joke to me when we're talking about easily readable code and such. This situation brings my memories of Flash days, when you could do the similar thing - place the code in the animation frames of clips. I can't even start to explain how frustrating this is.
So, good, honorable and experienced people of Unity. Can you please tell me where I'm wrong? Is there any other method of designing animation-related behaviours and I'm just overlooked some API? Please, point me to the right direction! (All I saw for now is AnimationState.normalizedTime and it's harldy an alternative.) Or are you not bothered at all with animation events? Maybe there are some methods to work with events and avoid frustration. Share them, please! I would love to hear any thoughts on this subjects, thank all of you in advance. :)
Answer by tanoshimi · Jan 30, 2017 at 04:25 PM
I don't have an issue with them. Although I normally prefer to see things in script rather than in a GUI, I'm not sure how else you're proposing, say, firing an event on exactly the frame in which a character's foot touches the floor other than by attaching it to the animation clip itself? Using code to attach an event to an arbitrary frame number or timestep through the animation seems much more obtuse to me.
To be clear, the only code I place in the animation clip is an event handler. So, in the frame that the foot touches the floor, I fire the OnStep() event. The code that plays the footstep SFX, leaves a footprint, or produces a dustcloud are nowhere in the animation clip - they live in their own classes and simply subscribe to the OnStep event. If you've got any logic or substantial code in your animation clip handler then I'd say you're doing it wrong.
Thank you for your answer!
Do you have any conventions about using such events? I mean, if I open your code, I see only event handlers, which are listening to, for instance, OnStep and doing something with it. How am I supposed to know who are firing it? If I want to change the behavior in some way, how I supposed to know where to look? You can have dozens of animation clips for your character, and opening each of them searching the event and altering doesn't seem like a good practice.
Answer by Meatloaf4 · Sep 17, 2017 at 05:20 PM
@Mifodii You are indeed correct, it can be very hard to track down these functions do to the constraints you stated. As is often the case with Unity, the power they give you to visually produce things rather then doing them in code comes with a price.
This is where conventions come in, I do think that Unity should have a much better bug tracking system in place for this as well though.
Answer by spesifikbrush · Nov 20, 2020 at 11:01 AM
I know this is an old question, and I don't know if this situation got better, but this is what I do when I'm dealing with 2D sprite sheet animations:
Reference the AnimationClip object.
Get the time between frames with 1f / clip.frameRate
Specify the frame where I want to fire an event (for example, weapon animation wind up ends at frame 5, frame 6 will be where the attack collision check should happen)
With a simple timer logic called in update (more precisely, in a state machine), execute code when timer hits the point.
It works really well. It may work with non-sheet animations, but I don't know how to integrate transition, exit times, or if it would break with blend animations or not.
As @tanoshimi suggested, simply creating C# events and subscribing to them would be much simpler. Maybe if Unity turns AnimationEvents to ScriptableObject assets, it would be much simpler.
Your answer
Follow this Question
Related Questions
Animation events firing all at once in mirrored blend tree 1 Answer
How can you check for player input during specific frames of an animation? 1 Answer
multiple Animation Events in an Animation 0 Answers
Animation Events added with code seem to be at wrong time 0 Answers
Is there a way to select animation events on the dopesheet beside clicking on them? 0 Answers