在导入的角色动画中添加动画事件的方法

Unity3d: Game Progress and Creating Animation Events

January 2 2011

I recently changed jobs a couple of months ago and have been pretty busy trying to get up to speed with it. I have been neglecting this blog for a while, which is something that I don’t like.

For the Witch Training game I have been working on, the biggest thing that I am proud of myself right now is getting all of the controls about 90% done along with getting in the animation events to fire off walking/running sounds.

I wrote a post a little while ago on animation events describing what they are and how to do them in code. In the original article, I said how to just do it all in code because I don’t like the copying method. I firmly retract my statement and think that doing the copy method is the best way for looped animations. I did have to do some trickery to get it to work right though.

Check out the project so far.

Controls

walk – W/A/S/D
look around - Mouse
jump - spacebar
flight mode – hit space bar again when jumping
throw bomb – Mouse button (you can hold it down to give it more power. Use while on the ground or flying!)
run/fly faster modifer – left shift

Get Source download


(说了好多 他说他换工作了好像!!自己做了个小项目,培训用的貌似 没细看 废话掠过)

If you can’t here it, there is sound effects for every footstep whether the witch is walking or running. The way I did it is the following.

(如果你不here it (啥意思*&&),音效好脚步的动作同步,我的做法如下)本人觉得下面讲得很清楚了 不需要翻译了  请自行阅读偷笑

Animation States

To create animation events for locked animations, the best way to add events is to duplicate the animation state. What does that mean? It means that you need to select the animation in your project window and go to Edit > Copy . See image below.

animation state selecting

Copying the animation from model

The above image is the type of thing you want to copy. If you aren’t using the model in my project, Ramius is the character’s model I made in Blender3D. Unity will create all of these items based on your import settings. The animations were created in the import settings as well with the names you give them. Select the animation like above copy and paste it.

The reason why you have to copy it is because the original walk animation is really just a dynamically created setting that Unity creates from the source file. You can’t edit or save it because it will constantly rebuild itself everytime you load new files or update anything. The only way to do anything with these files is duplicate them so there is a static version. You can do things like animation events at that point.

Animation state copies sitting safely outside of the model

Once you have the copy, you can add it to your animation state on your game object  in your animation component. Once you’ve added a animation component to your gameobject you want to animate, select the object you have your animation component on and add the new references to it. See below.

I added the new run and walk animations to the “Elements”. I do that by usually selecting one of the small “0″ on the right side of the image. You can select your duplicated animation state. With the game object still selected go to Window > Animation in the top toolbar to start setting points.

Selecting animation state to add animation event

You will see that when you choose which animation you want to edit, most of them will say “Read-Only”. This is because these animations are getting pulled from Blender,the 3d program I am using, to get this animation data. You can’t edit or add and save animation events with these.

But that doesn’t matter since we already created a copy of what we want to use. You can see by the picture above that the run and walk animations don’t have “Read-only”, since we duplicated them.

Side Note: You must have your gameobject in the heirarchy selected to see your new animations with your object. If you select your 3d model in the project panel, your new animations won’t appear. This is because they aren’t linked to your model. That is what you were doing when you added them to the animation  component.

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 scrub the timeline. See below image.

Where to put the animation events

Once you scrub 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 run 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.

Again, here is a link to the Google code project that has the source files if you missed it at the top.

Get Source download

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值