- Home /
random animations
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.
Answer by duck · Jan 16, 2010 at 02:41 PM
perhaps your idle animation could be controlled by a coroutine, something like this:
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);
}
}
}
how would i implement something like that? can barely muck through the demo scripts and patch together something that sort of works, i havent much of a clue when it comes to adding something along these lines.
the above code assumes that you would call the function Idle() when the character should start idling. You should also have a boolean variable called 'idling' which is set to true. The function above will keep playing idle animations until you set the 'idling' boolean variable to false.
Answer by eurosat7 · Mar 01, 2010 at 05:23 PM
This is what I did:
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