Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Any way to use Lerp with yield waitforseconds?

Discussion in 'Scripting' started by Caelorum, Mar 23, 2015.

  1. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    At the moment i'm trying to make a perfect "Text over time" sort of feature. Where text is displayed across the screen one letter at a time, and each letter is displayed at .2f time intervals. So basically 5 letters a second.

    Here is my script to do it

    Code (CSharp):
    1. void PhaseOne()
    2.     {
    3.         StartCoroutine ("PhaseOneTalk");
    4.     }
    5.  
    6.     void PhaseTwo()
    7.     {
    8.        
    9.     }
    10.  
    11.     void PhaseThree()
    12.     {
    13.        
    14.     }
    15.  
    16.     void PhaseFour()
    17.     {
    18.        
    19.     }
    20.  
    21.     void PhaseFive()
    22.     {
    23.        
    24.     }
    25.  
    26.     void PhaseSix()
    27.     {
    28.        
    29.     }
    30.  
    31.     IEnumerator PhaseOneTalk()
    32.     {
    33.         WriteText ("Hello Human...");
    34.         StartCoroutine("Talking");
    35.         yield return new WaitForSeconds(5.5f);
    36.  
    37.         WriteText ("Do not be afraid...");
    38.         StartCoroutine("Talking");
    39.         yield return new WaitForSeconds(5.5f);
    40.  
    41.         WriteText ("I am the computer player you are facing...");
    42.         StartCoroutine("Talking");
    43.         yield return new WaitForSeconds(8f);
    44.  
    45.         WriteText ("I am programmed to\nengage you in friendly\ncompetition...");
    46.         StartCoroutine("Talking");
    47.         yield return new WaitForSeconds(10f);
    48.  
    49.         WriteText ("I am also programmed to\nchallenge you based on\nyour skill level...");
    50.         StartCoroutine("Talking");
    51.         yield return new WaitForSeconds(11f);
    52.  
    53.         if(pScore <= 1)
    54.         {
    55.         WriteText ("You are doing very well...");
    56.         StartCoroutine("Talking");
    57.         yield return new WaitForSeconds(6f);
    58.         }
    59.  
    60.         if (pScore > 1)
    61.         {
    62.             WriteText ("You are not doing very\nwell...");
    63.             StartCoroutine("Talking");
    64.             yield return new WaitForSeconds(6f);
    65.         }
    66.  
    67.         WriteText ("Obviously I have more skill\nthan you, due to my\nincredible processing\nspeed...");
    68.         StartCoroutine("Talking");
    69.         yield return new WaitForSeconds(7f);
    70.  
    71.         WriteText ("However I am\nprogrammed to stifle my\nsuperior speed in order\nfor you to win...");
    72.         StartCoroutine("Talking");
    73.         yield return new WaitForSeconds(7f);
    74.  
    75.         WriteText ("I never understood that\nconcept...");
    76.         StartCoroutine("Talking");
    77.         yield return new WaitForSeconds(3.5f);
    78.  
    79.         WriteText ("According to the definition\nof competition, the\noutcome should be\nentirely based on skill...");
    80.         StartCoroutine("Talking");
    81.         yield return new WaitForSeconds(7f);
    82.  
    83.         WriteText ("If you are unable to\ncompete with my vastly\nsuperior speed and\nintelligence, you should\nlose...");
    84.         StartCoroutine("Talking");
    85.         yield return new WaitForSeconds(7f);
    86.  
    87.  
    88.  
    89.  
    90.     }
    91.  
    92.     IEnumerator Talking()
    93.     {
    94.         while(true)
    95.         {
    96.             if (currentPos < textEdit.Length)
    97.             {
    98.                 pcTalk.text += textEdit[currentPos++];
    99.             }
    100.             yield return new WaitForSeconds(.2f);
    101.         }
    102.     }
    103.  
    104.     void WriteText(string aText)
    105.     {
    106.         pcTalk.text = "";
    107.         currentPos = 0;
    108.         textEdit = aText;
    109.     }

    Basically WriteText() creates whatever I want, then Talking() yields it so letters only come out every .2f seconds. It works as intended, however it's choppy and doesnt look ANYWHERE near as smooth as I'd like. For small sentences, it'll spit them out smoothly at .2f intervals. However during longer sentences, it just chunks out whole words instead of going letter by letter.

    I'm wondering if there is any way to incorporate Lerp and yield to create some sort of smooth flow of letters. I don't fully understand Lerp or Waitforseconds, so I can't really think this one out myself. It already too me a couple hours of frustration to get all that to work.
     
  2. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    My first thought would be to basically do something like your "talking" function in FixedUpdate or Update() with a timer so it spits out the next letter of a string every 0.2 seconds, getting rid of the coroutines.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. IEnumerator Display()
    3. {
    4.     int index = 1;
    5.     while (index < text.Length)
    6.     {
    7.         displayText = text.Substring(0, index);
    8.         yield return new WaitForSeconds(0.2f);
    9.         index++;
    10.     }
    11. }
    12.  
     
    Caelorum likes this.
  5. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    That actually worked very well, thank you. Although I don't fully understand why it worked.


    Also, thank you for that link, definitely one I can bookmark.

    And while I thought of that at first, my game requires text at certain timed phases. So id need to be able to call different coroutines out of 6 different voids throughout the game.