Grab Detector

This guide explains how to use the Grab Detector class to detect grab gestures in your Unity project. Just add one to the scene and use the events as triggers in your own code.

The Grab Detector calculates a grab value based on the grab strength of the hand, using hysteresis for different grab and ungrab thresholds.


Using the Grab Detector

The Ultraleap Plugin provides the GrabDetector class, which can be used to detect grab gestures based on the grab strength of the hand.

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

GrabDetector Class Overview

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

Setting Up the Grab Detector

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

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

  2. Configure the GrabDetector properties in the Inspector:
    • Set the activateStrength and deactivateStrength values to appropriate thresholds for your application.

    • Assign a Leap Provider and chirality

  3. Implement the grab detection logic in your script:

using UnityEngine;
using Ultraleap;

public class GrabExample : MonoBehaviour
{
    public GrabDetector grabDetector;

    private void Update()
    {
        if (grabDetector.IsGrabbing)
        {
            // Handle grab logic here
            Debug.Log("Grabbing.");
        }

        if (grabDetector.GrabStartedThisFrame)
        {
            // Handle grab start logic here
            Debug.Log("Grab started this frame.");
        }
    }
}
  1. Subscribe to Grab Events: You can subscribe to the grab events (OnGrabStart, OnGrabEnd, and OnGrabbing) to execute custom logic.

private void OnEnable()
{
    grabDetector.OnGrabStart += HandleGrabStart;
    grabDetector.OnGrabEnd += HandleGrabEnd;
    grabDetector.OnGrabbing += HandleGrabbing;
}

private void OnDisable()
{
    grabDetector.OnGrabStart -= HandleGrabStart;
    grabDetector.OnGrabEnd -= HandleGrabEnd;
    grabDetector.OnGrabbing -= HandleGrabbing;
}

private void HandleGrabStart(Hand hand)
{
    Debug.Log("Grab started.");
}

private void HandleGrabEnd(Hand hand)
{
    Debug.Log("Grab ended.");
}

private void HandleGrabbing(Hand hand)
{
    Debug.Log("Grabbing...");
}