看起来实际使用Animation Layers 对你来说会更有趣!我无法在此处重新创建完整的手册 - 查看文档和 this tutorial。
但是,Animator.Play
有一个可选参数
normalizedTime:零到一之间的时间偏移量。
这样您就可以使用当前动画所在的偏移量开始新动画
您可以使用Animator.GetCurrentAnimatorStateInfo 获取当前状态以及当前剪辑所在的normalizedTime
和Animator.GetCurrentAnimatorClipInfo 获取剪辑的信息(例如长度)。
要么引用你的目标剪辑(我更喜欢那个),要么你可以使用Animator.runtimeAnimatorController 来获取所有AnimationClips,而不是使用LinQ FirstOrDefault 来查找具有目标名称的剪辑:
(在我的智能手机上输入,所以没有保证)
using System.Linq;
// ...
if (!shooting)
{
// assuming layer 0
var state = animator.GetCurrentAnimatorStateInfo(0);
var clipInfo = animator.GetCurrentAnimatorClipInfo(0);
// so the time you want to start is currentNormalizedTime * current.length / newClip.length
// if your clips will always have the exact same length of course you can
// simply use the currentNormalizedTime
var currentNormalizedTime = state.normalizedTime;
var currentRealtime = currentNormalizedTime * clipInfo[0].clip.length;
var newClip = animator.runtimeAnimatorController.animationClips.FirstOrDefault(c => string.Equals(c.name, "PlayerRunning"));
var newNormalizedTime = currentRealtime / newClip.length;
// for some reason it seems you can not use the optional parameters as you usually would
// like ("PlayerRunningShoot", normaizedTime = newNormalizedTime)
animator.Play("PlayerRunning", -1, newNormalizedTime);
}
if (shooting)
{
var state = animator.GetCurrentAnimatorStateInfo(0);
var clipInfo = animator.GetCurrentAnimatorClipInfo(0);
var currentNormalizedTime = state.normalizedTime;
var currentRealtime = currentNormalizedTime * clipInfo[0].clip.length;
var newClip = animator.runtimeAnimatorController.animationClips.FirstOrDefault(c => string.Equals(c.name, "PlayerRunning"));
var newNormalizedTime = currentRealtime / newClip.length;
animator.Play("PlayerRunningShoot", -1, newNormalizedTime);
}
您还可以使用在运行时创建平滑过渡
Animator.CrossFade 或 Animator.CrossFadeInFixedTime
if (!shooting)
{
// ...
// here the transition takes 0.25 % of the clip
animator.CrossFade("PlayerRunning", 0.25f, -1, 0, newNormalizedTime);
}
if (shooting)
{
// ...
// here the transition takes 0.25 seconds
animator.CrossFadeInFixedTime("PlayerRunningShoot", 0.25f, -1, 0, newNormalizedTime);
}