How to code a simple C# timer?
I think this is quite simple, but I'm fairly new to C sharp so I'm not too sure.
Basically I have a game where the user is a ball, and they have to travel down a long corridor collecting pick-ups a long the way, the game finishes when the player reaches the end of the corridor and they have collected all the pick-ups. What I need help with is coding the timer, I just need a timer that has a format of 00:00:00 (mm:ss:ms) and it starts when the player begins to move. And it stops as soon as all the pickups are collected. There are 7 picks-ups.
Answer by $$anonymous$$ · Sep 30, 2015 at 09:00 PM
I don't know how does your code look like but I'll give you a code which displays the time in the format that you'd like to display:
First of all you need 4 variables for this. A variable which represents time, one for milliseconds, one for seconds and one for the minutes. Plus you need another variable to only add up time if you need to.
private float millisecond, time;
private int second, minute;
private bool timerIsOn = true;
In your Update method, the magic happens. First you store Time.deltaTime in your time variable, which is basically the time passed between frames, since your game doesn't always run at 60fps or so. You add up this number, which is around 0.016 per frame or so, as many times as you need, we're gonna use a second here for our measurement. Then it's plain as it is, if a second has passed, you increment your second variable by one, when 60 seconds have passed you increment your minute variable by one etc.. For the milliseconds I used our time variable multipled by 100 to get the correct value.
void Update () {
time += Time.deltaTime;
if (timerIsOn) {
//set second
//set second
if (time > 1.0f) {
second++;
time = 0.0f;
//set the minute
if (second > 59) {
second = 0;
minute++;
}
}
//set millisecond
millisecond = Mathf.Floor(time * 100.0f);
}
}
Then for the displaying. I wrote a method which makes the display nicer, which here means that you don't get any result like having none formatted values like: 1: 1: 5 instead you get 01:01:05. If you want I can explain this to you in comment.
The method looks like this:
string zero(int _time){
if (_time < 10)
return "0";
else
return "";
}
Last but not least you have to display these variable through your OnGUI() method
void OnGUI(){
//Draw Time
GUI.Label (new Rect(Screen.width * 0.025f, Screen.height - Screen.height * 0.2f, 30, 50), zero (minute)+minute.ToString() +":");
GUI.Label (new Rect(Screen.width * 0.025f + 30, Screen.height - Screen.height * 0.2f, 30, 50), zero (second)+second.ToString() +":");
GUI.Label (new Rect(Screen.width * 0.025f + 60, Screen.height - Screen.height * 0.2f, 25, 50), zero ((int)millisecond)+millisecond.ToString());
}
To stop the timer when you get 7 objects I'd suggest you to use a code which when your variable which counts the gathered objects turns to 7, basically turn timerIsOn variable to false.
Thanks, with your code I was able to put a working timer that stopped when I collected all 7 objects. I just have another question, I currently have 2 other things on the UI which are Win Text and the Object Count, these are part of the unity UI so I can change the font and colour etc. The timer seems to be separate to that UI, so I was wondering if there was a way to get the timer linked to the unity UI? I can't really explain it very well, but thanks for your help I appreciate it :)
I'm not really sure about that. By the way, if you're satisfied with my answer could you please accept it as the proper answer ? :)
Answer by lloladin · Sep 30, 2015 at 08:27 PM
the simplest way is to Use Time.DeltaTime
example
Public Float coldown
public float coldownStore;
public void Start()
{
coldownStore = coldown;
}
public void Update()
{
if (input.getkey(keycode.Space))
{
coldown -= Time.deltatime();
}
if (coldown <= 0)
{
// Do stuff
coldownStore = coldown;
}
}
this basicaly makes the timer go down aslong as you hold space down and if the coldown hits 0 it sets coldown to be = to the coldownStore / resets the timer hope this helps Note this Code is untested and wont work
now i cant help you with the formating right now since i dont have my computer with the code before friday and i cant memorize it :( hope this atleast helps with how to make timers :)
Your answer
Follow this Question
Related Questions
Trying to get Player to shoot in both directions depending on what side he is facing? 0 Answers
Loot table problems 1 Answer
Left hand side of an assignment must be a variable, a property or an indexer 1 Answer
How would i modify my current script to remove these UI buttons without killing my pause menu? 1 Answer
How can I instantiate a object on the world coordinates 1 Answer