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.
Blend weights are always normalized before being applied
混合权重总是正常的在应用之前
Let's say you have a walk cycle and a***n 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***n cycle will also contribute 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.
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!
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,***n example, playing idle and***n will not affect the shoot animation and vice versa (you can change this behaviour with an optional parameter to animation.CrossFade if you like).
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***nning and turning.
我们假设你想要创建一个角色倾向一边当跑步和转弯时.
You already made a walk and***n cycle, now you could make individual walk-lean-left, walk-lean-right,***n-lean-left,***n-lean-right animations.
你已经有了走路和跑步循环,现在你要制作单独的左倾向走,右倾向走,左倾向跑,右倾向跑动画.
But that means you just qua***pled 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.
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***n 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 = ***e;
leanLeft.enabled = ***e;
leanRight.weight = 1.0;
leanLeft.weight = 1.0;
// For testing just play***n 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.