Unity Scripting Reference¶
The Leap Motion Unity scripts take the hand tracking data from the Leap Motion device and allow your hands to interact with a virtual scene.
Leap.Unity Namespace
The classes in the Leap.Unity namespace interact directly with GameObjects and other UnityEngine components. These scripts take tracking data and put it to use in Unity. The classes and objects you are most likely to use directly are:
- LeapServiceProvider - provides access to the tracking data and images as well as access to the Leap.|Controller|_ object. The LeapServiceProvider transforms the tracking data so that the coordinates are relative to the transform of the Unity GameObject to which it is attached. For example, if you rotate the GameObject, the hands are also rotated in the Unity scene. Likewise, a hand 200mm above the Leap Motion device is placed 200mm above the GameObject in the Unity scene. The LeapServiceProvider also converts the coordinate system from the right-handed convention used by the Leap Motion software to the left-handed convention used by Unity.
- IHandModel, HandModel, or one of its subclasses like CapsuleHand - all the hands included in the asset packages are based on these. If you create your own hands you must implement IHandModel or extend HandModel.
- LeapHandController - provides access to the active hand graphics and physics objects.
Leap.Unity Module Namespaces
Modules that work in concert with the core assets have their own namespaces. Modules include:
- Hand Module - Additional hand models and scripts.
- UI Input module - Aids for creating interactable UIs.
Note: The earlier PinchUtilities module has been superceded. The main LeapPinchUtility class has been rolled into the core assets (as |PinchUtility|_). The example scenes and scripts are now in the DetectionExamples module.
Leap Namespace
Classes in the Leap namespace define the Leap Motion tracking objects:
- A Frame is a set of data recorded at a specific moment in time. A Frame contains Hand objects
- A Hand object represents a tracked hand. A Hand (always) has five Finger objects and properties like Direction, PalmPosition, and Basis (orientation).
- A Finger object represents a tracked finger or thumb of a hand. A Finger has four bones.
- A Bone object represents a segment of a tracked finger and provides position, size, and orientation data.
- An Arm object is a specialized bone that tracks the forearm.
Scripts in the Leap namespace are not specific to Unity and can be used in any C# application. When using these classes in Unity there is an important difference: you should always get Frame objects from a LeapProvider — not from a Controller object. The LeapProvider converts the coordinates from Leap Motion space to Unity space and transforms them so that the hands appear in Unity relative to the GameObject to which the LeapProvider is attached (typically the LeapHandController). If you get a Frame directly from a Controller, you must handle the conversion yourself.
The Leap Motion and Unity APIs define their own classes for vectors and transforms. You can convert from these Leap Motion classes to their Unity equivalents as shown in the following examples:
Vector3 unityVector = leapVector.ToVector3();
Quaternion unityRotation = leapMatrix.Rotation();
Vector3 unityTranslation = leapMatrix.origin.ToVector3();
Vector3 unityScale = new Vector3(leapMatrix.xBasis.Magnitude,
leapMatrix.yBasis.Magnitude,
leapMatrix.zBasis.Magnitude);