AnimationEvents are a way to make sure that something happens at a specific point during an animation. You boss-character stomping and shaking the ground at frame 65? No problem… Frame 65 / 30 fps = 2.166 sec. Add an AnimationEvent to the AnimationClip and a function will be called every time this AnimationClip is played at the specified time!
var boss : GameObject;
//create and set up the AnimationEvent
var myEvent : AnimationEvent = new AnimationEvent();
myEvent.time = 2.166;
myEvent.functionName = "DoSomething";
//Add the event to an AnimationClip
boss.animation.clip.AddEvent(myEvent);
//Play the animation
boss.animation.Play();
Now simply add all the code you need to the function DoSomething in a script on the gameObject that contains the animation.
From what I can tell the function really has to be on a script on that gameObject and nowhere else. myEvent.functionName = “transform.parent.DoSomething”; didn’t seem to want to work…
The documentation points out that what actually happens is gameObject.SendMessage(animationEvent.functionName, animationEvent); - so I guess that explains why.
Update: Have a look at this new post for how to make it call a function on a different object (spoiler: I relay the message…)
4 Comments
That's a great hypothetical example!
Great! Animation is something that I've wanted to get into with Unity but I have an Event-Based mindset and I didn't realize that feature was in there!
good tip!
i was using coroutines for the same but i'll try this out
thanks for your"Unity: Animation-Events"
About col000r
Markus HoferIndie Game Developer
Austria, Europe
GAMES
NEW ORBITAge of Curling
Speedlap Red
RECENT POSTS
anyonemost peopleMERCH