Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HoloToolkit Motion Controllers + GrabMechanics

Discussion in 'VR' started by phoberman, Nov 11, 2018.

  1. phoberman

    phoberman

    Joined:
    Jan 16, 2008
    Posts:
    74
    We’ve been trying to implement the behavior of the motion controllers in the GrabMechanics scene (in HoloToolkit/Examples//MotionControllers-GrabMechanics/Scenes) but maintaining the standard (animated, realistic) controller models (not the ControllerCube models that are used in the GrabMechanics scene). But since the standard controller models are apparently instantiated via a call to an API, there's no corresponding prefab that can be accessed (and altered) in Assets. So we've taken the approach of running the scene, waiting for the controllers to appear, finding them, and then adding components via script.

    The ControllerCube prefabs have some components that aren’t included in the standard controller model: specifically a Box Collider, a Rigidbody, and a Grabber.cs script. We’ve figured out a way to add these three components via a simple script (first by finding the controller once the scene is running with
    rightController=GameObject.Find(“RightController”); 
    We can then add the components, and set the necessary parameters for the Box Collider (
    isTrigger = true
    ) and the RigidBody (
    isKinematic = true, useGravity = false
    ).

    However, we can’t figure out how to set the two necessary parameters for the Grabber (Handedness and Press Type). If we run the scene and set these two Grabber parameters in the Inspector, everything works as expected, but we need to set them in our script, and we can’t figure out the correct syntax; everything we try gives us errors.

    We have no idea if this is the best approach; this is all very ad hoc and we’re doing the best we can. We’d appreciate any advice on how to get this to work, or tips on other approaches.

    Here’s our current script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using HoloToolkit.Unity;
    6.  
    7. namespace HoloToolkit.Unity.InputModule.Examples.Grabbables {
    8.  
    9.     public class AddGrabber : MonoBehaviour {
    10.  
    11.         GameObject rightController;
    12.         bool initialized = false;
    13.  
    14.         void Update() {
    15.          
    16.             if (rightController == null) {
    17.              
    18.                 rightController = GameObject.Find("RightController");
    19.              
    20.             } else if (!initialized){
    21.              
    22.                 BoxCollider boxCollider = rightController.AddComponent<BoxCollider>();
    23.                 boxCollider.isTrigger = true;
    24.              
    25.                 Rigidbody rigidBody = rightController.AddComponent<Rigidbody>();
    26.                 rigidBody.isKinematic = true;
    27.                 rigidBody.useGravity = false;
    28.              
    29.                 Grabber grabber = rightController.AddComponent<Grabber>();
    30.                 Handedness h = Handedness.Right; // no error, but no effect
    31.                 //grabber.Handedness = Handedness.Right; // error
    32.                 //InteractionSourcePressType p = InteractionSourcePressType.Grasp; // error
    33.              
    34.                 initialized = true;
    35.             }
    36.         }
    37.     }
    38. }