When I start a new VR project in Unity 5 I always map the Recenter function to both the ‘R’ key and the XBox Controller Right Joystick button (which is the stick for rotating the user). It does not need the Oculus Utilities SDK.
It’s quick and easy.
1. Add a new Input item for pressing a key or joystick button named “Recenter”
Edit -> Project Settings -> Input, and insert a new item and name it “Recenter”:
Set the following:
Positive Button: “r”
Alt Positive Button: “joystick button 9″
Type: Key or mouse button
2. Add a script to detect “Recenter” being pressed and call the VR Tracker Recenter function.
Here’s the code:
VRRecenter.cs:
// Recenter input mapping for VR by Peter Koch <peterept@gmail.com> using UnityEngine; using UnityEngine.VR; using System.Collections; // To use: // 1. Drag this script onto any game object // 2. Map "Recenter" in the input manager to use XBox Right Joystick Button and 'r' key: // Ref: http://blogs.msdn.com/b/nathalievangelist/archive/2014/12/16/joystick-input-in-unity-using-xbox360-controller.aspx // Edit -> Project Settings -> Input, and insert a new item and name it "Recenter": // Positive Button: "r" // Alt Positive Button: "joystick button 9" // Type: Key or mouse button public class VRRecenter : MonoBehaviour { void Update () { if (Input.GetButtonDown("Recenter")) { Debug.Log("VR Recenter"); InputTracking.Recenter(); } } }
June 10, 2016 at 12:07 pm
Hello there, this code recenter only rotation, but I am wondering how about position? In my project every time it begins, the position of the head is distant from the original position. Do you have any solutions?
June 18, 2016 at 9:45 am
That code will recenter both position and orientation with respect to your head (VR camera) and your parent game object. The actual position within a scene is set using standard Transform position. Does that make sense?