Pinch Detector

This guide explains how to use the Pinch Detector class to detect pinch gestures in your Unity project. The Pinch Detector is the best way to determine whether the user is pinching or not. Just add one to the scene and use the events as triggers in your own code.

Pinch Detector calculates a pinch value based on the distance between a specified finger tip and the thumb tip, using hysteresis for different pinch and unpinch thresholds.


Using the Pinch Detector

The Ultraleap Plugin provides the PinchDetector class, which can be used to detect pinch gestures based on the distance between the thumb and a specified finger.

This tutorial assumes you have a basic understanding of C# and Unity. If not, please refer to the Scripting Fundamentals before proceeding.

PinchDetector Class Overview

The PinchDetector class is derived from the ActionDetector class and includes the following key features:

Setting Up the Pinch Detector

To use the PinchDetector in your Unity project, follow these steps:

  1. Add the PinchDetector script to a GameObject in your scene.

  2. Configure the PinchDetector properties in the Inspector:
    • Set the fingerType to the desired finger (INDEX, MIDDLE, RING, PINKY).

    • Set the activateDistance and deactivateDistance values to appropriate thresholds for your application.

    • Assign a Leap Provider and chirality

  3. Implement the pinch detection logic in your script:

using UnityEngine;
using Ultraleap;

public class PinchExample : MonoBehaviour
{
    public PinchDetector pinchDetector;

    private void Update()
    {
        if (pinchDetector.IsPinching)
        {
            // Handle pinch logic here
            Debug.Log("Pinching with squish percent: " + pinchDetector.SquishPercent);
        }

        if (pinchDetector.PinchStartedThisFrame)
        {
            // Handle pinch start logic here
            Debug.Log("Pinch started this frame.");
        }
    }
}
  1. Subscribe to Pinch Events: You can subscribe to the pinch events (OnPinchStart, OnPinchEnd, and OnPinching) to execute custom logic.

private void OnEnable()
{
    pinchDetector.OnPinchStart += HandlePinchStart;
    pinchDetector.OnPinchEnd += HandlePinchEnd;
    pinchDetector.OnPinching += HandlePinching;
}

private void OnDisable()
{
    pinchDetector.OnPinchStart -= HandlePinchStart;
    pinchDetector.OnPinchEnd -= HandlePinchEnd;
    pinchDetector.OnPinching -= HandlePinching;
}

private void HandlePinchStart(Hand hand)
{
    Debug.Log("Pinch started.");
}

private void HandlePinchEnd(Hand hand)
{
    Debug.Log("Pinch ended.");
}

private void HandlePinching(Hand hand)
{
    Debug.Log("Pinching...");
}