// Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;
        globalTimer += Time.deltaTime;

        if (Input.GetKeyDown(KeyCode.P))
        {
            if (pilotIndex < 0)
            {
                RunPilot();
            }
        }

        // Doesn't check input if not enough time has elapsed since last input
        if (timer < TIMER_THRESHOLD || pilotIndex < 0)
        {
            return;
        }



        if ((Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.X) || OVRInput.Get(OVRInput.Button.One) || OVRInput.Get(OVRInput.Button.Three)))
        {
            //Only triggers for every other, in order to save data
            if (pilotIndex > 5 && pilotIndex < 24 && pilotIndex % 2 == 0)
            {
                if (fileName == "" || type == "" || barChosen == -1 || responseRatio == -1)
                {
                    Debug.Log("ERROR - Attempted to record incomplete info.");
                    return;
                }
            }


            // This triggers for all of the experimental steps
            if (pilotIndex >= PREAMBLE_LEN - 1 && pilotIndex < (2 * NUM_EXPERIMENTS) + PREAMBLE_LEN)
            {
                

                if (pilotIndex % 2 == 0)
                {
                    // This section records the previous step
                    if (pilotIndex > PREAMBLE_LEN + 1)
                    {
                        responseRatio = meter.GetNum() / 100.0f;

                        // Logs the past entry
                        // NOTE: DOES NOT SAVE TO DISK. Use logger.CloseFile() to save all logged entries. 
                        logger.RecordResults(type, deltaSecsInit, fileName,
                            head.transform.position, head.transform.rotation,
                            VisualizationObject.transform.position, VisualizationObject.transform.rotation,
                            barChosen, responseRatio, timer);
                    }

                    int i = (pilotIndex - PREAMBLE_LEN) / 2;

                    fileName = fileNames[i];

                    Vector3 personalDeltaPos = headObjInitPos - head.transform.position,
                        personalDeltaRot = headObjInitRot - head.transform.rotation.eulerAngles;


                    VisualizationObject.transform.rotation = Quaternion.Euler(vizObjInitRot + rotations[i] + personalDeltaRot);
                    VisualizationObject.transform.position = vizObjInitPos + translations[i] + personalDeltaPos;


                    // This section shows the upcoming step
                    instructionTextMesh.text = textArray[PREAMBLE_LEN];


                    controller.WriteSpecFile(fileName + "Spec", templateString[0] + fileName + templateString[1]);
                    controller.UpdateVis(fileName + "Spec.json");
                    remainingCounterTextMesh.text = (((pilotIndex - PREAMBLE_LEN) / 2) + 1) + "/" + NUM_EXPERIMENTS;

                    meter.SetStatus(false);
                }
                else
                {
                    // This section records the previous step
                    barChosen = Input.GetKeyDown(KeyCode.Z) || OVRInput.Get(OVRInput.Button.Three) ? 0 : 1;
                    deltaSecsInit = timer;

                    // This section shows the upcoming step
                    instructionTextMesh.text = textArray[PREAMBLE_LEN + 1];

                    meter.SetStatus(true);
                }
            }

            // Triggers only on the last slide
            else if (pilotIndex == (2 * NUM_EXPERIMENTS) + PREAMBLE_LEN)
            {
                responseRatio = meter.GetNum() / 100.0f;

                // Logs the past entry
                // NOTE: DOES NOT SAVE TO DISK. Use logger.CloseFile() to save all logged entries. 
                logger.RecordResults(type, deltaSecsInit, fileName,
                    head.transform.position, head.transform.rotation,
                    VisualizationObject.transform.position, VisualizationObject.transform.rotation,
                    barChosen, responseRatio, timer);

                instructionTextMesh.text = textArray[PREAMBLE_LEN + 2];

                VisualizationObject.SetActive(false);
                meter.SetStatus(false);
            }

            // Triggers after the last slide - closes and saves everything
            else if (pilotIndex > (2 * NUM_EXPERIMENTS) + PREAMBLE_LEN)
            {
                pilotIndex = -1;

                logger.CloseFile();

                instructionPanel.SetActive(false);
                return; //neccesary so it doesn't increment index again
            }

            // Triggers for the preamble slides
            if (pilotIndex == 0)
            {
                VisualizationObject.SetActive(true);
                counterText.SetActive(true);
            }

            logger.LogPose(globalTimer, head.transform.position, head.transform.rotation.eulerAngles);
            timer = 0;
            pilotIndex++;

        }
    }