Random animations playing!

Hi guys!
my character has got tow “Idle” animationclip. one of them is the main an other one should play randomly! My own script looks correct(i hope)! but doesnt work fine!

IEnumerator RandomIdle()
{
while (Standing)
    {   
        if(Random.Range((1,10) > 1)
            {
                animation.CrossFade("Idle");
                yield return new WaitForSeconds(animation["Idle"].clip.length);
            }
        else
            {
                animation.CrossFade("Idle2");
                yield return new WaitForSeconds(animation["Idle2"].clip.length);
            }
    }
}

void Idle()
{
    StartCoroutine((RandomIdle());
}

the main “idle” will play but there is no delay(yield) to CrossFade into second “idle” animation ,and my character shake forever!

Its not clear from your code - does that coroutine only get started once, or is it possible it’s being run over and over again giving you a situation where your character is constantly crossfading between animations?

The other problem I see is that it idles for x time, even if idle is no longer true.

First thank for cmm!
Here is my script (of course simplized!)

	public bool Standing = false;
	void Start () {
	}
	void Update () 
	{
		DetectState();
		PlayState();
	}

	void DetectState()
	{		
		if (State != _state.Landing 
			CharacterController.velocity.sqrMagnitude < 0.2f)
		{
			State = _state.Idle;
			Standing = true;
		}
		else
			Standing = false;
	}
	
	void PlayState()
	{
		switch (State)
		{
			case _state.Idle:
				Idle();
			break;
			//and and and ...
		}
	}

	void Idle()
	{
		StartCoroutine((RandomIdle());
	}
	
	IEnumerator RandomIdle()
	{
	while (Standing)
		{   
			if(Random.Range((1,10) > 1)
				{
					animation.CrossFade("Idle");
					yield return new WaitForSeconds(animation["Idle"].clip.length);
				}
			else
				{
					animation.CrossFade("Idle2");
					yield return new WaitForSeconds(animation["Idle2"].clip.length);
				}
		}
	}

i suppose its should be unlimited!
couse as u know “Idle” state and animation is Loop State!

Finaly i could solve it!
The reason was that “yeild” actually shouldn’t use in Update or FixUpdate, the correct script is :
(I’ll put it here .for someone who has got the same problme as me! (; )

private float Rnd = 4f;

public IEnumerator Start() 
{
    yield return 0;
    while(Standing)
    {
        Rnd = Random.Range(0,10);
        yield return new WaitForSeconds(animation["Idle"].clip.length);
    }
}

void Idle()
{
    if(Rnd < 2)
        animation.CrossFade("Idle2");
    else
        animation.CrossFade("Idle");
}

You seem to not be using the idle function now. :frowning:

This still presents the same problem:

Step through the code in your mind… You start, and while your standing, start, and wait for the idle animation to finish, then go to the next instruction.

What this means is that if you want to walk. Your idle animation will still be running, until the loop is over.

I am sure someone here has done this before, or there is a different way to do it. You would have to write a very large animation handler to CrossFade animations for events. It may just be part of the work for doing animations.

You would have to write something that would run an idle loop, CrossFadeQueued the next animation. Play it at the end if it is not an interrupting animation or play it now if it is. Then return to the looping idle animation if nothing else is happening.

Keep in mind, that your character could be idle, or walking, running, attacking, shooting, jumping, falling or a whole host of other things.

You were right!
It still has problem.
But there is some NEW issue, “Rnd” in first running creating well !(cuose its Start/Awake void)
it means in game starting both idleS work well until the second time ,when State change or retrun to Idle (cuose its Start/Awake void)
“Rnd” wont change anymore, and the animation is playing is the last one!
I’v been searching a lot about it so far!
and only this one is close to my case :
http://answers.unity3d.com/questions/10243/random-animations.html
Im so confused and also intrested about Yield and Coroutine using in unity! :frowning: