Start timer with GVR controller

I’m new to unity and would like to develop a simple VR game that a timer starts when the player looks at the Start button and the timer stops when the player looks at the stop button. The timer function works, but I am having trouble integrating it with the gazedAt function. At the moment, the timer starts automatically without looking at it. Any help would be great!!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Timer : MonoBehaviour {


    public Text timerLabel;
    public bool pauseTimer = true;

    private float time;

    void Start() {
        SetGazedAt(false);
    }

    void Update() {

        if(pauseTimer)
            time += Time.deltaTime;

        var minutes = Mathf.Floor(time / 60);
        var seconds = time % 60;//Use the euclidean division for the seconds.
        var fraction = (time * 100) % 100;

        //update the label value
        timerLabel.text = string.Format ("{0:00} : {1:00} : {2:00}", minutes, seconds, fraction);
    }

    //Start Timer
    public void SetGazedAt(bool gazedAt){
        //Start Timer Here
        pauseTimer = true;
        Debug.Log("Timer Started");
    }
}

Set to true on line 9
public bool pauseTimer = true;

Increases timer if true on line 19 & 20
if(pauseTimer)
time += Time.deltaTime;

Perhaps you should set it to false by default:
public bool pauseTimer = false;

and now If condition is false so timer will not increase:
if(pauseTimer)

Until function SetGazedAt(true) is called. You could do this with some gaze input module and Event Trigger.

1 Like

Thank you so much, it worked! Also thanks for your tutorials, they are super helpful!