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)

 

posted @ 2022-12-18 02:27  yanghui01  阅读(204)  评论(0编辑  收藏  举报