Search Unity

  1. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

[VRTK] How would you go creating a joint between two grabbed objects?

Discussion in 'AR/VR (XR) Discussion' started by kunjalkiran, Sep 12, 2017.

  1. kunjalkiran

    kunjalkiran

    Joined:
    Jan 14, 2016
    Posts:
    12
    I have a scene with two interactable spheres.

    Using both controllers I can grab those sphere and put them close together.

    I would now like to create a joint to attach the spheres together, but I am a bit stuck as to how I would do that... Especially how I can get to the grabbed objects (get their name, position etc...).

    I searched through VRTK documentation, but I have not found anything that looks like "get the Gameobject that is currently being grabbed".

    VRTK users, how would you do Something like that?

    Thanks.
     
  2. kunjalkiran

    kunjalkiran

    Joined:
    Jan 14, 2016
    Posts:
    12
    Here is the solution I came up with for anyone interested.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using VRTK;
    6.  
    7. public class JoinObjects : MonoBehaviour {
    8.     public GameObject GO1, GO2;
    9.     public GameObject ControllerL;
    10.     public GameObject ControllerR;
    11.     public GameObject GO;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         GetComponent<VRTK_InteractableObject>().InteractableObjectGrabbed += new InteractableObjectEventHandler(ObjectGrabbed);
    17.         GetComponent<VRTK_InteractGrab>().GetGrabbedObject();
    18.     }
    19.     private void ObjectGrabbed(object sender, InteractableObjectEventArgs e)
    20.     {
    21.         Debug.Log("Im Grabbed");
    22.     }
    23.     public void Click()
    24.     {
    25.         ControllerL = VRTK_DeviceFinder.GetControllerLeftHand();
    26.         ControllerR = VRTK_DeviceFinder.GetControllerRightHand();
    27.         GO1 = ControllerL.GetComponent<VRTK_InteractGrab>().GetGrabbedObject();
    28.         GO2 = ControllerR.GetComponent<VRTK_InteractGrab>().GetGrabbedObject();
    29.         if (GO1 != null)
    30.         {
    31.             Debug.Log(GO1.name);
    32.         }
    33.         if (GO2 != null)
    34.         {
    35.             Debug.Log(GO2.name);
    36.         }
    37.         ConfigurableJoint CJoint;
    38.         CJoint = GO1.AddComponent<ConfigurableJoint>();
    39.         CJoint.connectedBody = GO2.GetComponent<Rigidbody>();
    40.         //CJoint.angularXMotion = ConfigurableJointMotion.Locked;
    41.         //CJoint.angularYMotion = ConfigurableJointMotion.Locked;
    42.         //CJoint.angularZMotion = ConfigurableJointMotion.Locked;
    43.         CJoint.xMotion = ConfigurableJointMotion.Locked;
    44.         CJoint.yMotion = ConfigurableJointMotion.Locked;
    45.         CJoint.zMotion = ConfigurableJointMotion.Locked;
    46.     }
    47. }
    48.  
     
unityunity