I wrote a script that allows the user to teleport between two spots by looking at designated objects. Specifically, it uses a ray to check whether one of the two objects is in the center of the screen. It works with the standard FPSController (placed under Main Camera) and mouse controls. When I enable VR support, the detection stops working (not just for the center point, but for the whole display). What should I add to my script to make it work with Oculus the same way it works with the mouse?
This code works by detecting objects by name and then teleporting to assigned objects by tag:
using UnityEngine;
using System.Collections;
using UnityEngine.VR;
public class lookTeleport : MonoBehaviour
{
public GameObject destination;
public Transform target;
RaycastHit hit;
Ray ray;
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update ()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition); //mouse control
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.name == "teleporterA")
{
//Debug.Log("teleporter A detected");
destination = GameObject.FindWithTag("teleportY");
transform.position = destination.transform.position;
}
if (hit.collider.gameObject.name == "teleporterB")
{
//Debug.Log("teleporter B detected");
destination = GameObject.FindWithTag("teleportX");
transform.position = destination.transform.position;
}
}
}
}
I think I will have to look into replacing Input.MousePosition with the VR equivalent.