Thursday, June 18, 2015

Animation Event in Unity3D

I wanted to know in an animation a few days ago when a certain animation I had transitioned to a second one. This was important to me as at the time I did not know all about the GetNextStateInfo to go along with my GetStateInfo. I ended up researching and writing code to test the current shortNameHash for the StateInfo and making sure I only changed my in code states values when the GetNextState was set to Zero. It worked great, but I really wanted to get the Animation events working, as their possibilities are endless.

Firstly lets add the event to the animation in Unity.
It is like a small pencil and it appears when clicked in the place of the active timeline marker.

Next up was to attach it to a function in my script... This is where I had my MAJOR issues.
Problem #1: Event could not see the script function
Problem #2: This happened when I solved problem #1, function would not show up.

Odd that both problems sound the same, but in fact they show up the same but required different solutions.

Solution #1: I found that the script had to be actually attached as a component at the SAME level as the Animator.
Solution #2: To get the function to show up (it didn't immediately), you have to KNOW that the function must have either an INT or FLOAT parameter. If you try to use a Vector or Bool or Array the function will not work, though it is possible you will get an error for this.

This is what the function chooser looks like for the event system. Nice when ya have a function actually show up. Finding out why it wasn't there was a bit of a bear.

This above is what my test script looked like. Placed as I said on the same level of the Object as the Animator.

Possibilities...
Are endless with the event system like this. You could simply use it to control and edit a whole sequence of fireworks to match up to a piece of music for example. Great way to use Unity :)
You could simply use it to create footfall effects of dust etc, or perhaps play a sound when the feet hit the floor.

Good Luck with your animations events

Da Voodoochief

No comments:

Post a Comment