Playable API - 简单动画混合
简单动画混合 - 根据行走速度慢慢从走变成跑
思路:AnimationMixerPlayable可以同时播放多个AnimationClip,同时可以设置动画的占用权重,走的时候walk动画权重为1,run动画权重为0;
从走变成跑的过程中,walk动画权重1-runWeight,run动画权重runWeight;
跑的时候walk动画权重为0,run动画权重为1;
using UnityEngine; using UnityEngine.Animations; using UnityEngine.Playables; public class SimpleAnimClipMix : MonoBehaviour { public AnimationClip walkAnimClip; public AnimationClip runAnimClip; [Range(0, 1)] public float runSpeed; private PlayableGraph _graph; private AnimationMixerPlayable _animMixerPlayable; void Start() { _graph = PlayableGraph.Create("ChanPlayableGraph"); var animationOutputPlayable = AnimationPlayableOutput.Create(_graph, "AnimationOutput", GetComponent<Animator>()); //往graph添加output _animMixerPlayable = AnimationMixerPlayable.Create(_graph, 2); //往graph添加playable(mixer) animationOutputPlayable.SetSourcePlayable(_animMixerPlayable); var walkAnimClipPlayable = AnimationClipPlayable.Create(_graph, walkAnimClip); //往graph添加playable(animClip) var runAnimClipPlayable = AnimationClipPlayable.Create(_graph, runAnimClip); //往graph添加playable(animClip) _graph.Connect(walkAnimClipPlayable, 0, _animMixerPlayable, 0); _graph.Connect(runAnimClipPlayable, 0, _animMixerPlayable, 1); _graph.Play(); } void Update() { _animMixerPlayable.SetInputWeight(0, 1 - runSpeed); _animMixerPlayable.SetInputWeight(1, runSpeed); } }
#
运行效果
PlayableGraph的可视化图形:跑的权重占0.87的时候
状态机动画Transition其实也是动画混合
比如下图中的从Jump切换到Locomotion
与BlendTree混合的区别?
a) Transition中的混合只是在两个State转换时,在给定的时间内进行混合,避免动画切换过于突兀。
b) BlendTree混合,是时时刻刻进行不同程度的混合。比如你的角色有站立、走、跑三个动作,走路的速度是2m/s,跑的速度是5m/s,那你想让角色的速度是3m/s,这时候怎么办?这时候用混合树就能很简单地解决。
参考
【Unity】简单使用Playable API控制动画 - 知乎 (zhihu.com)
分类:
Unity
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)