找回密码
 立即注册
查看: 6439|回复: 87

[源码] Unity3D动作事件运用及示例游戏完整项目包

[复制链接]
发表于 2012-12-17 12:51 | 显示全部楼层 |阅读模式
资源信息 Asset Information
资源名称: Unity3D动作事件运用及示例游戏完整项目包 (发帖教程)
版本: (版本)
资源等级: 1
资源格式: .unitypackage (链接失效请点击帖子右下方举报通知管理员)
资源大小: 20MB (默认:MB)
下载地址: 站内下载 (购买积分)
点击查看原图
美丽分割线
本帖最后由 money 于 2012-12-17 12:52 编辑

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.

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.

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.

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
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!


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

评分

参与人数 2 +2 收起 理由
yugo215 + 1 很给力!
graywolfx21 + 1 赞一个!

查看全部评分

发表于 2013-3-5 12:56 | 显示全部楼层
{:5_412:}{:5_412:}{:5_412:}
发表于 2013-7-1 15:29 | 显示全部楼层
感谢楼主分享
发表于 2013-7-7 11:09 | 显示全部楼层
哇, 这个好,感谢楼主~
发表于 2013-7-8 18:10 | 显示全部楼层
fffffffffffffffffffffffffffffffff
发表于 2013-7-9 12:05 | 显示全部楼层
学习学习哈哈~!
发表于 2013-7-27 18:41 | 显示全部楼层
很好的学习资源,多谢分享
发表于 2013-12-14 03:54 | 显示全部楼层
不错,谢谢楼主共享
发表于 2014-8-31 11:33 | 显示全部楼层
看看先了啊啊啊啊
发表于 2015-3-30 20:38 | 显示全部楼层
马克一下。。虽然看到全是英文就不想下咋了。。
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 19:44 , Processed in 0.252913 second(s), 35 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表