-1

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.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Rez
  • 187
  • 12

1 Answers1

0

You should replace ray = Camera.main.ScreenPointToRay(Input.mousePosition);
with ray = Camera.main.ScreenPointToRay(new Vector2(Screen.height / 2, Screen.width / 2));
Since you are in VR, and not using a mouse, the Input.mousePosition can't be used as a screen-point to cast the ray from, instead you could use the middle of the screen that the user sees (like crosshairs)

Hristo
  • 1,805
  • 12
  • 21