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.

more ▼

asked Jan 16, 2010 at 12:01 PM

kez717 gravatar image

15 4 4 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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

function Idle()
    while (idling) {
        if (Random.Range(0,5) > 0) {
            yield WaitForSeconds (idleClipNormal.length);
        } else {
            yield WaitForSeconds (idleClipOccasional.length);
more ▼

answered Jan 16, 2010 at 02:41 PM

duck gravatar image

duck ♦♦
47.8k 131 235 474

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.

Jan 17, 2010 at 11:48 AM kez717

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.

Jan 17, 2010 at 12:12 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

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); }


more ▼

answered Mar 01, 2010 at 05:23 PM

eurosat7 gravatar image

277 18 20 31

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jan 16, 2010 at 12:01 PM

Seen: 5450 times

Last Updated: Jan 16, 2010 at 12:01 PM