BLACKISH DEV BLOG Support
 

 

Unity: Animation-Events

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…)
Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.

4 Comments

    Mk on October 21, 2009 at 14:13 | Permalink

    That's a great hypothetical example!

    Infiniteunity3D on October 21, 2009 at 15:06 | Permalink

    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!

    Anonymous on October 21, 2009 at 19:55 | Permalink

    good tip!

    i was using coroutines for the same but i'll try this out

    hk網頁設計 on December 30, 2009 at 10:44 | Permalink

    thanks for your"Unity: Animation-Events"

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>