Skip to content

Instantly share code, notes, and snippets.

@neogeek
Last active September 14, 2022 07:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neogeek/f463d1f4d6d4fb85ad497c128fca1ca7 to your computer and use it in GitHub Desktop.
Save neogeek/f463d1f4d6d4fb85ad497c128fca1ca7 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
@01GOD
Copy link

01GOD commented Sep 14, 2022

Thanks! That step by step guide is the best documentation I have found on setting up grabbing with the controllers and that SDK. Tried the script, but it needs to be updated. By the way, setting the TrackingOriginType on the Camera Rig to Floor makes the auto-height calibration function properly on that SDK. That way the capsule collider doesn't need to be scaled up.

Thanks in advance if posting an update to that script. I would say "should work at Oculus", but that company is sadly gone...not going to say "should work at farsebook" to anybody I like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment