Search Unity

Question TextMesh Pro UI Lerp not working while Camera is also Lerping

Discussion in 'Scripting' started by Only4gamers, Feb 9, 2021.

  1. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Hello everyone,
    TextMesh Pro UI Lerp not working while Camera is also Lerping. Here is video of this problem.



    There are two things are Lerping. First Camera and second TM Pro UI for Countdown. Countdown texts should Lerp Left to center. As you can see in the above video "3" and "GO!" are lerping as expected because Camera is still both times. But "2" and "1" are not lerping because camera is moving both times.

    These are some extra infos, maybe relevant:
    1. Canvas Render Mode is Screen Space - Camera. As I am using bloom effect on TM Pro UI texts, I can't change it.
    2. Canvas Plane Distance is 0.06.
    3. Camera Clipping Planes near is 0.01.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Post your code?
     
  3. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Yes. I am not posting whole script but only relevant parts.
    Code (CSharp):
    1. Transform tra;
    2. Vector3 from;
    3. Vector3 to;
    4.  
    5. // Code for Camera Lerp
    6.  
    7. StartCoroutine(LerpText());
    8.  
    9. if(tmp_Countdown.text == "3")
    10. {
    11.                  
    12. }
    13.  
    14. else if(tmp_Countdown.text == "2")
    15. {
    16.          StartCoroutine(LerpCameraFirst());
    17. }
    18.  
    19. else if(tmp_Countdown.text == "1")
    20.  {
    21.           StartCoroutine(LerpCameraSecond());
    22.   }
    23.  
    24. else if(tmp_Countdown.text == "GO!")
    25. {
    26.           cameraP1.GetComponent<Cam_Follow>().enabled = true;
    27.           Time.timeScale = 1;
    28.           tmp_Countdown.fontSize = 150;
    29.           _audio.pitch = 0.7f;
    30.           _audio.volume = 0.5f;
    31.  }
    32.  
    33.  
    34. public Transform three;
    35.     public Transform two;
    36.     public Transform one;
    37.  
    38.     IEnumerator StartCutScene()
    39.     {
    40.         cameraP1.transform.position = three.position;
    41.         cameraP1.transform.rotation = three.rotation;
    42.      
    43.      
    44.         yield return null;
    45.     }
    46.  
    47.  
    48.     IEnumerator LerpCameraFirst()
    49.     {
    50.         Transform from = three;
    51.         Transform to = two;
    52.         tra = cameraP1.transform;
    53.         var t = 0f;
    54.  
    55.         while (t < 1f)
    56.         {
    57.             t += 1 * Time.deltaTime;
    58.             tra.position = Vector3.Lerp(from.position, to.position, t);
    59.             tra.rotation =  Quaternion.Lerp (from.rotation, to.rotation, t);
    60.             yield return null;
    61.         }
    62.         tra.position = to.position;
    63.         tra.rotation = to.rotation;
    64.  
    65.     }
    66.  
    67.  
    68.     IEnumerator LerpCameraSecond()
    69.     {
    70.         Transform from = two;
    71.         Transform to = one;
    72.         tra = cameraP1.transform;
    73.         var t = 0f;
    74.  
    75.         while (t < 1f)
    76.         {
    77.             t += 1 * Time.deltaTime;
    78.             tra.position = Vector3.Lerp(from.position, to.position, t);
    79.             tra.rotation =  Quaternion.Lerp (from.rotation, to.rotation, t);
    80.             yield return null;
    81.         }
    82.         tra.position = to.position;
    83.         tra.rotation = to.rotation;
    84.     }
    85.  
    86.  
    87.  
    88. // Code for TM Pro UI Lerp:
    89. IEnumerator LerpText()
    90.     {
    91.         from = new Vector3(-40, transform.localPosition.y, transform.localPosition.z);
    92.         to = new Vector3(0, transform.localPosition.y, transform.localPosition.z);
    93.         tra = tmp_Countdown.transform;
    94.         var t = 0f;
    95.  
    96.         while (t < 1f)
    97.         {
    98.             t += 1 * Time.deltaTime;
    99.             tra.localPosition = Vector3.Lerp(from, to, t);
    100.             yield return null;
    101.         }
    102.         tra.localPosition = to;
    103.     }
     
    Last edited: Feb 9, 2021
  4. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Nothing jumps out at me from the snippets you've posted other than you've only called
    StartCoroutine(LerpText());
    once at the very start.

    You can also clean up some of the code by passing parameters to a function instead of copy and pasting your code, so instead of LerpCameraFirst, LerpCameraSecond, LerpCameraThird, you just have
    LerpCamera(Vector3 from, Vector3 to)
    , which will make your code less error prone and easier to debug what's going wrong.
     
    Only4gamers likes this.
  6. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Yeah, thanks but as I said I didn't posted whole script. Problem is not in script as far as I can understand. If I am disabling Camera Lerp then TM Pro UI Lerp is working as expected. Below script part is calling 4 times for 3, 2, 1 and GO!:
    Code (CSharp):
    1. StartCoroutine(LerpText());
    2. if(tmp_Countdown.text == "3")
    3. {
    4.                
    5. }
    6. else if(tmp_Countdown.text == "2")
    7. {
    8.          StartCoroutine(LerpCameraFirst());
    9. }
    10. else if(tmp_Countdown.text == "1")
    11. {
    12.           StartCoroutine(LerpCameraSecond());
    13.   }
    14. else if(tmp_Countdown.text == "GO!")
    15. {
    16.           cameraP1.GetComponent<Cam_Follow>().enabled = true;
    17.           Time.timeScale = 1;
    18.           tmp_Countdown.fontSize = 150;
    19.           _audio.pitch = 0.7f;
    20.           _audio.volume = 0.5f;
    21. }