Skip to content

Instantly share code, notes, and snippets.

@01GOD
Forked from neogeek/OculusSDKAutomation.cs
Created September 14, 2022 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 01GOD/af8619df965c23b079585387dda76813 to your computer and use it in GitHub Desktop.
Save 01GOD/af8619df965c23b079585387dda76813 to your computer and use it in GitHub Desktop.
Oculus SDK Unity Setup

Oculus SDK Unity Setup

OVRPlayerController

Setup:

  • Add OVRPlayerController prefab to scene.
  • Add the Character Controller Constraint component to the OVRPlayerController game object.
  • Attach the camera from the OVRPlayerController game object to the Character Controller Constraint component.
  • Check Dynamic Height on the Character Controller Constraint component.
  • Change Tracking Origin on the OVRCameraRig to Floor Level.
  • Move OVRPlayerController to a position where it won't fall through the floor.
  • Change the radius of the Character Controller on OVRPlayerController to 0.25 so you can walk closer to objects before bumping into them.

OVRControllerPrefab

Setup:

  • Add OVRControllerPrefab to both LeftControllerAnchor and RightControllerAnchor game objects in the OVRCameraRig.
  • Change the Controller dropdown on each controller to L Touch and R Touch respectively.

OVRGrabber

Setup

  • Add the OVRGrabber component to both OVRControllerPrefab game objects.
  • Disable Use Gravity and enable Kinematic on the Rigidbody component that was added with the OVRGrabber component.
  • Check Parent Held Object to make the object grabbed a child of the controller. This prevents having to update the position of the object to match the controller.
  • Set the Grip Transfrom to the game object the component is attached to.
  • Add a Sphere Collider with a size of 0.075, set Is Trigger to true, and add it to the Grab Volumes array.

OVRGrabbable

Setup

  • Add the OVRGrabbable component to anything you want to be grabbable.
  • Also, attach a Rigidbody if you want the grabbable object to react to physics after it has been released.
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
public static class OculusSDKAutomation
{
private static readonly GameObject playerControllerPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Oculus/VR/Prefabs/OVRPlayerController.prefab");
private static readonly GameObject controllerPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Oculus/VR/Prefabs/OVRControllerPrefab.prefab");
private const string leftControllerAnchorPath = "OVRCameraRig/TrackingSpace/LeftHandAnchor/LeftControllerAnchor";
private const string rightControllerAnchorPath = "OVRCameraRig/TrackingSpace/RightHandAnchor/RightControllerAnchor";
[MenuItem("Oculus/Custom/Setup Player Controller")]
private static void SetupPlayerController()
{
var playerController = PrefabUtility.InstantiatePrefab(playerControllerPrefab) as GameObject;
var characterController = playerController.GetComponent<CharacterController>();
var ovrCameraRig = playerController.GetComponentInChildren<OVRCameraRig>();
var ovrManager = playerController.GetComponentInChildren<OVRManager>();
characterController.radius = 0.25f;
ovrManager.trackingOriginType = OVRManager.TrackingOrigin.FloorLevel;
var characterCameraConstraint = playerController.AddComponent<CharacterCameraConstraint>();
characterCameraConstraint.CameraRig = ovrCameraRig;
characterCameraConstraint.DynamicHeight = true;
var leftControllerAnchor = playerController.transform.Find(leftControllerAnchorPath);
var leftController = PrefabUtility.InstantiatePrefab(controllerPrefab, leftControllerAnchor) as GameObject;
leftController.GetComponent<OVRControllerHelper>().m_controller = OVRInput.Controller.LTouch;
var leftControllerRigidbody = leftController.AddComponent<Rigidbody>();
var leftControllerSphereCollider = leftController.AddComponent<SphereCollider>();
var leftOVRGrabber = leftController.AddComponent<OVRGrabber>();
leftControllerRigidbody.useGravity = false;
leftControllerRigidbody.isKinematic = true;
leftControllerSphereCollider.isTrigger = true;
leftControllerSphereCollider.radius = 0.075f;
var rightControllerAnchor = playerController.transform.Find(rightControllerAnchorPath);
var rightController = PrefabUtility.InstantiatePrefab(controllerPrefab, rightControllerAnchor) as GameObject;
rightController.GetComponent<OVRControllerHelper>().m_controller = OVRInput.Controller.RTouch;
var rightControllerRigidbody = rightController.AddComponent<Rigidbody>();
var rightControllerSphereCollider = rightController.AddComponent<SphereCollider>();
var rightOVRGrabber = rightController.AddComponent<OVRGrabber>();
rightControllerRigidbody.useGravity = false;
rightControllerRigidbody.isKinematic = true;
rightControllerSphereCollider.isTrigger = true;
rightControllerSphereCollider.radius = 0.075f;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment