首页 > 其他分享 >Playable API - 动画混合

Playable API - 动画混合

时间:2022-12-18 02:33:05浏览次数:73  
标签:动画 权重 graph Create 混合 API animMixerPlayable Playable

涉及到动画混合常见例子

a) 根据行走速度慢慢从走变成跑

b) 角色在跑步时,同时播放向左或向右倾斜动画

c) fps游戏中,角色边跑边攻击(需要动画分层+avatarMask)

d) 状态机动画切换时,比如下图中的从Jump切换到Locomotion

 

 

 

状态机动画切换混合(Transition)与BlendTree混合的区别

a) Transition中的混合只是在两个State转换时,在给定的时间内进行混合,避免动画切换过于突兀。
b) BlendTree混合,是时时刻刻进行不同程度的混合。比如你的角色有站立、走、跑三个动作,走路的速度是2m/s,跑的速度是5m/s,那你想让角色的速度是3m/s,这时候怎么办?这时候用混合树就能很简单地解决。

 

简单动画混合 - 根据行走速度慢慢从走变成跑

思路: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);
    }

}

#

 

 

运行效果

 

 

 

 参考

 Unity 动画系列六 BlendTree混合树 - 简书 (jianshu.com)

 Unity动画融合-Avatar Mask动画融合、Layers动画分层 - 简书 (jianshu.com)

 

标签:动画,权重,graph,Create,混合,API,animMixerPlayable,Playable
From: https://www.cnblogs.com/sailJs/p/16987702.html

相关文章