Reputation: 3045
I was using this C# code in Unity3d to count up from zero seconds and display the result as well as increase a bar, but now I need it to count down from 60 seconds, I've replaced the values with 60, but it doesn't work, it actually counts down from 6 mins.
void UpdateTime()
{
currentTime -= Time.deltaTime / 60f;
barAmount = (int)Mathf.Lerp(0f, 100f, currentTime);
timeBar.valueCurrent = barAmount;
float cTime = 60f * currentTime;
TimeSpan timeSpan = TimeSpan.FromSeconds(cTime);
timeTxt.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}",timeSpan.Days ,timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
Debug.Log (timeTxt.text);
}
What am I doing wrong?
Upvotes: 0
Views: 1020
Reputation: 1213
public String s;
public float currentTime = 60f;
private int barAmount;
void Update () {
currentTime -= Time.deltaTime;
barAmount = (int) Mathf.Lerp(0f, 100f, currentTime);
// timeBar.valueCurrent = barAmount;
float cTime = currentTime;
TimeSpan timeSpan = TimeSpan.FromSeconds(cTime);
s = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}",timeSpan.Days ,timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
}
I just made a quick test and it works flawlessly. You don't need to divide through 60. Time.deltaTime
is actually in seconds.
Upvotes: 1