- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
 
- 纳金币
- -1
- 精华
- 11
|
Setting Animation Events
Once you have your animation state selected ( I have the walk animation state selected), you can begin adding events. If you bring up Scene view behind, you can see your model actually moving when you s***b the timeline. See below image.
![]()
Where to put the animation events
Once you s***b your animation to know where you want an event. You can create an event there (right most icon on the play toolbar). If you select the event, you will notice that there are no options for what it wants to do. We will do that next.
Binding functions to Events
In order for the animation events to to fire off a function for an event, it needs to know where it is. The way animation events work, there has to be a script attached to the gameobject that has the animation component as well. See below.
![]()
Adding a script for animation events
I added a script to the same place the animation component is. You can create functions in there and that is what the animation window tool will see.
public void walkAnimationEvent()
{
Debug.Log("Foot is down walking");
audio.PlayOneShot(walkSound);
audio.volume = 0.4f;
}
Once you add a function like the one above, you can go back to your animation window and select the event again. This time your new function will show up. When you***n the game now, you will see that the event will fire every time the animation hits that frame. Success!
Extra Credit
Looping animations such as walking are a little more complicated from what I have done with them. Since they are looping, sometimes the fire off more than they should. To solve it, I have add an enum that has what animation state the character is in. It will only do the function if it is firing AND if it is in the right state. You can download the “Witch Training Project” to see if you are interested. It still needs a little tweaking since the walk sound lingers a little too long – but you the idea
I haven’t seen this in-depth of animation events on many blog posts, so hopefully this will help in understanding and using them in your project. Happy game making!
|
|