Hey all. I have an animated 2d character who has two different animated textures right now (quicktime movies). To animate them I'm using this lil bit of code that I nabbed off of the documentation: Code (csharp): // Animate the texture frames var texture : Texture2D; var framesPerSecond = 12.0; function Update () { var frameIndex : int = Time.time * framesPerSecond; // make the animation repeat by taking modulo frameIndex = frameIndex % texture.frameCount; // change texture frame texture.frame = frameIndex; } function Reset () { if (renderer) { texture = renderer.sharedMaterial.mainTexture; } } But the problem is that when the current animation isn't the active material, it still plays invisibly, so next time I switch to that animation it starts not at its actual first frame (like it should), but from wherever it was in its hidden animation. Um, did you get all of that? So what should I do?
Declare another variable and when the material is setted, start adding time to that variable starting from 0, its like having your own time Code (csharp): private var ownTime = 0.0; private var animating = false; function Update () { if ( animating ) { ownTime += Time.deltaTime; } else { ownTime = 0.0; } var frameIndex = ownTime / framesPerSecond; } Here you control the animating variable so when the material is set, animating should be turned to true, and use ownTime to calculate the current frame. For performance reasons, you should put all your frame calculus code inside the if ( animating ) { } .ORG
The animations aren't really running; it's just that Time.time keeps going no matter what, and that's where the index value is coming from. (Annoying, isn't it...I propose that Unity 2.0 make it so that you can stop time. ) So, use something else other than Time.time. Like instead of using Update, you could have the animation use a function that you call with InvokeRepeating or something. --Eric