- Home /
making a 2D character Speak for different lenghts of time
here's the problem, I'll try to be as clear as possible: I have a 2D character and next to him there is a text box managed by a dialogue manager called VIDE.
My character has 2 animations one moving his lips to emulate "reading out loud" the text next to him and the default one which is Idle (Silence).
By pressing a button I can go to the next dialogue and I can get the text ID currently displayed on screen in this case as an int number like this.
void Update() {
textID = (int)VD.nodeData.extraVars["cuadro"];
print(textID);
}
I want to use this "textID" variable so my character moves his lips more time or less time depending on the amount of text displayed in each text dialogue and I figured out I needed to use a coroutine to make this possible, I wrote something like this to make it happen:
IEnumerator Speaking()
{
switch (textID)
{
case 0:
yield return null;
break;
case 1:
funcionesScript.LipsMoving();
yield return new WaitForSeconds(3);
funcionesScript.Silence();
yield break;
case 2:
funcionesScript.LipsMoving();
yield return new WaitForSeconds(3);
funcionesScript.Silence();
yield break;
}
yield return null;
}
The problem is that I can't make it work I can't put the coroutine in Update() and I don't quite know how to use this so whenever the textID changes the character starts "speaking" and then stops after the time specified for the respective textID and goes to Idle state and stays in Idle untill the user hits the button and the next dialogue appears so it can start speaking again, I'll appreciate any ideas on how to achieve this :(
Answer by FallingRocketGames · Sep 30, 2017 at 12:54 AM
Answer by · Sep 29, 2017 at 07:45 PM
Like you said, you can make a coroutine.
public showText(float textTime) //Your function that is called to show the text
{
StartCoroutine(MakeCharacterSpeak(textTime));
}
IEnumerator MakeCharacterSpeak(float waitTime)
{
//StartSpeaking. Change the animation to speaking
yield return new WaitForSeconds(waitTime);
//StopSpeaking. Change the animation to Silence
}
See if you can make this work.
Edit: You can also make it work with Invoke
public showText(float textTime) //Your function that is called to show the text
{
Invoke("StopSpeaking", textTime); //This will call the function StopSpeaking after textTime seconds
}
void StopSpeaking()
{
//StopSpeaking. Change the animation to Silence
}
This is the docs: https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
So where do I have to call the ShowText function? I can't put it in update right? Ho will the value of textID be used to update my textTime every time the dialogue changes?
No, you can't put it in Update. You must have a function somewhere that changes what the text should be, right? Even if it is the function that the button calls when it is pressed.
I get you, I'll look to a different approach ins$$anonymous$$d of the update one, seemed to be easier in my $$anonymous$$d but technically proved me wrong, thanks a lot