- Home /
How to make a timer read to the .001 of a second
I'm in the process of making a drag racing game. i have a timer that starts my tree activation sequence (the 3 lights the light up sequentially before the green light) then logging the reaction time when the car leaves the starting line trigger. and as of now it works, however it only logs in ever .02 seconds and i need to much higher resolution to measure the time. reaction time is usually measured to the .00x of a second and sometimes a race is won to the .000x of a second. how can i make the timer read at a higher resolution? i know my code probably isnt the cleanest and most efficient as i am a beginner game maker but im understanding it better now.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RightTree : MonoBehaviour
{
public MeshRenderer Yellow1Off;
public MeshRenderer Yellow2Off;
public MeshRenderer Yellow3Off;
public MeshRenderer Yellow1On;
public MeshRenderer Yellow2On;
public MeshRenderer Yellow3On;
public MeshRenderer GreenOff;
public MeshRenderer GreenOn;
public MeshRenderer RedOff;
public MeshRenderer RedOn;
public IsStagedRight stagedR;
public DisqualifiedR disqualifiedR;
public RightTree treeActiveR;
public float timer;
public string seconds;
public Text ReactionRight;
private void Start()
{
Yellow1Off.enabled = true;
Yellow2Off.enabled = true;
Yellow3Off.enabled = true;
Yellow1On.enabled = false;
Yellow2On.enabled = false;
Yellow3On.enabled = false;
GreenOff.enabled = true;
GreenOn.enabled = false;
RedOff.enabled = true;
RedOn.enabled = false;
treeActiveR.enabled = false;
}
private void FixedUpdate()
{
if (treeActiveR.enabled == true)
{
timer += Time.deltaTime;
seconds = (timer % 60).ToString("f3");
timer = float.Parse(seconds);
if (timer == 1f)
{
Yellow1Off.enabled = false;
Yellow1On.enabled = true;
}
if (timer == 1.5f)
{
Yellow1Off.enabled = true;
Yellow1On.enabled = false;
Yellow2Off.enabled = false;
Yellow2On.enabled = true;
}
if (timer == 2f)
{
Yellow2Off.enabled = true;
Yellow2On.enabled = false;
Yellow3Off.enabled = false;
Yellow3On.enabled = true;
}
if (timer >= 2.5f && stagedR.enabled == true)
{
Yellow3Off.enabled = true;
Yellow3On.enabled = false;
GreenOff.enabled = false;
GreenOn.enabled = true;
Debug.Log(timer - 2.5f);
//log reaction time in text box
ReactionRight.text = (timer - 2.5f).ToString("f3");
}
if (timer < 2.5f && stagedR.enabled == false)
{
disqualifiedR.enabled = true;
Debug.Log(timer - 2.5f);
//log reation time in text box
ReactionRight.text = (timer - 2.5f).ToString("f3");
}
}
}
}
This is going to be dependent on how your frame rate is. If you have 50 fps, you have a new frame every 0.02f seconds. Try increasing your target frame rate:
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = 300;
Answer by stuntrunt · Jun 24, 2019 at 09:56 PM
I've decided to run a separate timer in the update function to read the reaction time since I couldn't come up with a solution that would drastically hurt the gameplay of the game.
Answer by Bunny83 · Jun 20, 2019 at 10:18 AM
Since he does all the timer stuff inside FixedUpdate changing targetFrameRate won't do anything. He has to increase the physics framerate by lowering the fixedDeltaTime step.
Oops ya, I missed that. $$anonymous$$y solution would be for if you had it in Update().
I've tried to decrease the physics timestep however when doing so it lag down really bad (and I have a very decent computer). I tried to change to Update() ins$$anonymous$$d of FixedUpdate() and things no longer worked like they should however I didnt change Time.deltaTime to Time.fixedDeltaTime.
Your answer
Follow this Question
Related Questions
Game Timer help needed 2 Answers
Best time and saving best time 3 Answers
Timer doesn't work properly 2 Answers
timer not ticking down 2 Answers