2017-09-25 85 views
0

我想將兩個抓取的對象連接在一起。試圖在兩個抓取的對象之間創建聯合

爲此,我需要知道被操縱的gameobjects,所以我可以添加一個聯合組件。

我目前遇到的最大問題是遇到正在被抓取的gameobjects。

我試過使用GetGrabbedObject()但我只得到「null」。

從我有限的瞭解:

private GameObject ControllerL 

ControllerL = VRTK_DeviceFinder.GetControllerLeftHand(); // this should get me the left hand 

GameObject GO; 

GO = ControllerL.GetComponent<VRTK_InteractGrab>().GetGrabbedObject(); // this should get me the gameobject Grabbed by the left hand 

別的我失蹤?

+0

你見過所有的這裏列出的要求:[https://vrtoolkit.readme.io/docs/vrtk_interactgrab](https://vrtoolkit.readme.io/docs/vrtk_interactgrab ) – lockstock

+0

VRTK_ControllerEvents控制器上:檢查 VRTK_InteractTouch控制器上:檢查 可交互對象有isGrabbable設置爲true:檢查 我能搶在遊戲中的目標沒有問題。 我只是無法獲取抓取對象的名稱。試用了一些包含「GetGrabbedObject」的教程中的代碼,因此我開始懷疑它是否真的是我正在尋找的東西...... – wheric

回答

0

This Works。

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using VRTK; 

public class JoinObjects : MonoBehaviour { 

public GameObject GO1, GO2; 
public GameObject ControllerL; 
public GameObject ControllerR; 
public GameObject GO; 


// Use this for initialization 
void Start() 
{ 
    GetComponent<VRTK_InteractableObject>().InteractableObjectGrabbed += new InteractableObjectEventHandler(ObjectGrabbed); 
    GetComponent<VRTK_InteractGrab>().GetGrabbedObject(); 
} 

private void ObjectGrabbed(object sender, InteractableObjectEventArgs e) 
{ 
    Debug.Log("Im Grabbed"); 
} 

public void Click() 
{ 
    Debug.Log("pouet"); 

    ControllerL = VRTK_DeviceFinder.GetControllerLeftHand(); 
    ControllerR = VRTK_DeviceFinder.GetControllerRightHand(); 

    GO1 = ControllerL.GetComponent<VRTK_InteractGrab>().GetGrabbedObject(); 
    GO2 = ControllerR.GetComponent<VRTK_InteractGrab>().GetGrabbedObject(); 

    if (GO1 != null) 
    { 
     Debug.Log(GO1.name); 
    } 

    if (GO2 != null) 
    { 
     Debug.Log(GO2.name); 
    } 

    ConfigurableJoint CJoint; 
    CJoint = GO1.AddComponent<ConfigurableJoint>(); 
    CJoint.connectedBody = GO2.GetComponent<Rigidbody>(); 
    //CJoint.angularXMotion = ConfigurableJointMotion.Locked; 
    //CJoint.angularYMotion = ConfigurableJointMotion.Locked; 
    //CJoint.angularZMotion = ConfigurableJointMotion.Locked; 
    CJoint.xMotion = ConfigurableJointMotion.Locked; 
    CJoint.yMotion = ConfigurableJointMotion.Locked; 
    CJoint.zMotion = ConfigurableJointMotion.Locked; 
} 

}