Wednesday, September 18, 2013

Mecanim and Unity Pro: How to match events to actions cleanly.

Today, I'll be discussing a method I'm experimenting with in Unity Pro, but I'll also talk about how to use a similar feature in basic Unity as well.

As you know, there is not event system built into mecanim currently, and the workaround is to use Curves to make variable spikes that you can check on to see what float value they have.

For example:  You have an animation of a man firing a gun.  You want to have the attack even happen during the actual firing moment of the animation, or about 0.13 seconds in.  You could make a coroutine to wait that long, but what happens if the animation is interrupted by the character's death?  Then you would have to put in even more options to check for such other events.

In the case of Curves, you can add a floating point curve named "FIRING" to the animation of the gun firing, and have it jump to 1.0 when it reaches the moment the gun actually fires.  Next you add a FIRING variable to the Mecanim Animator system you're using for the character. Then you have your code check each frame, and if the curve get's above 0.9, then run the damage code to attack the target.

This works fine, but it has a small flaw:  If you get a low framerate, you can actually miss the firing frame that gets the FIRING variable above 0.9.  It could jump from 0.8, miss several frames, and then run 0.7 as it leaves the spike you made.

So, how to fix that?  Simple.  Use an upward curve that ends in a sudden falloff at the moment that the attack should happen.

You then have your code check to see if the current FIRING value is less than the last frame's FIRING value.  If so, then trigger damage.  This will work even when many frames are being skipped, and prevents odd glitches.

But what if I can't use curves?

Well, this method depends on you actually making the animations, so if you're getting animations from someone and cannot edit them, it won't work.  If you do make your own animations, then do this:

Attach some additional bones to your character, and name them after the event they will represent.  Then treat them like curves and have them move (or rotate) based on the event you want to represent.  I used to use a bone that moved from near the feet to up near the head to show something happening, such as being a trigger for a particle effect.  All you will need to do is watch that bone on your code to change in order to use it as a trigger.

No comments:

Post a Comment