0

我想将两个 Grabbed 对象连接在一起。

为此,我需要知道正在被操纵的游戏对象,以便我可以添加一个关节组件。

目前我遇到的最大问题是获取被抓取的游戏对象。

我尝试使用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

我还缺少什么吗?

4

1 回答 1

0

这行得通。

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;
}

}

于 2017-09-27T07:43:13.833 回答