- Home /
incrementing instead of speeding up ingame clock
my ingame clock increments instead of speeding up. i have created 3 methods between x1, x5, and x10 to speed up the clock when i press a button. here's my code for clear understanding
Someone can help me figure it out? Thank you!
public Text DateTimeText;
float gameTimer = 0f;
void Update ()
{
gameTimer += Time.deltaTime;
int seconds = (int) (gameTimer % 60);
int minutes = (int) (gameTimer / 60) % 60;
int hours = (int) (gameTimer / 3600) % 24;
string timeString = string.Format("{0:0}:{1:00}:{2:00}",hours,minutes,seconds);
DateTimeText.text = timeString;
}
public void x1()
{
gameTimer += Time.deltaTime;
}
public void x5()
{
gameTimer += Time.deltaTime * 300;
}
public void x10()
{
gameTimer += Time.deltaTime * 600;
}
Answer by LCStark · Oct 01, 2018 at 07:59 AM
Your Update
method increments your gameTimer
by Time.deltaTime
every frame. Your three xY
functions don't change the speed - they only add the time of a single frame (multiplied by 300 or 600). If you want to change the speed with which your clock is running, you need to multiply the deltaTime
value in your Update
method.
Add a float
field called timeMultiplier
and set it to 1.0f
by default. Make your xY
functions set that value to 1
, 5
and 10
. Replace gameTimer += Time.deltaTime;
in the Update
method with gameTimer += Time.deltaTime * timeMultiplier;
.
thank you! did exactly everything and now it speeds up!
Answer by cnczcraft · Oct 01, 2018 at 07:33 AM
When x5 is called it's just adding 300. Make it set a variable to 300 and factor that variable into the first line of your update.