2017-10-17 6 views
0

IEnumerator를 사용하여 재설정 할 수있는 타이머로 작동합니다. 나는 웨이 포인트 시스템을 가지고있다. 당신이 n 초 동안 웨이 포인트를보고 응시 한 곳으로 텔레포트한다. IEnumerator를 사용하여 시간을 제어하고 우발적 인 순간 이동을 막을 수 있습니다. 괜찮 으면 좋겠지 만, 어떤 레이캐스팅 항목을 보면 로직 어딘가에 덤비다. 그러면 웨이 포인트를 다시 보면서 모든 것을 다시 시작해야한다. 이 실패는 내 웨이 포인트의 모든 인스턴스에서도 실행됩니다. 하나를보고 멀리 보아 다른 곳을보고 순간 이동하지 못합니다. 나는 GVR 레이저 포인터와 GVR 레티클을 1.70 (1.100 빌드 실패)에서 구축하고 있습니다.IEnumerator로 Unity GVR 시선 이벤트

코드 :

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

이 MonoBehaviour.StartCoroutine

  1. 공공 코 루틴 StartCoroutine (IEnumerator를 루틴)의 서로 다른 호출을 참조;
  2. 공용 Coroutine StartCoroutine (string methodName, object value = null);

경우 1. - 테스트하지하지만 분명히 당신은

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

경우 2. 같은 코 루틴에 루프를 사용해야합니다 - sligthly 수정되었지만 테스트 확인

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; 
    } 
} 
관련 문제