- Home /
Timer last 5 second counter
Hello i have problem counting delta timer last 5 second counts. i have image for explanation
Word explanation Timer have run 23 seconds and total click in 23 seconds is 72. Last 5 seconds is 20 clicks. i want make so that lable show that last 5 second counts, how i can do it ?. But i dont have idea how to make it so.
I would use a queue of times. Each time a player clicks the mouse add the time the mouse was clicked to the queue. Every Update remove all times that are older than 5 second. Then use the queue's length to deter$$anonymous$$e the number of clicks in the last 5 seconds.
okay ty i try figure out how use it now :D maybe i will get work it now if im so pro :D
Answer by Rick Love · Jun 21, 2015 at 11:29 AM
Something like the below will work to keep track of the last 5 seconds of ticks.
However, it will only update once a second, but it will be efficient:
private int lastWholeSecond;
private List<int> _ticksDuringSecond = new List<int>(6);
void Update()
{
int wholeSecond = (int)Time.time;
if( wholeSecond > lastWholeSecond )
{
lastWholeSecond = wholeSecond;
// Get rid of the oldest second
_ticksDuringSecond .RemoveAt(5);
// Add a new second at the beginning that starts at 0
_ticksDuringSecond .Insert(0,0);
}
// Add a tick to the current second
_ticksDuringSecond [0]++;
// Count the seconds from 1-5
// (Skip the current second because it is not done)
int sum = 0;
for(int i = 1; i <= 5; i++)
{
sum += _ticksDuringSecond [i];
}
TickCountLabel.text = "" + sum;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
not resetting the death count on reload 5 Answers
GUI Text Variable Counter In C# 1 Answer