Type out text in sync with audioclip
Hey there guys.
Im trying to type out a text char-by-char in sync with an audioclip. Since I have done similar things before I quickly had an idea for my script. here it is:
foreach (char c in text.ToCharArray()) {
textDisplay.text += c;
yield return new WaitForSeconds (clip.length / text.Length);
}
This is of course inside a coroutine.
By logic this should work exactly but its a little too slow. That is because of the time it takes to type out the text each frame. Its just like 0.01 seconds but its enough to throw off my calculations.
I tried accounting for this with a custom deltaTime. Now my script looked like this:
float time = Time.time;
float delta = 0;
foreach (char c in text.ToCharArray()) {
textDisplay.text += c;
delta = Time.time - time;
time = Time.time;
yield return new WaitForSeconds ((clip.length / text.Length) - delta);
}
But now the text is spelled out slightly too fast, Its just a case of like 1 second but I'd rather fix it than leave it in the game.
Any suggestions? :)
Your answer
Follow this Question
Related Questions
Syncing Video and Game Object Audio 0 Answers
is it possible to reset Time.time 1 Answer
I am completely misunderstanding what the point of Time.deltaTime is. 0 Answers
How to export in a text file float numbers generated in a list 1 Answer
How to Sync GameObject Creation to The Beat of a Song? 2 Answers