Using leapmotion to control Unity3d interface

785 views Asked by At

I understand that I can use leapmotion in game with Unity3D.

What I can't see any information on, is if I can use it to actual interact with assets, models etc as I build the game. For example revolving a game object around the x axis or zooming in or out of the view.

Is this possible?

1

There are 1 answers

0
Charles Ward On BEST ANSWER

Yes, it is possible, but requires some scripts that nobody has written yet (ASFAIK). Here is a VERY rough example that I worked up today since I've been curious about this question, too.

All it does is move, scale, and rotate a selected game object -- it doesn't try to do this in a good way -- it is a proof of concept only. To make it work you would have to do a sensible conversion of Leap coordinates and rotations to Unity values. To try it, put this script in a folder called "Editor", select a game object in the scene view and hold a key down while moving your hand above your Leap. As I said, none of these movements really work to edit an object, but you can see that it is possible with some sensible logic.

@CustomEditor (Transform)
class RotationHandleJS extends Editor {
    var controller = new Leap.Controller();
    var position;
    var localScale;
    var localRotation;
    var active = false;
    function OnSceneGUI () {
        e = Event.current;
        switch (e.type) {
            case EventType.KeyDown:
                position = target.transform.position;
                localScale = target.transform.localScale;
                localRotation = target.transform.localRotation;
                active = true;
                Debug.Log("editing");
                break;
            case EventType.KeyUp:
                active = false;
                target.transform.position = position;
                target.transform.localScale = localScale;
                EditorUtility.SetDirty (target);
                break;
        } 
        if(active){
            frame = controller.Frame();
            ten = controller.Frame(10);
            scale = frame.ScaleFactor(ten);
            translate = frame.Translation(ten);
            target.transform.localScale = localScale + new Vector3(scale, scale, scale);
            target.transform.position = position + new Vector3(translate.x, translate.y, translate.z);
            leapRot = frame.RotationMatrix(ten);
            quats = convertRotation(leapRot);
            target.transform.localRotation = quats;
        }
    }

    var LEAP_UP = new Leap.Vector(0, 1, 0);
    var LEAP_FORWARD = new Leap.Vector(0, 0, -1);
    var LEAP_ORIGIN = new Leap.Vector(0, 0, 0);

    function convertRotation(matrix:Leap.Matrix) {
      var up = matrix.TransformDirection(LEAP_UP);
      var forward = matrix.TransformDirection(LEAP_FORWARD);

      return Quaternion.LookRotation(new Vector3(forward.x, forward.y,forward.z), new Vector3(up.x, up.y, up.z));
    }
}