- Home /
Outputting Reaction Time Data
Hi everyone,
Im creating an experiment with a driving simulator in Unity. I need to record player reaction time as soon as they enter a trigger. My problem is that I don't know how to get unity to provide me with a print out of the reaction time to each triggerevent. Here is the code ive been using so far:
using UnityEngine; using System.Collections;
public class ReactionTimer : MonoBehaviour {
class colliderWithTimer: MonoBehaviour
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;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I was wondering if; a) this is correct for what I want to do and b) if there is a way so that the system will print out the reaction time to each event?
Thank you all very much!
Answer by Mindmapfreak · Oct 03, 2016 at 08:44 PM
If you just want to print out the time in the editor, use Debug.Log. If you want to see the reaction time in the compiled application you have to use the UI system. For Debug.Log something like this should work:
void Update(){
if (isTiming)
{
timeTillKeyisPressed += Time.DeltaTime;
}
if (Input.GetKeyDown(Keycode.S))
{
isTiming = false;
Debug.Log("Reaction time: "+timeTillKeyisPressed+"ms"); //Or use UI here
timeTillKeyisPressed = 0; //Reset timer
}
}
Thank you very much! I tried this out and for some reason I can't figure out why I'm getting these 2 errors in the console:
Assets/Scripts/ReactionTimer.cs(8,15): error CS1031: Type expected
Assets/Scripts/ReactionTimer.cs(13,26): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
Is there something wrong in my code? Thanks again for all your help.
Can you show the part of the code that is causing the error (or the whole ReactionTimer.cs if that is not too big)?
Yup, Here it is:
using UnityEngine; using System.Collections;
public class ReactionTimer : $$anonymous$$onoBehaviour {
public class colliderTimer: $$anonymous$$onoBehaviour
private bool isTi$$anonymous$$g = false;
private float timeTill$$anonymous$$eyisPressed = 0;
void OnCollisionEnter(Collision collision) {
isTi$$anonymous$$g = true;
}
void Update(){
if (isTi$$anonymous$$g) {
timeTill$$anonymous$$eyisPressed += Time.DeltaTime;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eycode.S)) {
isTi$$anonymous$$g = false;
Debug.Log ("Reaction time: " + timeTill$$anonymous$$eyisPressed + "ms");
}
}
}