我正在使用IEnumerator作为可以重置的计时器。我有一个航点系统设置,你可以在这里观察一个航路点n秒钟传送到它。我使用IEnumerator来控制那个时间,并阻止意外的远距传送。它工作得很好,但是如果我注视任何一个光线投射项目,它会在某个地方与逻辑混淆,你必须看一下然后重新看一个航点再次启动它。这个失败也会在我的航点的所有实例中运行,看一眼并远视并看另一个将无法传送。我正在使用GVR激光指针和来自GVR的GVR Reticle构建1.70(1.100无法构建)。
代码:
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);
}
}
答案 0 :(得分:0)
查看MonoBehaviour.StartCoroutine
的不同调用案例1. - 未经测试但很明显 你必须在协程中使用循环,如
4278255873
-16711423
案例2. - 经过修改但经过测试确定
while (true){
yield return new WaitForSeconds(waitTime);
debug.Log("WaitAndPrint");
}