Search code examples
unity-game-enginemrtk

How to move an object with Motion Controller


Using MRTK V2 and c# scripting, I need to grab and move an object (say a cube) around the camera, at the same distance as it was grabbed and with the cube facing the camera initially to remain perpendicular to the camera. I don't know if I'm clear...

I have not found some "grab" example is the MRTK v2 as there was in the original MRTK, so I'm wondering how to program this actually. I want to affect the transform of the object, not only use the "interactable" script.

I've been able to read the transform of the controller at the beginning and the end of the movement, but I'm wondering how to affect the transform of the target object so it remains at a fixed distance from the controller and stays perpendicular to the controller (face selected still facing the camera).

I've tried affecting the transform of the target object and looking for the right calculation, but without success.

Any idea where I could start looking?


Solution

  • You can add a ManipulationHandler to the object you want to move. Here are some sample steps:

    1. Add a Box Collider to the object you want to move.
    2. Add a ManipulationHandler your object
    3. To make the object maintain its original rotation relative to you, set "One Hand Rotation Mode Far" to "Maintain Rotation to User".
    4. If you want to also be able to grab the object directly (this is for VR and HoloLens 2), then add a NearInteractionGrabbable component to your object.

    Here is an example configuration that I have to make a cube movable:

    enter image description here

    For an example of how a ManipulationHandler is used, see the Lunar Module or Cheese objects in the HandInteractionExamples scene at Assets\MixedRealityToolkit.Examples\Demos\HandTracking\Scenes\HandInteractionExamples.unity