Search code examples
c#unity-game-engineleap-motion

How to grab and move gameObjects in unity with leap motion


I am making a project with unity and leap motion, but I'm new with leap. I'm trying to move gameObjects but when I add the property of rigidBody to it and touch it with the handmodels it flies away, how do I avoid this?


Solution

  • Because the default physics hands Leap provides in its core assets are interpreted as "kinematic," they basically have infinite mass. Objects are flying away when you try to grab or touch them because the underlying physics solver is trying to figure out what happens when you crush a perfectly rigid body (hence "Rigidbody") between two other rigidbodies of infinite mass... The solver winds up with huge velocities and as a result you get behavior that looks weird and feels bad.

    There's some fancy math (Kabsch solving) and real-time physics (soft, non-rigid contact behavior) involved in make touching and grasping objects feel good, so you're probably just going to want to download Leap's Interaction Engine, which does all of that for you!

    Make sure you have the latest "Core Assets" from the main developer resource page, and you can find the "Interaction Engine" on that same page.

    Download those packages and import them into Unity, then your workflow will basically work thusly:

    • Find the InteractionManager prefab in the InteractionEngine folder, and drag it into your scene. This object just does necessary bookkeeping for the interaction system.
    • Remove those RigidRoundHands you were using from the Hand Controller, and add the Interaction Brush Hands in the InteractionEngine folder instead. These hands have extra physics logic baked in that prevents interactions from being explosive the way you were describing.
    • Add an InteractionBehaviour component to any object you would like to be able to touch or pick up.

    From there, you should be good to go. If something stops working at any step, you can check out the Interaction Engine 101 example on the developer page; it's an open-source Unity project with a working implementation of the latest Interaction Engine.