如何使用脚本在动画器中播放随机动画?
原标题:How can I play random animations in animator using script?
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.
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
();
// 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 after 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
{
///
/// Shuffles the element order of the specified list.
///
public static void Shuffle(this IList 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;
}
}
}
问题回答
暂无回答
相关问题
What is the use of `default` keyword in C#?
What is the use of default keyword in C#?
Is it introduced in C# 3.0 ?
Anyone feel like passing it forward?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs.
...
NSArray s, Primitive types and Boxing Oh My!
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits.
Now here s the question: I feel really inclined to create some type of ...
C# Marshal / Pinvoke CBitmap?
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class.
My import looks like this:
[DllImport(@"test.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ...
ADO.NET Entity Framework Association of Entities by Value Range
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber.
I want to create a many to many association ...
How to Use Ghostscript DLL to convert PDF to PDF/A
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
What is the most efficient keyvalue pair for ordering?
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Linqy no matchy
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control.
...