- Home /
Mecanim Trigger event on specific frame of an animation
Without using collision detection, is there a way I can define a "hit" frame within an animation package that can be read by Unity to trigger an event?
I'm using a mecanim generic skeleton and doing the animations in Maya.
Answer by JesseBusch@IL · Mar 15, 2013 at 07:57 PM
While this may not let you get an event to occur on an exact frame of an animation, here is a trick I've been using to get a semblance of animation events out of Mecanim.
In Mecanim, you can add data curves to animations that will allow you to pull values from the animation system while its running. (To find out how to work with curves, check out this section of the Mecanim video tutorial
So, for example, let's say we have a boy who needs to throw a ball. WIth the techniques described in the video, I could add a curve to his throw animation. That curve would start and end the animation at 0, but about the time the boy needs to release the ball, I would make the curve spike up to above a value of 1.
You can then have a script watch and pull this float value from the animator, and if it detects the int value has gone above 1, it can do what it needs to do to populate the ball or do a hit or whatever event you need it to perform. You'll, of course, need to flush out the code a little bit so that the event only happens once during a single animation, but this should at least help you get going in the right direction.
Cool thanks, will this stay with the animation when its speed is modified in the Animator Controller ? Also just a mention to everyone else looking at this that Curves seems to be a Pro Only feature.
Answer by G1NurX · Mar 15, 2013 at 04:26 PM
https://www.assetstore.unity3d.com/#/content/5969 I think this tool meet your need.
Answer by HowBoring · May 13, 2014 at 07:20 AM
https://www.assetstore.unity3d.com/#/content/16357 This tool might be better. Just check it out.
Answer by Jason_DB · Jan 11, 2013 at 03:35 AM
I'm not overly familiar with mecanim so I can't be sure if this still applies, I think that this could be done with animation events, which allow you to call a function when a specific animation frame is played.
*Also, if Unity doesn't let you edit the animation, try making a duplicate of it - you can't edit animations directly in the imported file, but duplicating it creates an editable animation file.
Hey thanks for the reply, I looked into it and it looks like that solution won't work with mecanim :(
http://forum.unity3d.com/threads/156492-$$anonymous$$ecanim-and-Animation-Event
It seems like this is pretty crucial feature that they overlooked. I think I might try to do something in $$anonymous$$aya that I could then listen for in Unity. We'll see how it goes.
That's very unfortunate - animation events are really useful in a lot of different situations. If you can still access animation states in mecanim (http://docs.unity3d.com/Documentation/ScriptReference/AnimationState.html), you might be able to check the 'time' of the animation as an approximation for the frame.
Thanks,
I believe "AnimationState.normalizedTime" would be sufficient http://docs.unity3d.com/Documentation/ScriptReference/AnimationState-normalizedTime.html
However, I'm pretty sure I can't use that with $$anonymous$$ecanim as all the available options are listed here in the Animator class. I hope im wrong on this and someone can correct me. http://docs.unity3d.com/Documentation/ScriptReference/Animator.html
Thanks for the help, but if this is the case its pretty bad on Unity's part...
Your answer
Follow this Question
Related Questions
Why isn't my animation being played? 0 Answers
Mecanim animation events not working properly 0 Answers
on trigger enter - play mecanim animation 0 Answers
Clear mecanim trigger input? 1 Answer
Mecanim Trigger Stays too long true 1 Answer