Well, I have, what I thought was, a simple animation script that randomizes which animation my character will perform during the Main Menu. However what’s happening is that over time, the CPU gets bogged down and animations begin to become jerky when they played (like watching it at 15 fps instead of 60)
The animations will play without jerkiness if they play first, but if they play some-odd seconds later, they become jerky (and, well, my fps drops like mad)
Any suggestions on how to clean it up and to get it to… I’m guessing stopping it from adding onto the stack of calls? I know that the animation Queue is getting huge due to the constant calls to add an animation to it, but I need it to play a random animation as soon as one’s finished.
I would probably do something like this? (not tested)
EDIT: I just noticed you had some logic for making sure the new animation is not the same as the previous animation, I obviously didn’t do that but you could probably figure out how to shoehorn it in.
private var animIsPlaying : boolean;
function Update() {
if(!animIsPlaying){
PlayRandomAnim();
}
}
function PlayRandomAnim(){
animIsPlaying = true;
var animRand = Random.Range(0,5);
switch(animRand){
case 0:
animation.Play("anim0");
yield WaitForSecond(animation["anim0"].length);
break;
case 1:
animation.Play("anim1");
yield WaitForSecond(animation["anim1"].length);
break;
case 2:
animation.Play("anim2");
yield WaitForSecond(animation["anim2"].length);
break;
case 3:
animation.Play("anim3");
yield WaitForSecond(animation["anim3"].length);
break;
case 4:
animation.Play("anim4");
yield WaitForSecond(animation["anim4"].length);
break;
}
animIsPlaying = false;
}
Maybe something like this would work? I never tried using a while loop like this, but it seems good in theory in my head (while animRand equals lastAnim, keep getting new random numbers until it isn’t anymore?):
private var animIsPlaying : boolean;
private var animRand : int;
private var lastAnim : int = -1;
function Update() {
if(!animIsPlaying){
PlayRandomAnim();
}
}
function PlayRandomAnim(){
animIsPlaying = true;
animRand = Random.Range(0,5);
while(animRand == lastAnim)
animRand = Random.Range(0,5);
switch(animRand){
case 0:
animation.Play("anim0");
yield WaitForSecond(animation["anim0"].length);
break;
case 1:
animation.Play("anim1");
yield WaitForSecond(animation["anim1"].length);
break;
case 2:
animation.Play("anim2");
yield WaitForSecond(animation["anim2"].length);
break;
case 3:
animation.Play("anim3");
yield WaitForSecond(animation["anim3"].length);
break;
case 4:
animation.Play("anim4");
yield WaitForSecond(animation["anim4"].length);
break;
}
lastAnim = animRand;
animIsPlaying = false;
}
Note that I initialized lastAnim at -1 so the first time the code is run lastAnim and animRand cannot be the same value.