Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Teleport.cs
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Collider))]
public class Teleport : MonoBehaviour {
private CardboardHead head;
private Vector3 startingPosition;
private float delay = 0.0f;
void Start() {
head = Camera.main.GetComponent<StereoController>().Head;
startingPosition = transform.localPosition;
}
void Update() {
RaycastHit hit;
bool isLookedAt = GetComponent<Collider>().Raycast(head.Gaze, out hit, Mathf.Infinity);
GetComponent<Renderer>().material.color = isLookedAt ? Color.green : Color.red;
if (!isLookedAt) { delay = Time.time + 2.0f; }
if ((Cardboard.SDK.CardboardTriggered && isLookedAt) || (isLookedAt && Time.time>delay)) {
// Teleport randomly
Vector3 direction = Random.onUnitSphere;
direction.y = Mathf.Clamp(direction.y, 0.5f, 1f);
float distance = 2 * Random.value + 1.5f;
transform.localPosition = direction * distance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.