Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question shaking timer

Discussion in 'UGUI & TextMesh Pro' started by lili412, Aug 21, 2021.

  1. lili412

    lili412

    Joined:
    Dec 25, 2020
    Posts:
    14
    Hi!

    I have created a timer in the following way and it works as intended however the text seems to shake and jitter as the milliseconds increase and I am wondering how to keep it stable. I thought at first it was due to the size of the UI TMPro container because I initially just disabled text wrapping but even after making it fit inside it still shakes. Is the shaking just due to the numbers changing very quickly? I would like to keep the time in this format as it will be used for a highscore and milliseconds is a nice touch for that. Any help would be appreciated! (a picture of the inspector is also attached. The only things I really changed was font size, alignment, and disabling wrapping)

    public class TimerController : MonoBehaviour
    {
    [SerializeField] private TextMeshProUGUI timeText;

    private float startTime;
    private float elapsedTime;
    TimeSpan timePlaying;

    private void Update()
    {
    elapsedTime = Time.time - startTime;
    timePlaying = TimeSpan.FromSeconds(elapsedTime);

    string timePlayingString = timePlaying.ToString("mm' : 'ss' . 'ff");
    timeText.text = timePlayingString;
    }
    }

    public void StartTime()
    {
    startTime = Time.time;
    }
    }
     

    Attached Files:

  2. Chewico

    Chewico

    Joined:
    May 9, 2020
    Posts:
    2
    Ok, I had the same problem, when the text shows 11 (for me), the font and all moves and when it changes it returns to normality, what i have done is an algorithm that checks if there is an 11 and replaces it with the 10.