P1505C
P1505C

Reputation: 23

Unity GVR Gaze events with IEnumerator

I'm using an IEnumerator to work as a timer that can be reset. I have a waypoint system set up where you gaze at a waypoint for n seconds to teleport to it. I use the IEnumerator as a way to control that time, and to stop accidental teleportation. It works fine, but if I gaze at any raycast item it messes with the logic somewhere, and you have to look then re-look at a waypoint to start it all back up again. This failure runs across all instances of my waypoints also, look at one and look away and look at another will fail to teleport. I'musing GVR Laser Pointer and GVR Reticle from GVR build 1.70 (1.100 fails to build).

The code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gaze_Waypoints : MonoBehaviour {

public int gazeTime = 3; //the amount of time needed to look to teleport
public GameObject playerCam; //the players camera
private Vector3 waypointPosition; //the position of the current waypoint
private Vector3 playerCamPosition; //the current position of the players camera used for height information
private IEnumerator waypointEnumerator; //my co-routine
private bool startCoRoutine; //is the co-routine running

void Start () {
    waypointPosition = transform.position;  //get the position of the owners waypoint
    waypointEnumerator = waypointTimerEvent (); //set the ienumerator to the relevant function below
    startCoRoutine = true; //testing this out as true or false
}

void Update () {
    playerCamPosition = playerCam.transform.position; //keep track of the players camera, mainly for height info
}

// when I gaze a waypoint
public void PointerEnter() {
    Debug.Log("I entered.");
    if (startCoRoutine) {
        StartCoroutine (waypointEnumerator);
    } else {
        StopCoroutine (waypointEnumerator);
    }
    startCoRoutine = !startCoRoutine;
}

// when I look away
public void PointerExit() {
    Debug.Log("I exited.");
    StopCoroutine (waypointEnumerator);
}

// if I look for 3 seconds teleport the user, if I look away reset the timer
IEnumerator waypointTimerEvent() {
    yield return new WaitForSeconds (gazeTime);
    playerCam.transform.position = new Vector3 (waypointPosition.x, waypointPosition.y + playerCamPosition.y, waypointPosition.z);
    StartCoroutine(waypointEnumerator);
}
}

Upvotes: 0

Views: 139

Answers (1)

pause
pause

Reputation: 1

see different invocations of MonoBehaviour.StartCoroutine

  1. public Coroutine StartCoroutine(IEnumerator routine);
  2. public Coroutine StartCoroutine(string methodName, object value = null);

case 1. - not tested but obviously you have to use a loop in coroutine like

while (true){
    yield return new WaitForSeconds(waitTime);
    debug.Log("WaitAndPrint");
}

case 2. - sligthly modified but tested ok

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gaze_Timer : MonoBehaviour {

    public int gazeTime = 3; //the amount of time needed to look to teleport
    public GameObject playerCam; //the players camera
    private Vector3 startingPosition;
    private Vector3 changePosition;

    private bool startCoRoutine; //is the co-routine running

    void Start() {
        startingPosition = transform.localPosition;
        changePosition = startingPosition;
        startCoRoutine = false; //testing this out as true or false
    }

    void Update() {
        //...
    }

    // when I gaze 
    public void PointerEnter() {
        Debug.Log("I entered.");
        if (startCoRoutine) {
            StopCoroutine("waypointTimerEvent");
        } else {
            StartCoroutine("waypointTimerEvent", gazeTime);
        }
        startCoRoutine = !startCoRoutine;
    }

    // when I look away
    public void PointerExit() {
        Debug.Log("I exited.");
        if (startCoRoutine) {
            StopCoroutine("waypointTimerEvent");
            startCoRoutine = false;
        }
    }

    IEnumerator waypointTimerEvent(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        Debug.Log("I waited " + waitTime + " seconds");
        GetComponent<Renderer>().material.color = Color.blue;
        changePosition.y += 0.1f;
        transform.localPosition = changePosition;
    }
}

Upvotes: 0

Related Questions