# random animations

**URL:** https://discussions.unity.com/t/random-animations/1065
**Category:** Questions & Answers
**Created:** [January 16, 2010, 12:01pm UTC](https://discussions.unity.com/t/random-animations/1065 "2010-01-16T12:01:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [January 16, 2010, 12:01pm UTC](https://discussions.unity.com/t/random-animations/1065/1 "2010-01-16T12:01:01Z")

</div>

how can i have animations randomly play? for example i have 2 idle animations, one main and one that i would like to play once through at random intervals while the character is not moving. how would i go about getting this to happen? so far i can only get the main idle animation to play.

---

<div class="post-metadata">

### Author: ![duck](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/duck/32/477465_2.png) [@duck](https://discussions.unity.com/u/duck)
#### Post date: [January 16, 2010, 2:41pm UTC](https://discussions.unity.com/t/random-animations/1065/2 "2010-01-16T14:41:48Z")

</div>

perhaps your idle animation could be controlled by a coroutine, something like this:

```auto
function Idle()
{
    while (idling) {
        if (Random.Range(0,5) > 0) {
            animation.Play(idleClipNormal);
            yield WaitForSeconds (idleClipNormal.length);
        } else {
            animation.Play(idleClipOccasional);
            yield WaitForSeconds (idleClipOccasional.length);
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [March 1, 2010, 5:23pm UTC](https://discussions.unity.com/t/random-animations/1065/3 "2010-03-01T17:23:02Z")

</div>

This is what I did:

```auto
var SceletorObject : GameObject = null;
var curAnim : String = null;
var doAnimation : boolean = false;
private var AnimObj : Animation = null;

function Start(){
    AnimObj=SceletorObject.GetComponent(Animation);
}

function OnTriggerEnter() {
    AnimObj.PlayQueued("raise");
    doAnimation=true;
}
function OnTriggerExit() {
    AnimObj.PlayQueued("hit");
    AnimObj.PlayQueued("die");
    doAnimation=false;
}
function OnTriggerStay(){
    if (!AnimObj.isPlaying && doAnimation)
        doNextAnim();
}
function doNextAnim(){
    var anims=new Array("idle1","idle1","idle1","idle1","idle2","strike"); 
    var length=anims.length-1;
    var index=Mathf.Round(length*UnityEngine.Random.value);
    curAnim=anims[index];
    AnimObj.PlayQueued(curAnim);
}

```

HTH
