- Home /
Syncing 30FPS animations in Unity to trigger events
Hi!
I'd like to sync some attack animations I made in an animation package, and trigger some events in Unity.
For example, I have this animation running at 30FPS in my animation software: IDLE 10 frames -> MOVE 5 frames -> ATTACK -> IDLE 10 frames
(the character is walking 'in place' for the MOVE part, and it sends out messages to the enemies for the ATTACK part)
The way I did it in Unity was:
IDLE:
yield return new WaitForSeconds(10F/30F);
MOVE:
float t = 5F/30F;
while (t>0)
{
characterController.SimpleMove(direction);
t-=Time.deltaTime;
yield return 0;
}
and then ATTACK and another WaitForSeconds(10F/30F);
I'm not sure I understood correctly how frames and time are related. So am I doing the right thing to sync the animations with the actions perfectly? (even if the game runs at 5FPS or 60FPS)
Thanks a lot!
Answer by Tetrad · Jun 10, 2010 at 10:00 PM
You probably want to use animation events.
Here's a video describing animation events: http://vimeo.com/11399349 Here's a video telling you how to edit (specifically, copy then edit) imported animations to add animation events: http://vimeo.com/11399550
And here's the documentation: http://unity3d.com/support/documentation/Components/animeditor-AnimationEvents.html
Copying/editing imported animations is a bit of a pain depending on your work flow, so you may consider adding your events programmatically: http://unity3d.com/support/documentation/ScriptReference/AnimationClip.AddEvent.html
Read that carefully, specifically this part:
Note that events added with AddEvent will only persist until play mode is exited of player is quit.
The way we do things is have an animation event setup state that gets called on game start that adds animation events to our clips once and only once. If you add the "same" event multiple times to the same animation clip, it'll fire multiple times (so don't have your enemy class add events to their clips every time you spawn an enemy, for example).
Thanks for your answer, Tetrad. Those animation events could prove really useful. I forgot to mention I'm using Unity iPhone 1.7, so I don't have access to those animation features in Unity.
So regarding my method, do you think the animations and the events would sync perfectly?
Thanks!
You don't have access to the fancy animation windows, but you can still add animation clip events procedurally, I think.
Answer by Mike 3 · Jun 25, 2010 at 01:31 AM
The code looks reasonable enough to me
Animation frames are done exactly how you seem to be thinking - they're just run at x frames a second, completely independent of the game FPS
Re. Animation events - they'll work except for the moving part of your code, you'd still need to do that via a coroutine or some form of update
Your answer
![](https://koobas.hobune.stream/wayback/20220613061842im_/https://answers.unity.com/themes/thub/images/avi.jpg)