- Home /
Is it possible to fire an event on every animation frame?
I have a system where I depict the character's equipment by spawning & tethering additional sprites on top of their normal sprite. For instance, if the character has a sword equipped, then I spawn a sword on top of their character sprite and orient it so that it's in their hand.
One consequence of this is that my animations all need specific data about how to move around these equipment sprites on a per-frame basis. For example when the character swings their sword, the sword sprite needs to know to be offset by 4 on x, 2 on y and rotated by 90 in frame 1, 6 on x and -2 on y in frame 2, etc. - such that the sword is arcing naturally and always in the character's hand.
I am currently trying to script a system that will position & rotate those equipment sprites correctly each animation frame. However, the only way I can find to fire a function at the beginning of an animation frame is using the Animation Event system in the animation editor. This is a headache and a half, because it requires adding animation event to every frame of every animation in the game!
The best case scenario would be to be able to say "Whenever this gameObject's animator controller enters ANY new animation frame, return the index of that frame and fire this function to position their sword sprite." However I'm having trouble finding anything within the Unity API that allows me to do that. Any advice? Thanks!