找回密码
 立即注册
查看: 6689|回复: 93

[基础] Unity3D 角色动画中文教程

[复制链接]
发表于 2012-11-22 11:48 | 显示全部楼层 |阅读模式
资源信息 Tutorial Information
教程名称: Unity3D 角色动画中文教程(发帖教程)
适用引擎:   (适用引擎,为空默认为Unity)
教程语种: 中文
教程等级: 1
教程格式: 图文(请用IE9以上浏览器访问本版块)
教程作者: 转载自互联网 (如有问题请短消息联系作者或发表回复)
下载地址: (兑换积分)
点击查看原图
美丽分割线

Character Animation
角色动画

Unity Manual > User Guide > Creating Gameplay > Character Animation
***Unity 手册 >        使用指南 >        创建游戏 >        角色动画
Unity's Animation System allows you to create beautifully animated skinned characters. The Animation
System supports animation blending, mixing, additive animations, walk cycle time synchronization,
animation layers, control over all aspects of the animation playback (time, speed, blend-weights), mesh
skinning with 1, 2 or 4 bones per vertex and finally physically based ragdolls.  


Unity 的动画系统允许你创建漂亮的动画角色.动画系统支持动画合成,混合,添加动画,步调周期时间同步,动
画层,控制所有方面的动画回放(时间,速度,混合-偏重),网格面每个三角有 1,2 或 4 个骨骼,且完成基本的物理
玩偶.


There are some best practices for creating a rigged character with optimal performance in Unity. It is
recommended that you read about these techniques on the Modeling Optimized Characters page.  


这里有一些最好的方法创建一个作弊的角色在Unity获得最好的展示.推荐你阅读下Modeling Optimized
Characters页面上的教程。


Making an animated character involves two things; moving them through the world and animating them
accordingly.


制作一个动画角色需要 2 个东西;移动他在世界中和做相应的动作.


This page focuses on the animation. If you want to learn more about moving characters around (for a
Super Mario Bros style game or a first-person shooter), take a look at the Character Controller page.  


这个页面致力于动画.如果你想要学习更多关于移动角色的(为一个超级玛丽风格游戏或第一人称射击),查
看Character Controller page.


If you like, quickly jump to one of the topics covered on this page:  


Importing Character Animations   
Animation Splitting   
Multiple Files   
Inverse Kinematics   
Inserting Into a Unity Scene   
Animating the Character   
Animation Blending   
Animation Layers   
Additive Animation   




如果你喜欢,直接跳到一个在这个页面的标题:

输入角色动画
动画片段
多个文件
反向运动
插入 Unity 场景
使角色做动作
动画混合
动画层
添加动画

You can download an example demo showing pre-setup animated characters here.  


你能在here下载演示动画角色设置的例子example demo。


Importing The Animations

输入动画

First of all we have to import the character. Unity natively imports Maya (.mb or .ma) files, Cinema 4D
(.c4d) files, and fbx files which can be exported from most animation packages. Click here to learn how
to export from your modelling/animation package.  


首先我们输入动画。Unity支持输入Maya (.mb or .ma)文件, Cinema 4D (.c4d)文件,和fbx文件,这些能输出
为很多动画包.点击这里学习怎样输出模型/动画包export from your modelling/animation package.


Importing Animations using Animation Splitting
输出动画使用动画片段
The most convenient way for animators to work is to have a single model containing all animations.
When importing the animated model, you can define which frames make up each part of the animation.
Unity will automatically split the animation into the individual parts, called Animation Clips.  


最方便的制作动画的方法是使用单一的模型包含所有的动画.当输入动画模型,你能定义每部分动画的
帧.Unity 将自动把动画片段分成单个动作部分,名为动画片段 Animation Clips.


For example:  


walk animation during frames 1 - 33  
run animation during frames 41 - 57  
kick animation during frames 81 - 97  

例如:

走路动画在帧 1 -33
跑步动画在帧 41 – 57
踢的动画在帧 81 - 97




To import the animations you simply place the model in the Assets folder of your project. Unity will now
automatically import it. Highlight it in the Project View and edit the Import Settings in the Inspector.  


输入动画你可以直接放置他到工程里的 Assets 文件夹.Unity 将自动输入他.突出显示他在工程视图和编辑
输入设置在检视视图.



The Import Settings Dialog for a mesh
一个模型的输入设置对话框

In the Import Settings, the Split Animations table is where you tell Unity which frames in your asset file
make up which Animation Clip. The names you specify here are used to activate them in your game.  




在输入设置, Split Animations 表是告诉 Unity 你的资源文件的哪些帧是表示哪些动画片段的.你指定在这
里的名字在你的游戏使用触发他们.

name
first frame
last frame
loop frame

名字
起始帧
终止帧
循环帧



Defines the Animation Clip's name within Unity.
The first frame of the animation. The frame number refers to the same frame as in
the 3D program used to create the animation.
The last frame of the animation.
If enabled, an extra loop frame is inserted at the end of the animation. This frame
matches the first frame in the clip. Use this if you want to make a looping animation
and the first & last frames don't match up exactly.


定义 Unity 里动画片段的名字.
动画的起始帧.帧数值和创建动画的 3D 程序使用相同的帧.
动画的终止帧
如果激活,额外的循环帧插入到动画的最后.这个帧匹配片段的起始帧.如果你想要制
作一个循环动画和起始&终止帧不是很准确的匹配时.


Importing Animations using multiple model files
输入动画使用多个模型文件
The other way to import animations is to follow the @ animation naming scheme. You create separate
model files and use this naming convention: 'model name'@'animation name'.fbx  


另一个输入动画的方法是在动画名后面加@.你创建单独的模型文件和使用这种命名方法'模型名字'@'动画
名字'.fbx



An example of four animation files for an animated character

有 4 个动画文件的动画角色例子





Unity automatically imports all four files and collects all animations to the file without the @ sign in. In the
example above, the goober.mb file will be set up to reference idle, jump, walk and wallJump
automatically.  


Unity 自动输入所有 4个文件并收集所有动画到没有@标签的文件.在上面的例子里, goober.mb 将自动建立
空闲,跳跃,走路和翻墙的引用.


Importing Inverse Kinematics
输入反向运动
When importing animated characters from Maya that are created using IK, you have to check the Bake
IK & simulation box in the Import Settings. Otherwise, your character will not animate correctly.  


当输入动画角色使用 Maya 会创建使用 IK,你可以检查 Bake IK & simulation 盒在输入设定.否则你的角色
将不能正常运动.


Bringing the character into the Scene

放置角色到场景

When you have imported your model you drag the object from the Project view into the Scene View or
Hierarchy.  


当你输入模型后,拖拽物体从工程视图到场景视图或层次视图.



The animated character is added by dragging it into the scene
动画角色通过拖拽添加到场景

The character above has three animations in the animation list and no default animation. You can add
more animations to the character by dragging animation clips from the Project View on to the character
(in either the Hierarchy or Scene View). This will also set the default animation. When you hit Play, the
default animation will be played.  




上面角色的三个动作列表不是缺省动作.你能添加更多动画到角色通过在工程视图的角色中拖拽动画片段
(到层次或场景视图).这也将设置缺省动画.当你按下运行,缺省动画将运行.


TIP: You can use this to quickly test if your animation plays back correctly. Also use the Wrap Mode to
view different behaviors of the animation, especially looping.   


技术:你能使用这个快速测试,如果你的动画重播正常.也可使用卷模式 Wrap Mode 观察不同的动画行为.特
别是循环.


Animating the Character

使角色动起来

The actual animating of characters is done through Unity's scripting interface.  


真实的角色动画使用 Unity 脚本接口执行.


Animation Blending

动画合成
In today's games, animation blending is an essential feature to ensure that characters have smooth
animations. Animators create separate animations, e.g. a walk cycle, run cycle, idle animation or shoot
animation. At any point in time in your game you need to be able to transition from the idle animation into
the walk cycle and vice versa. Of course you don't want any sudden jumps in the motion, you want the
animation to smoothly transition.  


现在的游戏,动画合成是一个必不可少的特征为确保角色有平滑的动画.动画师创建单个动画,如走路循环,跑
步循环,空闲动画或射击动画.有些时候你的游戏需要能转换空闲动画到走路循环或反之.当然你不想要在运
动中有突然的跳跃,你想要动画平滑转换.


This is where animation blending comes in. In Unity you can have an arbitrary amount of animations
playing on the same character. All animations are blended or added together to generate the final
animation.


这就是动画合成到来的原因.在Unity 你能在同一个角色身上有任意多个动画播放.所有的动画合成或添加到
一起产生最终动画.  


Our first step will be to make a character smoothly blend between the idle and walk animations. In order
to make our job simpler when scripting, we wil  first set the Wrap Mode of the animation to Loop. Then
we will turn off Play Automatically to make sure our script is the only one playing animations.


我们的第一步是使角色平稳合成空闲和走路动画.为了做这个我们使用简单的脚本,我们首先设置卷模式
Wrap Mode 的动画循环 Loop.接着我们关闭自动播放使我们的脚本只有一个正在演奏的动画.  




Our first script for animating the character is quite simple; we only need some way to detect how fast our
character is moving, and then fade between walk and idle animation. For this simple test we use the
pre-setup input axes.


我们的第一个角色动画脚本十分简单;我们值需要一些方法知道我们的动画运行的速度,再减退走路和空闲
动画.为了这个简单的测试我们使用 pre-setup 输入轴.  


function Update ()


{


   if (Input.GetAxis("Vertical") > 0.2)


              animation.CrossFade ("walk");


      else


      animation.CrossFade ("idle");


}  


To get this script running:


使这个脚本运行:  



1.
2.
3.


Create a javascript using Assets->Create Other->Javascript.  
Copy & Paste the code into it  
Drag the script onto the character (It needs to be the same GameObject as the animation)


*1.         创建 javascript 使用 Assets->Create Other->Javascript.  
*2.         复制粘贴代码进入
*3.         拖拽脚本到角色(需要有一个同样动画的 GameObject)

When you hit the Play button, the character will start walking in place when you hold the up arrow key
and return to the idle pose when you release it.


当你按下运行按钮,且按下向上键角色将开始走路,释放他又回到空闲状态.  


Animation Layers

动画层次
Layers are an incredibly useful concept that allow you to group animations and prioritize weighting.  





In Unity's animation system, you can blend between as many animation clips as you want. You can
assign blend weights manually or simply use animation.CrossFade(), which will animate the weight
automatically.


层次是一个令人惊讶的使用观念,允许你分组动画和区分权重.在 Unity 动画系统,你能混合你想要的任意多
的动画片段.你能指定混合权重手动或简单的使用 animation.CrossFade(),这是动画的自动权重.  


Blend weights are always normalized before being applied

混合权重总是正常的在应用之前
Let's say you have a walk cycle and a run cycle, both have a weight of 1 (100%). When Unity generates
the final animation it will normalize the weights, which means walk will contribute 50% to the animation,
the run cycle will also contribute 50%.


我们假定你有一个走路循环和一个跑步循环,每个都有权重为 1(100%).当 Unity 产生最终动画他将恢复正常
权重,意思是走路贡献为 50%动画,跑步也贡献 50%.  


This is all very nice, but often you want to prioritize which animation receives most weight when there are
two animations playing. Surely you could just make sure that the weight sums up to 100% manually, but it
is a lot easier to use layers for this purpose.


这很好,但是有时你想要区分 2 个同时演奏的动画那一个有更多的权重.当然你要确保权重之和为 100%,但
是他使用层次可以很容易的达到这个目的.  


Layering Example

层次例子
As an example, you might have a shoot animation, an idle and a walk cycle. You will want to continously
fade between the walk and idle animation based on the player's speed. But when the player shoots you
want to only show the shoot animation. Thus the shoot animation essentially has a higher priority.


一个例子,你可能有一个射击动画,一个空闲和走路循环.你将想要基于玩家的速度逐渐褪去走路和空闲的动
画.但是当玩家开火你想要值展示开火动画.因此开火动画本质上有更高的权力.  


The easiest way to do this is to simply keep playing the walk and idle animations while shooting. Then we
need to make sure that the shoot animation is in a higher layer than idle and walk. This means the shoot
animation will receive blend weights first. The walk and idle animation will receive weights only if the
shoot animation doesn't use all of the 100% blend weights. So when CrossFading the shoot animation in,
the weight will start out at zero and over a short period become 100%. In the beginning the walk and idle
layer will still receive blend weights but when the shoot animation is completely faded in, they will receive
no weights at all. This is exactly what we need!   




简单的方法是保持走路和空闲动画在开火时.这样我们需要使开火的动画层次高于空闲和走路.意思是射击
动画将有最高权重.走路和空闲动画只有在射击动画不使用 100%混合权重使才能接收权重.所有当射击动
画在 CrossFading 里,权重将从 0 开始一会到达 100%.开始时走路和空闲层次将任然接收混合权重,知道射
击动画完成逐渐增强,他们将不接收权重.我们需要严格执行!


function Start ()


{


      // Set all animations to loop 设置所有动画为循环状态


   animation.wrapMode = WrapMode.Loop;


   // except shooting 除了射击


   animation["shoot"].wrapMode = WrapMode.Once;





      // Put idle and walk into lower layers (The default layer is always 0)放置空闲和走路到底层次(缺省为 0)


      // This will do two things 这里做 2 件事


      // Since shoot and idle/walk are in different layers they will not affect 射击和空闲/走路在不同层次没影响


      // each other's playback when calling CrossFade.当呼叫 CrossFade 重放每个


      // Since shoot is in a higher layer, the animation will replace idle/walk 射击在高层次,他取代空闲和走路


      //      animations when faded in.动画将逐渐增强


   animation["shoot"].layer = 1;





      // Stop animations that are already playing 停止动画确实在演奏时


      //(In case user forgot to disable play automatically)别忘了关闭自动播放


   animation.Stop();


}








function Update () {


      // Based on the key that is pressed,基于按下键


      // play the walk animation or the idle animation 演奏走路或空闲动画


   if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)


      animation.CrossFade("walk");


      else


            animation.CrossFade("idle");





      // Shoot 射击


   if (Input.GetButtonDown ("Fire1"))


      animation.CrossFade("shoot");


}  


By default the animation.Play() and animation.CrossFade() will stop or fade out animations that are in
the same layer. This is exactly what we want in most cases. In our shoot, idle, run example, playing idle
and run will not affect the shoot animation and vice versa (you can change this behaviour with an optional
parameter to animation.CrossFade if you like).


*缺省的 animation.Play()和 animation.CrossFade()        将停止或减弱动画在相同的层次.你想要避免这是严
格的.在我们的射击,空闲,跑步例子,演奏空闲和跑步将不影响射击动画反之也可以(你可以改变行为通过设
置一个参数给 animation.CrossFade 如果你想).


Additive Animations and Animation Mixing

添加动画和动画混合
Additive Animations and Animation mixing allow you to cut down on the number of animations you have
to create for your game, and are important for creating facial animations.


添加动画和动画混合允许你减少你创建的游戏的动画数量,且很重要的创建面部动画.  




Let's say you want to create a character that leans to the sides when running and turning.


我们假设你想要创建一个角色倾向一边当跑步和转弯时.  


You already made a walk and run cycle, now you could make individual walk-lean-left, walk-lean-right,
run-lean-left, run-lean-right animations.  


你已经有了走路和跑步循环,现在你要制作单独的左倾向走,右倾向走,左倾向跑,右倾向跑动画.


But that means you just quadrupled the amount of animation work! Creating a huge amount of
animations is not feasiable. Additive animations and Mixing to the rescue!   


但是这样意味着你有翻倍数量的动画工作!创建一个庞大的动画数量不是可取的.添加动画和混合能解决!


Additive Animation Example

添加动画例子
Additive animations allow you to overlay the effects of animation on top of any others that may be playing.
When making additive animations, Unity will calculate the difference between the first frame in the
animation clip and the current frame. Then it will apply this difference on top of all other playing
animations.  


添加动画允许你覆盖动画效果到另一个之上演奏.当使用添加动画,Unity 将计算动画片段的第一帧和现在帧
的不同.接着他将应用这种不同到所有其他动画上.


Now you only have to make a lean-left and lean-right animation. Unity will then layer this animation on top
of the walk, idle or run cycle.


现在你值有偏左和偏右动画.Unity 将放置这个动画层次高于走路,空闲或跑步循环.  


Here is the code to make that happen:  


这是制造这些的代码:


private var leanLeft : AnimationState;


private var leanRight : AnimationState;





function Start ()




{


   leanLeft = animation["leanLeft"];


   leanRight = animation["leanRight"];





      // Put the leaning animation in a seperate layer 设置偏向动画在指定层


      // So that other calls to CrossFade won't affect it.所有呼叫 CrossFade 不会影响他


   leanLeft.layer = 10;


   leanRight.layer = 10;





      // Set the lean animation to be additive 设置偏向动画为添加物


   leanLeft.blendMode = AnimationBlendMode.Additive;


   leanRight.blendMode = AnimationBlendMode.Additive;





      // Set the lean animation ClampForever 设置偏向动画 ClampForever


      // With ClampForever animation's will not automatically 设置 ClampForever 动画不自动播放


      // stop when reaching the end of the clip 停止当到片段最后


   leanLeft.wrapMode = WrapMode.ClampForever;


   leanRight.wrapMode = WrapMode.ClampForever;





      // Enable the animation and fade it in completely 激活动画和逐渐增强到完全




      // We don't use animation.Play here because we manually adjust the time 我们不使用 animation.Play
因为要手动调整时间


   // in the Update function.在 Update 函数里


      // Instead we just enable the animation and set it to full weight 替代我们激活的动画和设置他为完全权重


   leanRight.enabled = true;


      leanLeft.enabled = true;


   leanRight.weight = 1.0;


   leanLeft.weight = 1.0;





      // For testing just play run animation and loop it 尝试运行动画和循环他


   animation["walk"].wrapMode = WrapMode.Loop;


   animation.Play("walk");


}





// Every frame just set the normalized time 每帧设置为正常时间


// based on how much lean we want to apply 基于我们要应用的偏向有多少


function Update ()


{


   var lean = Input.GetAxis("Horizontal");


*      // normalizedTime is 0 at the first frame and 1 at the last frame in the clip        。normalizedTime 在片段
的第一帧为 0 和在最后帧为 1


   leanLeft.normalizedTime = -lean;




   leanRight.normalizedTime = lean;


}  


Tip: When using Additive animations it is critical that you are also playing some other non-additive
animation on every transform that is also used in the additive animation, otherwise the animations will
add on top of the last frame's result. This is most certainly not what you want.


建议:当使用添加动画,关键是你既要使用没有添加动画在每个改变,又要使用添加动画,不同的是动画将在最
后帧的结果上.这必然不是你想要的.  


Procedurally animating characters

程序控制动画角色
Sometimes you want to animate the bones of your character procedural y. For example you might want
the head of your character to look at a specific point in 3D space. This is best done with a script.
Fortunately, Unity makes this very easy. In Unity all bones are just Transforms which drive the skinned
mesh. Thus you can script bones of a character just like any other GameObject.


有时你想要拉动你的角色骨骼通过程序.例如你可能想要你的角色的头看向 3D 空间的某点.这用脚本很好解
决.幸好,Unity 做这个很简单.在 Unity 所有的骨骼是改变驱使表皮网格.因此你能脚本骨骼角色,像其他游戏
物体一样.


One important thing to know is that the animation system updates the Transforms after the Update()
function and before the LateUpdate() function is called. Thus if you want to do a LookAt() function you
should do that in LateUpdate() to make sure that you are really overriding the animation.


要知道的一个很重要的事情是动画系统是在 Update()函数之后更新改变,和在 LateUpdate()函数之前被呼
叫.因此如果你想要使用 LookAt()函数,你要让他在 LateUpdate()里面,以确保你的动画确实高于一切.  


Ragdolls are created in the same way. You simply have to attach Rigidbodies, Character Joints and
Capsule Colliders to the different bones. This will then physically animate your skinned character.


玩偶被创建有相同的方式.你直接附上刚体,角色联合和碰撞仓给不同的骨骼.这将为身体动画你的表皮角色.  


Finally

最后
You have learned how to make a basic character animation please see the projects for in-depth
examples of character animation and the animation script interface.  


你已经学习怎样创建一个基本的角色动画,请看projects里的深入角色动画例子和animation script interface.

本帖子中包含更多资源

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

×

评分

参与人数 2鲜花 +2 收起 理由
沐七七 + 1 赞一个!
曹叨叨 + 1 很给力!

查看全部评分

发表于 2012-11-23 09:36 | 显示全部楼层
顶一下!{:5_412:}
发表于 2012-11-23 14:27 | 显示全部楼层
看看 研究一下
发表于 2012-11-27 17:17 | 显示全部楼层
{:5_427:}仔细看看
发表于 2012-11-29 11:57 | 显示全部楼层
顶一下!!!
发表于 2012-12-4 23:44 | 显示全部楼层
要是能有代码学习下更好
发表于 2012-12-31 16:37 | 显示全部楼层
来一个,顶一下
发表于 2013-1-13 16:30 | 显示全部楼层
{:5_435:}这个帖子不错,大家快来顶起来!
发表于 2013-3-21 08:53 | 显示全部楼层

不错 不错 不错{:soso__3922851084632044791_6:}
发表于 2013-4-20 23:34 | 显示全部楼层

膜拜中。。。。{:soso__7524161091986203637_5:}
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-24 10:16 , Processed in 0.139140 second(s), 37 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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