如何使用脚本在动画制作器中播放随机动画? [英] How can I play random animations in animator using script?

查看:207
本文介绍了如何使用脚本在动画制作器中播放随机动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有12个状态,它们都通过循环中的过渡连接. 因此,播放的流程始终是相同的.但我想随机播放.

I have 12 states they are all connected by transitions in loop. So the flow of the playing is always the same. But I want to play them randomly.

但是,如果它们都通过过渡连接在一起,我想这是不可能的,因为过渡已经确定了路径.

But if they are all connected by transitions I guess it's impossible since the transition make the path.

并且使用Animation组件不能与mixamo动画一起使用.由于在动画"组件中,动画必须是传统类型,但是在动画师中,它们必须是类人动物类型,并且要正确播放动画,我需要将它们设置为类人动物类型,这就是为什么我使用Animator而不是"Animation"的原因.播放它们效果很好,但我想按随机顺序播放它们.

And using Animation component not working with mixamo animations. Since in Animation component the animations must to be legacy type but in the animator they have to be humanoid type and to play the animations correctly I need to set them to be humanoid type that's why I'm using the Animator and not Animation. And playing them is working fine but I want to play them in a random order.

推荐答案

private AnimationClip[] clips;
private Animator animator;

private void Awake()
{
    // Get the animator component
    animator = GetComponent<Animator>();

    // Get all available clips
    clips = animator.runtimeAnimatorController.animationClips;
}

现在您拥有了所有动画.

Now you have all animations.

现在有多种方法可以将这些方法随机化...我向您展示了最简单的方法,即随机选择一个索引并播放该动画.

There are various ways of how those shall be randomized now ... I show you the simplest one which is just randomly pick an index and play that animation.

首先,您将使用协程,以便从头开始

First you will use a Coroutine so to start it from the beginning

private void Start()
{
    StartCoroutine (PlayRandomly);
}

在协程中选择一个随机索引,然后在动画师中播放状态

In the Coroutine pick a random index and play the state in the animator

private IEnumerator PlayRandomly ()
{
    while(true)
    {
        var randInd = Randome.Range(0, Clips.length);

        var randClip = clips[randInd];

        animator.Play(randClip.name);

        // Wait until animation finished than pick the next one
        yield return new WaitForSeconds(randClip.length);
    }
}


请注意,这是最简单的方法,不能保证不要在同一个动画上播放两次相同的动画"或在重复一个动画之前先播放所有动画"之类的东西


Note as said this is the simplest way and does not assure things like "Don't play the same animation twice right afyer another" or "First play all animations before repeating one"

要实现这些目标,您可以改组剪辑列表,遍历它们,然后在最后一个剪辑之后再次进行洗牌等,例如喜欢

To achieve those you would instead shuffle the list of clips, run through them and after the last clip shuffle again etc e.g. like

private IEnumerator PlayRandomly ()
{
    var clipList = clips.ToList();

    while(true)
    {
        clipList.Shuffle();

        foreach(var randClip in clipList)
        {
            animator.Play(randClip.name);
            yield return new WaitForSeconds(randClip.length);
        }
    }
}

public static class IListExtensions 
{
    /// <summary>
    /// Shuffles the element order of the specified list.
    /// </summary>
    public static void Shuffle<T>(this IList<T> ts) {
        var count = ts.Count;
        var last = count - 1;
        for (var i = 0; i < last; ++i) {
            var r = UnityEngine.Random.Range(i, count);
            var tmp = ts[i];
            ts[i] = ts[r];
            ts[r] = tmp;
        }
    }
}

这篇关于如何使用脚本在动画制作器中播放随机动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆