- Home /
How can I match up some code and an animation keyframe
Hi all,
Basically I have a script which parses some text and reveals a character from the string every x seconds to give the effect of text being printed out. I also have a portrait sprite which is placed next to the text as it is printed.
The portrait sprite is controlled using the animation editor in unity to turn the sprite on/off at a timed keyframe.
sprite render = true at 3 seconds as the text script is enabled
sprite render = false at 20 seconds as the text script finishes
Now the problem seems to be that the text script can take a varying amount of time to complete which makes it impossible to turn the sprite off at the correct time.
I know I could access the portrait sprite object from within the code and turn it off manually, but there is potentially multiple sprites all turning off at different times and I would rather leave this side to the designer of the scene
Anyone have any ideas how to fix this?
Added a rough idea of how the code works below.
void FixedUpdate ()
{
if (Active)
{
StartCoroutine( AnimateText(text) );
Active = false;
}
}
IEnumerator AnimateText(string strComplete)
{
while( i < strComplete.Length )
{
bool found = false;
bool done = false;
char newchar = strComplete[i];
while( !found )
{
newchar = strComplete[i];
switch (newchar)
{
default: found = true; break;
}
}
if (!done)
{
font.text += newchar;
i++;
yield return new WaitForSeconds(TextSpeed);
}
}