Skip to content

Instantly share code, notes, and snippets.

@markcastle
Last active January 12, 2018 22:22
Show Gist options
  • Save markcastle/d9228f7a5967aeb095ec689453a938c0 to your computer and use it in GitHub Desktop.
Save markcastle/d9228f7a5967aeb095ec689453a938c0 to your computer and use it in GitHub Desktop.
Very simple Unity class for triggering Hmd recentering for Cardboard, GearVR etc.
/*
Copyright 2017 Captive Reality Ltd
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is
hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Source:
Description: https://gist.github.com/markcastle/d9228f7a5967aeb095ec689453a938c0
Very simple class for Recentering the Cardboard, GearVR etc.
Usage: You'll need the EventSystem all working prior to using this.
Add a gameobject under the Camera hierachy and attach the this script at position 0, 0, 0 and rename it RecenterOverlay.
Now add another gameobject under that at position 0, 0, 0 and call it RecenterContainer
Under that add another gameobject and a Text object at location 0, 0, 0.5 (eg 0.5m away from the camera in the middle of the users gaze).
You're going to need to size the text correctly for your project. I'm using
X: 0 Y: 0 Z: 0.521
Width: 160 Height: 30
Scale: X: 0.001 Y: 0.001 z: 0.001
Font, Arial 12
Text is Centered Horizontally and Vertically.
This is how it looks...
MainScene
+-- CameraHolder
+-- Main Camera
+-- RecenterOverlay
+-- RecenterContainer
+-- Text
+-- Quad
In the inspector, click RecenterOverlay and then hook RecenterContainer to the "Recenter Container" item.
Also in the inspector set RecenterOverlay to inactive.
Give the text some text at loc like: "Tap again to recenter view" and add an event trigger...
Pointer Down --> Drag the RecenterOverlay object into the target and select "Recenter.RecenterVR" as the trigger.
You can also repeat the process with a quad of some sort behind the text..
I prefer to have a semi transparent black rect. Then add the same trigger to that.
You're going to need something to trigger StartRecentering(). I either put a button on a Gui of some
sort or you can call it when a button on the GearVR is pressed or something.
So what happens then is.. User either clicks a button or presses a button to invoke StartRecentering()
This sets RecenterOverlay to active which displays the RecenterContainer object which in turn shows the "Tap to Recenter" text and a quad or whatever.
These sit in front of the gaze of the user wherever they look and the Pointer Down Event trigger on them is the only thing that will fire.
So the user is forced to do a pointer down, which then fires the RecenterVR method which simply calls Unity's XR recentering code.
*/
using UnityEngine;
public class Recenter : MonoBehaviour {
public bool isRecentering = false;
public GameObject recenterContainer;
private void Start()
{
isRecentering = false;
recenterContainer.gameObject.SetActive(false);
}
public void StartRecentering()
{
isRecentering = true;
recenterContainer.gameObject.SetActive(true);
}
public void RecenterVR()
{
isRecentering = false;
recenterContainer.gameObject.SetActive(false);
UnityEngine.XR.InputTracking.Recenter();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment