0
votes

I'm trying to implement a feature similar to HTC Vive's controller with the leap motion on my Unity project. I wanted to generate a laser pointer from the index finger and teleport the Vive's room on the position of the laser (as it's done with the controller). The problem is the latest leap motion (orion) documentation, it's unclear. Any ideas how to do that? More in general, we thought about using HandController but we don't understand where to add the script component. Thanks!

1

1 Answers

1
votes

It's unclear to me whether the problem you're having is getting hand data in your scene at all, or using that hand data.

If you're just trying to get hand data in your scene, you can copy a prefab from one of the Unity SDK's example scenes. If you're trying to integrate Leap into an existing scene that already has a VR rig set up, check out the documentation on the core Leap components to understand what pieces need to be in place for you to start getting Hand data. LeapServiceProvider has to be somewhere in your scene to receive hand data.

As long as you have a LeapServiceProvider somewhere, you can access hands from the Leap Motion from any script, anywhere. So for getting a ray from the index fingertip, just pop this script any old place:

using Leap;
using Leap.Unity;
using UnityEngine;

public class IndexRay : MonoBehaviour {
  void Update() {
    Hand rightHand = Hands.Right;
    Vector3 indexTipPosition = rightHand.Fingers[1].TipPosition.ToVector3();
    Vector3 indexTipDirection = rightHand.Fingers[1].bones[3].Direction.ToVector3();
    // You can try using other bones in the index finger for direction as well;
    // bones[3] is the last bone; bones[1] is the bone extending from the knuckle;
    // bones[0] is the index metacarpal bone.

    Debug.DrawRay(indexTipPosition, indexTipDirection, Color.cyan);
  }
}

For what it's worth, the index fingertip direction is probably not going to be stable enough to do what you want. A more reliable strategy is to cast a line from the camera (or a theoretical "shoulder position" at a constant offset from the camera) through the index knuckle bone of the hand:

using Leap;
using Leap.Unity;
using UnityEngine;

public class ProjectiveRay : MonoBehaviour {

  // To find an approximate shoulder, let's try 12 cm right, 15 cm down, and 4 cm back relative to the camera.
  [Tooltip("An approximation for the shoulder position relative to the VR camera in the camera's (non-scaled) local space.")]
  public Vector3 cameraShoulderOffset = new Vector3(0.12F, -0.15F, -0.04F);

  public Transform shoulderTransform;

  void Update() {
    Hand rightHand = Hands.Right;
    Vector3 cameraPosition = Camera.main.transform.position;
    Vector3 shoulderPosition = cameraPosition + Camera.main.transform.rotation * cameraShoulderOffset;

    Vector3 indexKnucklePosition = rightHand.Fingers[1].bones[1].PrevJoint.ToVector3();
    Vector3 dirFromShoulder = (indexKnucklePosition - shoulderPosition).normalized;

    Debug.DrawRay(indexKnucklePosition, dirFromShoulder, Color.white);

    Debug.DrawLine(shoulderPosition, indexKnucklePosition, Color.red);
  }
}