Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FallingRocketGames · Sep 29, 2017 at 05:53 PM · 2dcoroutine2d animationienumerator

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 :(

alt text

01.png (113.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FallingRocketGames · Sep 30, 2017 at 12:54 AM

Indeed my problem had a bad approach it was easier to make this outside the update function, thanks to > @Nyro for pointing out a different approach, turned out to be easier to set the number in the same "Next" button as a function called inside the On Click() resulting in something like this: public void TextID() { textID= (int)VD.nodeData.extraVars["cuadro"]; print(textID); switch (textID) { case 0: break; case 1: StartCoroutine(LipsMoving(3)); break; case 2: StartCoroutine(LipsMoving(6)); break; } } IEnumerator LipsMoving(int talkTime) { funcionesScript.Speaking(); yield return new WaitForSeconds(talkTime); funcionesScript.Idle(); yield return null; } In this way I get the frame ID as the user clicks the button and also runs my coroutine without weird loops. Thanks again for the help and patience it's refreshing to have different points of view to find the right way, hope this is useful to you guys
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FallingRocketGames · Sep 29, 2017 at 09:13 PM 0
Share

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?

avatar image FallingRocketGames · Sep 29, 2017 at 09:54 PM 0
Share

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.

avatar image FallingRocketGames · Sep 30, 2017 at 12:15 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem With Coroutine - Infinite Loop? 1 Answer

Line of code skips randomly at times? 1 Answer

Destroy Without A Collision Or Trigger 2 Answers

2D Grid Movement - StartCoroutine, IEnumerator, SmoothMovement best practices? 0 Answers

Do Something Relative to Animation Time 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges