Leap Motion gesture recognition in Unity3D always returns TYPEINVALID and STATEINVALID

StackOverflow https://stackoverflow.com/questions/18292149

  •  24-06-2022
  •  | 
  •  

Pergunta

Tried enabling any/all gestures with Controller.EnableGesture, tried getting from current frame > gesture(0) or from new CircleGesture (or any other type). Always getting invalid gestures... Here's the code, I'm using LeapInput static class from their Unity example (all it does is saves controller and updates the current frame).

 void Update()
    {
        LeapInput.Update();
        Frame thisFrame = LeapInput.Frame;
        for (int i = 0; i < thisFrame.Gestures().Count; i++)
            {
                Gesture gesture = thisFrame.Gesture(i);
            }
    }

P.s. yes, I'm enabling gestures in the same controller instance.

Foi útil?

Solução

Do you have the most up to date drivers? (1.0.7+7648 as of this post) Here is your code in my handler class, it returns the circle gesture just fine:

Updated Code (Working).

I wasn't paying attention to the OP's original code. .gestures(0) won't work, just change to .gestures[0], see example below.

using UnityEngine;
using System.Collections;
using Leap;

public class LeapTest : Leap.Listener {
    public Leap.Controller Controller;

    // Use this for initialization
    public void Start () {
        Controller = new Leap.Controller(this);
        Debug.Log("Leap start");
    }

    public override void OnConnect(Controller controller){
        Debug.Log("Leap Connected");
        controller.EnableGesture(Gesture.GestureType.TYPECIRCLE,true);
    }

    public override void OnFrame(Controller controller)
    {
        Frame frame = controller.Frame();
        GestureList gestures = frame.Gestures();
        for (int i = 0; i < gestures.Count; i++)
        {
            Gesture gesture = gestures[0];
            switch(gesture.Type){
                case Gesture.GestureType.TYPECIRCLE:
                    Debug.Log("Circle");
                    break;
                default:
                    Debug.Log("Bad gesture type");
                    break;
            }
        }
    }
}

Outras dicas

I'm going to add an answer as well, since I encountered difficulty with this. It looks like you solved your particular problem but mine was a little different. I wasn't receiving any data at all in the Gestures arrays.

I found that in addition to enabling the gestures I also had to look back in the frame history to see if any gestures had been attached to old frames after the fact. In other words, controller.Frame() didn't necessarily have any gesture data associated with it but skipped frames, or the previous polled frame did have the gesture data. So something like this worked out (though bear in mind you'll have to do some long-int type casting):

Frame lastFrame;
Frame thisFrame;

void Update()
{
    LeapInput.Update();
    lastFrame = thisFrame;
    thisFrame = LeapInput.Frame;

    long difference = thisFrame.Id - lastFrame.Id;

    while(difference > 0){
      Frame f = controller.Frame(difference);
      GestureList gestures = f.Gestures();
      if(gestures.Count > 0){
        foreach(Gesture gesture in gestures){
          Debug.Log(gesture.Type + " found in frame " + f.Id);
        }
      }
      difference--;
    }



}

Once you find those gestures in all your skipped frames, or your historical frames, do with them as you wish.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top