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.
c# unity3d
add a comment |
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.
c# unity3d
add a comment |
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.
c# unity3d
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.
c# unity3d
c# unity3d
edited Jan 2 at 1:08
Dubi Duboni
asked Jan 1 at 23:44
Dubi DuboniDubi Duboni
394110
394110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
}
}
}
Working. But now in my other project the states with the animations in a sub state machine. How can get them from the sub state machine ? The sub state machine name is Magic StateMachine.
– Dubi Duboni
Jan 2 at 8:59
1
As far as I know you can get substates by connecting them with a... but I'm pretty sure you shouldn't have a space in the StateName then .. would look likePlay("MagicStateMachine.somesubstate");
– derHugo
Jan 2 at 9:09
1
Unfortunately there seems to be no way for theruntimeAnimatorControllerto know whether a state is only one clip or a sub-statemachine.. not sure butanimationClipsmight also already contain the sub-statemachine's clips as well so you could filter the list by the states name in orser to get only those clips.
– derHugo
Jan 2 at 9:16
1
See stackoverflow.com/questions/22956518/…
– derHugo
Jan 2 at 9:27
Sorry for messed it all up. Your answer here is including the substates already.
– Dubi Duboni
Jan 2 at 9:41
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999817%2fhow-can-i-play-random-animations-in-animator-using-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
}
}
Working. But now in my other project the states with the animations in a sub state machine. How can get them from the sub state machine ? The sub state machine name is Magic StateMachine.
– Dubi Duboni
Jan 2 at 8:59
1
As far as I know you can get substates by connecting them with a... but I'm pretty sure you shouldn't have a space in the StateName then .. would look likePlay("MagicStateMachine.somesubstate");
– derHugo
Jan 2 at 9:09
1
Unfortunately there seems to be no way for theruntimeAnimatorControllerto know whether a state is only one clip or a sub-statemachine.. not sure butanimationClipsmight also already contain the sub-statemachine's clips as well so you could filter the list by the states name in orser to get only those clips.
– derHugo
Jan 2 at 9:16
1
See stackoverflow.com/questions/22956518/…
– derHugo
Jan 2 at 9:27
Sorry for messed it all up. Your answer here is including the substates already.
– Dubi Duboni
Jan 2 at 9:41
|
show 1 more comment
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;
}
}
}
Working. But now in my other project the states with the animations in a sub state machine. How can get them from the sub state machine ? The sub state machine name is Magic StateMachine.
– Dubi Duboni
Jan 2 at 8:59
1
As far as I know you can get substates by connecting them with a... but I'm pretty sure you shouldn't have a space in the StateName then .. would look likePlay("MagicStateMachine.somesubstate");
– derHugo
Jan 2 at 9:09
1
Unfortunately there seems to be no way for theruntimeAnimatorControllerto know whether a state is only one clip or a sub-statemachine.. not sure butanimationClipsmight also already contain the sub-statemachine's clips as well so you could filter the list by the states name in orser to get only those clips.
– derHugo
Jan 2 at 9:16
1
See stackoverflow.com/questions/22956518/…
– derHugo
Jan 2 at 9:27
Sorry for messed it all up. Your answer here is including the substates already.
– Dubi Duboni
Jan 2 at 9:41
|
show 1 more comment
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;
}
}
}
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;
}
}
}
edited Jan 2 at 5:01
answered Jan 2 at 4:52
derHugoderHugo
8,25631535
8,25631535
Working. But now in my other project the states with the animations in a sub state machine. How can get them from the sub state machine ? The sub state machine name is Magic StateMachine.
– Dubi Duboni
Jan 2 at 8:59
1
As far as I know you can get substates by connecting them with a... but I'm pretty sure you shouldn't have a space in the StateName then .. would look likePlay("MagicStateMachine.somesubstate");
– derHugo
Jan 2 at 9:09
1
Unfortunately there seems to be no way for theruntimeAnimatorControllerto know whether a state is only one clip or a sub-statemachine.. not sure butanimationClipsmight also already contain the sub-statemachine's clips as well so you could filter the list by the states name in orser to get only those clips.
– derHugo
Jan 2 at 9:16
1
See stackoverflow.com/questions/22956518/…
– derHugo
Jan 2 at 9:27
Sorry for messed it all up. Your answer here is including the substates already.
– Dubi Duboni
Jan 2 at 9:41
|
show 1 more comment
Working. But now in my other project the states with the animations in a sub state machine. How can get them from the sub state machine ? The sub state machine name is Magic StateMachine.
– Dubi Duboni
Jan 2 at 8:59
1
As far as I know you can get substates by connecting them with a... but I'm pretty sure you shouldn't have a space in the StateName then .. would look likePlay("MagicStateMachine.somesubstate");
– derHugo
Jan 2 at 9:09
1
Unfortunately there seems to be no way for theruntimeAnimatorControllerto know whether a state is only one clip or a sub-statemachine.. not sure butanimationClipsmight also already contain the sub-statemachine's clips as well so you could filter the list by the states name in orser to get only those clips.
– derHugo
Jan 2 at 9:16
1
See stackoverflow.com/questions/22956518/…
– derHugo
Jan 2 at 9:27
Sorry for messed it all up. Your answer here is including the substates already.
– Dubi Duboni
Jan 2 at 9:41
Working. But now in my other project the states with the animations in a sub state machine. How can get them from the sub state machine ? The sub state machine name is Magic StateMachine.
– Dubi Duboni
Jan 2 at 8:59
Working. But now in my other project the states with the animations in a sub state machine. How can get them from the sub state machine ? The sub state machine name is Magic StateMachine.
– Dubi Duboni
Jan 2 at 8:59
1
1
As far as I know you can get substates by connecting them with a
. .. but I'm pretty sure you shouldn't have a space in the StateName then .. would look like Play("MagicStateMachine.somesubstate");– derHugo
Jan 2 at 9:09
As far as I know you can get substates by connecting them with a
. .. but I'm pretty sure you shouldn't have a space in the StateName then .. would look like Play("MagicStateMachine.somesubstate");– derHugo
Jan 2 at 9:09
1
1
Unfortunately there seems to be no way for the
runtimeAnimatorController to know whether a state is only one clip or a sub-statemachine.. not sure but animationClips might also already contain the sub-statemachine's clips as well so you could filter the list by the states name in orser to get only those clips.– derHugo
Jan 2 at 9:16
Unfortunately there seems to be no way for the
runtimeAnimatorController to know whether a state is only one clip or a sub-statemachine.. not sure but animationClips might also already contain the sub-statemachine's clips as well so you could filter the list by the states name in orser to get only those clips.– derHugo
Jan 2 at 9:16
1
1
See stackoverflow.com/questions/22956518/…
– derHugo
Jan 2 at 9:27
See stackoverflow.com/questions/22956518/…
– derHugo
Jan 2 at 9:27
Sorry for messed it all up. Your answer here is including the substates already.
– Dubi Duboni
Jan 2 at 9:41
Sorry for messed it all up. Your answer here is including the substates already.
– Dubi Duboni
Jan 2 at 9:41
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999817%2fhow-can-i-play-random-animations-in-animator-using-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
