Skip to content

Instantly share code, notes, and snippets.

@JamesHagerman
Created November 4, 2015 23:26
Show Gist options
  • Save JamesHagerman/03bae8e405c6baa4b11e to your computer and use it in GitHub Desktop.
Save JamesHagerman/03bae8e405c6baa4b11e to your computer and use it in GitHub Desktop.
Leap Motion + Unity 5 + Oculus Hand debugging script
using UnityEngine;
using Leap;
public class HandDebug : MonoBehaviour {
// Attach this script to any game object in your scene. I have an empty GameObject named "GlobalScripts"
// that I attach these kinds of "global" scripts to, but anywhere should work.
//
// Once this script is in your scene, select the game object the script is attached to in Unity's
// Inspector panel (the panel that's usually on the right hand side of Unity's main window). When you
// see "Oculus Rig" and a box that reads "None (GameObject)", you'll be looking at the right place.
//
// Now, assuming you've already got a LMHeadMountedRig object in your scene, you should be able to click
// on it in the Hierarchy panel on the left hand side of Unity and drag-and-drop it into the box named
// "Oculus Rig"
//
// Now, click on the "Game" tab, find the "Gizmos" drop down at the top of the Game tab, and click on it
// to enable Gizmos in the game view.
//
// Run your scene, put your HMD on, and wave your hands around in front of your face.
//
// You should see a bunch of rays coming out of your hands!
//
// Oh, and select "VR_WORLD_AR_HANDS" from the drop down in the LMHeadMountedRig object while you're
// messing around. Those hands operate a lot more smoothly in some situations...
public GameObject oculusRig;
HandController handController;
// Use this for initialization
void Start () {
handController = oculusRig.transform.GetComponentInChildren<HandController>();
}
// Update is called once per frame
void Update () {
HandModel[] hands = handController.GetAllGraphicsHands();
bool leftValid = false;
bool rightValid = false;
HandModel rightH = null;
HandModel leftH = null;
Vector3 leftP, rightP;
Vector3 leftN, rightN;
foreach (HandModel hand in hands)
{
Hand leapHand = hand.GetLeapHand();
if (leapHand.IsRight)
{
rightH = hand;
rightValid = true;
}
else if (leapHand.IsLeft)
{
leftH = hand;
leftValid = true;
}
}
if (rightValid)
{
rightP = rightH.GetPalmPosition();
rightN = rightH.GetPalmNormal();
Debug.DrawLine(Vector3.zero, rightP, Color.white);
Debug.DrawRay(rightP, rightN, Color.red);
// Draw some finger (index 0):
//Ray derp = rightH.fingers[0].GetRay();
//Debug.DrawRay(derp.origin, derp.direction, Color.yellow);
// Draw all fingers:
foreach (FingerModel finger in rightH.fingers)
{
Ray fingerRay = finger.GetRay();
Debug.DrawRay(fingerRay.origin, fingerRay.direction, Color.yellow);
}
}
if (leftValid)
{
leftP = leftH.GetPalmPosition();
leftN = leftH.GetPalmNormal();
Debug.DrawLine(Vector3.zero, leftP, Color.blue);
Debug.DrawRay(leftP, leftN, Color.green);
// Draw some finger (index 0):
//Ray derp = leftH.fingers[0].GetRay();
//Debug.DrawRay(derp.origin, derp.direction, Color.cyan);
// Draw all fingers:
foreach (FingerModel finger in leftH.fingers)
{
Ray fingerRay = finger.GetRay();
Debug.DrawRay(fingerRay.origin, fingerRay.direction, Color.cyan);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment