- Home /
on trigger enter, start timer, end when button is pressed & output reaction time.
I'm wondering if anyone has some insight on how to start a timer when the player enters a collider (is trigger). I want the player to react as quickly as possible by pressing a button in response to entering the trigger collider. I would also like the system to output a reaction time for each trigger collider. Is this possible?
Answer by hoogemast · Sep 23, 2016 at 07:55 AM
If i understand correctly what you would like to accomplish I would write something like this:
class colliderWithTimer: MonoHehaviour
private bool isTiming = false;
private float timeTillKeyIsPressed = 0;
void OnCollisionEnter(Collision collision) {
isTiming = true;
}
void Update(){
if (isTiming)
{
timeTillKeyIsPressed += Time.DeltaTime;
}
if (Input.GetKeyDown(KeyCode.S)) // I used S, but ofc this can be something else
{
isTiming = false;
}
}
now the timeTillKeyisPressed can be used to display the time.
Take note!! i do not know if it is the best practice to time in the update function with time.deltatime, but since you catch the keypressed in the update function as well i suspect this wouldn't give any performance issues. However when your framerate drops very low this can be a problem (but again the keypressed wont be caught either).
Thank you so much!!! I will try this out and if it works out, I will find a way to compensat eyou :)) Thank you!
Hey do you know how I can get it to show the reaction time (either in an onscreen window or in the editor/console?
yeah just add debug.log to show it in the console. Just like this:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)) { isTi$$anonymous$$g = false; Debug.Log(timeTill$$anonymous$$eyIsPressed) }
if you want to show it on screen you could try to use a ui element and change the text to with the timeTill$$anonymous$$eyIsPressed. You should of course cast the variable to a string.
Your answer
Follow this Question
Related Questions
Javascript beginner here: how do I implement time such as hours and days? 2 Answers
Why does my MatchTime Class not work when using Properties? 1 Answer
Static Variable Doesn't Change ? 0 Answers
reaction game: measure time of touch exactly? 1 Answer
How to make a date/time system with a fast forward feature? 2 Answers