Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity Oculus将手的位置锁定在一个轴上,同时在抓取物体时允许在其他轴上移动_C#_Unity3d_Constraints_Oculus - Fatal编程技术网

C# Unity Oculus将手的位置锁定在一个轴上,同时在抓取物体时允许在其他轴上移动

C# Unity Oculus将手的位置锁定在一个轴上,同时在抓取物体时允许在其他轴上移动,c#,unity3d,constraints,oculus,C#,Unity3d,Constraints,Oculus,当一个物体被抓取时,我试图将模型的手锁定在一个物体上。当前,我的项目中的对象已锁定,仅允许在抓取时在Y轴和Z轴上移动。我想在抓取物体时锁定玩家手在X轴上的位置,但在释放时返回常规手跟踪。(例如,在VR中抓住门把手,您希望手保持固定在门把手上,直到松开为止) 我试着用手抚摸物体,但我可能做得不对。我已经对OVRGrabber脚本进行了一些修改,以用于我的项目。下面的示例代码正在添加到可以找到的VRMirror脚本中。在固定更新结束时调用lockHands() void lockHands(

当一个物体被抓取时,我试图将模型的手锁定在一个物体上。当前,我的项目中的对象已锁定,仅允许在抓取时在Y轴和Z轴上移动。我想在抓取物体时锁定玩家手在X轴上的位置,但在释放时返回常规手跟踪。(例如,在VR中抓住门把手,您希望手保持固定在门把手上,直到松开为止)

我试着用手抚摸物体,但我可能做得不对。我已经对OVRGrabber脚本进行了一些修改,以用于我的项目。下面的示例代码正在添加到可以找到的VRMirror脚本中。在固定更新结束时调用lockHands()

    void lockHands()
    {
         if(L_GrabbedObject.GetComponent<OVRGrabber>().isGrabbed || R_GrabbedObject.GetComponent<OVRGrabber>().isGrabbed)
         {

            if (mirrorLeft)
            {
                string name = L_GrabbedObject.GetComponent<OVRGrabber>().m_grabbedObj.name;
                Debug.Log(name + " Left");
            }
            else if (mirrorRight)
            {
                var objGrabbed = R_GrabbedObject.GetComponent<OVRGrabber>().m_grabbedObj;
                string name = objGrabbed.name;
                Debug.Log(name + " Right");
            }
        }
    }
void lockHands()
{
if(L|u GrabbedObject.GetComponent().isharghed | | R|u GrabbedObject.GetComponent().isharghed)
{
如果(左后视镜)
{
字符串名称=L_GrabbedObject.GetComponent().m_grabbedObj.name;
Debug.Log(名称+“左”);
}
else if(右后视镜)
{
var objGrabbed=R_GrabbedObject.GetComponent().m_grabbedObj;
字符串名称=objGrabbed.name;
Debug.Log(name+“Right”);
}
}
}
我需要这一部分与双边镜像脚本以及OVRGrabber和OVRCameraRig一起工作

盘子是被抓取的物体。以下是该项目的屏幕截图:

我提出的解决方案使用了OVRCameraRig脚本。我在UpdateAnchors()方法中对该纸条做了大量更改

var mirror=GameObject.Find(“OVRCameraRig”).GetComponent();
var L_GrabbedObject=GameObject.Find(“左手目标”).GetComponent();
var R_GrabbedObject=GameObject.Find(“右手目标”).GetComponent();
//控制器偏移需要这个,因为如果我们在OpenVR上,我们希望设置Unity指定的局部姿势,但是如果我们没有,OVRInput local position是正确的定位
if(OVRManager.loadedXRDevice==OVRManager.XRDevice.OpenVR)
{
Vector3 leftPos=Vector3.0;
Vector3 rightPos=Vector3.0;
四元数leftQuat=四元数.identity;
四元数rightQuat=四元数.identity;
如果(镜像。镜像左侧)
{
if(OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand,NodeStatePropertyType.Position,OVRPlugin.Node.HandLeft,OVRPlugin.Step.Render,out leftPos))
if(L_GrabbedObject.isgrapped)
{
if(L_GrabbedObject.m_grabbedObj.name==“rollingPin”)
{
Vector3 leftHand=leftHandAnchor.localPosition;
leftHand.z=leftPos.z;
leftHandAnchor.localPosition=leftHand;
}
}否则
{
leftHandAnchor.localPosition=leftPos;
}
if(OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.LeftHand,NodeStatePropertyType.Orientation,OVRPlugin.Node.HandLeft,OVRPlugin.Step.Render,out leftQuat))
如果(!L_GrabbedObject.isgrapped)
leftHandAnchor.localRotation=leftQuat;
}else if(mirror.mirrorRight)
{
if(OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.righand,NodeStatePropertyType.Position,OVRPlugin.Node.HandRight,OVRPlugin.Step.Render,out rightPos))
if(R_GrabbedObject.isgrapped)
{
if(R_GrabbedObject.m_grabbedObj.name==“rollingPin”)
{
Vector3 rightHand=rightHandAnchor.localPosition;
右侧z=右侧z位置;
rightHandAnchor.localPosition=右手;
}
}
其他的
{
rightHandAnchor.localPosition=rightPos;
}
if(OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.righHand,NodeStatePropertyType.Orientation,OVRPlugin.Node.HandRight,OVRPlugin.Step.Render,out rightQuat))
如果(!R_GrabbedObject.isgrapped)
{
rightHandAnchor.localRotation=rightQuat;
}
}
}
其他的
{
如果(镜像。镜像左侧)
{
if(L_GrabbedObject.isgrapped)
{
if(L_GrabbedObject.m_grabbedObj.name==“rollingPin”)
{
Vector3 leftHand=leftHandAnchor.localPosition;
leftHand.z=OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch).z;
leftHandAnchor.localPosition=leftHand;
}
}
其他的
{
leftHandAnchor.localPosition=OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
leftHandAnchor.localRotation=OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
}
}else if(mirror.mirrorRight){
if(R_GrabbedObject.isgrapped)
{
if(R_GrabbedObject.m_grabbedObj.name==“rollingPin”)
{
Vector3 rightHand=rightHandAnchor.localPosition;
rightHand.z=OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch).z;
rightHandAnchor.localPosition=右手;
}
}
其他的
{
rightHandAnchor.localPosition=OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
rightHandAnchor.localRotation=OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
    var mirror = GameObject.Find("OVRCameraRig").GetComponent<VRMirror>();
    var L_GrabbedObject = GameObject.Find("Left Hand Target").GetComponent<OVRGrabber>();
    var R_GrabbedObject = GameObject.Find("Right Hand Target").GetComponent<OVRGrabber>();
    //Need this for controller offset because if we're on OpenVR, we want to set the local poses as specified by Unity, but if we're not, OVRInput local position is the right anchor
    if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
    {
        Vector3 leftPos = Vector3.zero;
        Vector3 rightPos = Vector3.zero;
        Quaternion leftQuat = Quaternion.identity;
        Quaternion rightQuat = Quaternion.identity;


        if (mirror.mirrorLeft)
        {
            if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandLeft, OVRPlugin.Step.Render, out leftPos))
                if(L_GrabbedObject.isGrabbed)
                {
                    if(L_GrabbedObject.m_grabbedObj.name == "rollingPin")
                    {
                        Vector3 leftHand = leftHandAnchor.localPosition;
                        leftHand.z = leftPos.z;
                        leftHandAnchor.localPosition = leftHand;
                    }
                }else
                {
                    leftHandAnchor.localPosition = leftPos;
                }

            if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.LeftHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandLeft, OVRPlugin.Step.Render, out leftQuat))
                if (!L_GrabbedObject.isGrabbed)
                    leftHandAnchor.localRotation = leftQuat;
        }else if (mirror.mirrorRight)
        {
            if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandRight, OVRPlugin.Step.Render, out rightPos))
                if (R_GrabbedObject.isGrabbed)
                {
                    if (R_GrabbedObject.m_grabbedObj.name == "rollingPin")
                    {
                        Vector3 rightHand = rightHandAnchor.localPosition;
                        rightHand.z = rightPos.z;
                        rightHandAnchor.localPosition = rightHand;
                    }
                }
                else
                {
                    rightHandAnchor.localPosition = rightPos;
                }
            if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.RightHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandRight, OVRPlugin.Step.Render, out rightQuat))
                if (!R_GrabbedObject.isGrabbed)
                {
                    rightHandAnchor.localRotation = rightQuat;
                }
        }
    }
    else
    {
        if (mirror.mirrorLeft)
        {
            if (L_GrabbedObject.isGrabbed)
            {
                if (L_GrabbedObject.m_grabbedObj.name == "rollingPin")
                {
                    Vector3 leftHand = leftHandAnchor.localPosition;
                    leftHand.z = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch).z;
                    leftHandAnchor.localPosition = leftHand;
                }
            }
            else
            {
                leftHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
                leftHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
            }


        }else if  (mirror.mirrorRight) {
            if (R_GrabbedObject.isGrabbed)
            {
                if (R_GrabbedObject.m_grabbedObj.name == "rollingPin")
                {
                    Vector3 rightHand = rightHandAnchor.localPosition;
                    rightHand.z = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch).z;
                    rightHandAnchor.localPosition = rightHand;
                }
            }
            else
            {
                rightHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
                rightHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
            }


        }
    }