在谷歌纸板示例代码中添加时间延迟多维数据集传送

时间:2015-10-08 18:58:34

标签: android unity3d google-cardboard

当我看到(将gazepointer聚焦在立方体上)立方体瞬移(消失然后来到新的地方)时,我想在其传送活动之间增加一些时间延迟。

1.)Teleport.cs

    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof(Collider))]
    public class Teleport : MonoBehaviour {
      private Vector3 startingPosition;

      void Start() {
        startingPosition = transform.localPosition;
        SetGazedAt(false);
      }

      public void SetGazedAt(bool gazedAt) {
        GetComponent<Renderer>().material.color = gazedAt ? Color.green : Color.red;
      }

      public void Reset() {
        transform.localPosition = startingPosition;
      }

      public void ToggleVRMode() {
        Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
      }

      public void TeleportRandomly() {
        Vector3 direction = Random.onUnitSphere;
        direction.y = Mathf.Clamp(direction.y, 0.5f, 1f);
        float distance = 2 * Random.value + 1.5f;
        transform.localPosition = direction * distance;
      }
    }

2.)Teleportlegacy.cs

    using UnityEngine;
    using System.Collections;

    public class TeleportLegacyUI : Teleport {
      private CardboardHead head;

      void Awake() {
        head = Camera.main.GetComponent<StereoController>().Head;
        CardboardOnGUI.IsGUIVisible = true;
        CardboardOnGUI.onGUICallback += this.OnGUI;
      }

      void Update() {
        RaycastHit hit;
        bool isLookedAt = GetComponent<Collider>().Raycast(head.Gaze, out hit, Mathf.Infinity);
        SetGazedAt(isLookedAt);
        if (Cardboard.SDK.Triggered && isLookedAt) {
                print("gazed");
                ///maaaaibbbbbbbnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
               TeleportRandomly();
               //Update2();
        }
      }

      void OnGUI() {
        if (!CardboardOnGUI.OKToDraw(this)) {
          return;
        }
        if (GUI.Button(new Rect(50, 50, 200, 50), "Reset")) {
          Reset();
        }
        if (GUI.Button(new Rect(50, 110, 200, 50), "Recenter")) {
          Cardboard.SDK.Recenter();
        }
        if (GUI.Button(new Rect(50, 170, 200, 50), "VR Mode")) {
          ToggleVRMode();
        }
      }

      void OnDestroy() {
        CardboardOnGUI.onGUICallback -= this.OnGUI;
      }
    }

如何在立方体的传送活动之间进行时间延迟?

1 个答案:

答案 0 :(得分:1)

我建议使用一个协程,你可能需要添加一个标志或其他东西以避免它被双重触发(你可以在协程结束时重置它)

public IEnumerator TeleportCoroutine()
{
    yield return new WaitForSeconds(3.5f);
    Vector3 direction = Random.onUnitSphere;
    direction.y = Mathf.Clamp(direction.y, 0.5f, 1f);
    float distance = 2 * Random.value + 1.5f;
    transform.localPosition = direction * distance;
 }

  public void TeleportRandomly() {
    StartCoroutine(TeleportCoroutine());
  }

编辑:如果您想要在凝视一段时间后使物体变形,请尝试:

using UnityEngine;
using System.Collections;

public class TeleportLegacyUI : Teleport {

private CardboardHead head;
private float gazeTotalTime = 0;

void Awake() 
{
    head = Camera.main.GetComponent<StereoController>().Head;
    CardboardOnGUI.IsGUIVisible = true;
    CardboardOnGUI.onGUICallback += this.OnGUI;
}

void Update() {
    RaycastHit hit;
    bool isLookedAt = GetComponent<Collider>().Raycast(head.Gaze, out hit, Mathf.Infinity);
    SetGazedAt(isLookedAt);

    if(isLookedAt)
        gazeTotalTime += Time.deltaTime;
    else
        gazeTotalTime = 0;

    if(gazeTotalTime > 1.0f)
        TeleportRandomly();
}

void OnGUI() {
    if (!CardboardOnGUI.OKToDraw(this)) {
        return;
    }
    if (GUI.Button(new Rect(50, 50, 200, 50), "Reset")) {
        Reset();
    }
    if (GUI.Button(new Rect(50, 110, 200, 50), "Recenter")) {
        Cardboard.SDK.Recenter();
    }
    if (GUI.Button(new Rect(50, 170, 200, 50), "VR Mode")) {
        ToggleVRMode();
    }
}

void OnDestroy() {
    CardboardOnGUI.onGUICallback -= this.OnGUI;
}
}