- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
vlamor · Jul 19, 2012 at 02:14 PM ·
animationrandominvokerepeating
Random animation and InvokeRepeating
I have wrote a code to change idle animation when there are no actions don't happen during 5 seconds:
private string [] idleArray = new string[]{"idle"", "idle2', "idle3", "idle4", "idle5", "idle6"};
void Idle()
{
animation.CrossFade("idle");
if(!IsInvoking("ChangeIdleAnimation"))
InvokeRepeating("ChangeIdleAnimation", 5.0f, 5.0f);
}
void ChangeIdleAnimation()
{
string idleAnimation = idleArray[Random.Range(1,5)];
animation.CrossFade(idleAnimation);
}
void StopRandomIdleAnimation()
{
if(IsInvoking("ChangeIdleAnimation"))
{
CancelInvoke("ChangeIdleAnimation");
}
}
void Running()
{
StopRandomIdleAnimation();
animation.CrossFade("run");
}
But the idle animation doesn't change, in spite of that method ChangeIdleAnimation() invokes every 5 seconds. Where is a mistake? enter code here
Comment
Answer by vlamor · Jul 19, 2012 at 08:21 PM
The problem has been solved. The code should be following:
void Idle()
{
if(!IsInvoking("ChangeIdleAnimation"))
{
InvokeRepeating("ChangeIdleAnimation", 5.0f, 5.0f);
animation.CrossFade("idle");
}
}
And the CancelInvoke("ChangeIdleAnimation") method should be invoked after the changing of Idle animation. Hope it helps to somebody.