Hi, i have been trying to script a image sequence of a book flipping through the pages. I have the individual images and placed them on a array of textures as suggested in a thread here
http://forum.unity3d.com/viewtopic.php?t=24957&highlight=playing+image+sequence.
My problem is when i use
var idx: int = Mathf.RoundToInt(Mathf.Repeat(Time.time / adjFactor, numImages));
Due to time being involved, there are times wherein nothing happens and sometimes it seems to skip through an image. Is there any way around this?
The issue is in the rounding to an integer, but why not just do something like this:
public var myBook: GameObject;
public var bookPages: Texture[];
private var pageIdx: int = 0;
private var startFlipping: boolean = false;
function FlipPages() {
this.pageIdx = 0;
while (true) {
this.pageIdx++;
if (this.pageIdx == this.bookPages.length) {
this.pageIdx = 0;
}
this.myBook.renderer.material.mainTexture = this.bookPages[this.pageIdx];
yield WaitForSeconds(5); // Desired time between page flips.
}
return;
}
function Update() {
if (!this.startFlipping) {
StartCoroutine("FlipPages");
this.startFlipping = true;
}
return;
}
I don’t know what I was smoking when I wrote that original post, but there are several problems with it aside from the problem you discovered. The other problems are:
-
Division is expensive and should be avoided. If you need to divide, multiply by a decimal value instead which is faster, unless this is going to create a precision issue.
-
Two function calls are unnecessarily introduced which makes absolutely no sense.
-
Rounding is expensive an subject to precision issues.
-
Using a coroutine will an appropriate WaitForSeconds time interval will guarantee that the pages flip at a consistent rate.
-
I was trying to be cute recommending something that I obviously didn’t try out. Sorry for that. :roll:
Its ok 
I’ll try the one that you posted above 
It worked :lol: ty so much 