繁体   English   中英

Unity XR VR 可能存在输入和 GetDeviceWithCharacteristics 语法问题?

[英]Unity XR VR Issue with Inputs and GetDeviceWithCharacteristics syntax possible?

我不得不注释掉我的一堆代码,但最终导致了 2 个错误。 问题似乎与使用“GetDevicesWithCharacteristics”的行有关,因为这也是错误指向的地方,但我检查了文档,它看起来不错。 我还检查了上面的行,但我不确定“,左撇子控制器;” 我在事后添加以遵循文档(这实际上不是问题)。

我没有经验,所以如果这是一个简单的错误,请原谅我。


using UnityEngine.Events;
using UnityEngine.XR;

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

public class controlerInputScript : MonoBehaviour
{
    GameObject gameObjectToMove;
    public int scale = 3;


    var leftHandedControllers = new List<UnityEngine.XR.InputDevice>();
    InputDeviceCharacteristics leftcontrollerdesiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Left | UnityEngine.XR.InputDeviceCharacteristics.Controller, leftHandedControllers;
    InputDevices.GetDevicesWithCharacteristics(XR.InputDeviceCharacteristics leftcontrollerdesiredCharacteristics, List<InputDevice> leftHandedControllers);


    // var rightHandedControllers = new List<UnityEngine.XR.InputDevice>();
    // var rightcontrollerdesiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Right | UnityEngine.XR.InputDeviceCharacteristics.Controller;
    // UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(rightcontrollerdesiredCharacteristics, rightHandedControllers);


    // if (leftHandedControllers > 1) || (rightHandedControllers >1){
    //     Debug.Log("DEBUG: ERROR: More than one Left or Right handed controller detected!")
    // }

    //assign Controlllers
    InputDevice leftController = leftHandedControllers[0];
    //InputDevice rightController = rightHandedControllers[0];

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // // Update is called once per frame
    void Update()
    {
        Vector3 p = this.transform.position;
        float leftTriggerState;
        if (leftController.TryGetFeatureValue(CommonUsages.trigger, out leftTriggerState)){
            p.y = scale * leftTriggerState;
            gameObjectToMove.transform.position = p;
        }
     
    //     float rightGripState;
    //     if (rightController.TryGetFeatureValue(CommonUsages.grip, out rightGripState)){
    //         p.x = scale* rightGripState;
    //         gameObjectToMove.transform.position = p;
    //     }
    }
}

错误是:

Assets\ConQuestV3\controlerInputScript.cs(58,47): error CS1519: Invalid token '(' in class, struct, or interface member declaration

Assets\ConQuestV3\controlerInputScript.cs(58,156): error CS1519: Invalid token ';' in class, struct, or interface member declaration

提前感谢您的帮助!

我说的是那些台词

var leftHandedControllers = new List<UnityEngine.XR.InputDevice>();
InputDeviceCharacteristics leftcontrollerdesiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Left | UnityEngine.XR.InputDeviceCharacteristics.Controller, leftHandedControllers;
InputDevices.GetDevicesWithCharacteristics(XR.InputDeviceCharacteristics leftcontrollerdesiredCharacteristics, List<InputDevice> leftHandedControllers);

在第二行,你确定这个

, leftHandedControllers;

因为这种语法不正确,也没有任何语义。

在第三行中,您要在参数中做什么! 您正在重新定义 function 参数中的先前变量!

我相信你的代码应该是这样的:

var leftHandedControllers = new List<UnityEngine.XR.InputDevice>();
InputDeviceCharacteristics leftcontrollerdesiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Left | UnityEngine.XR.InputDeviceCharacteristics.Controller;
InputDevices.GetDevicesWithCharacteristics(leftcontrollerdesiredCharacteristics, leftHandedControllers);

您的代码仍然抛出错误的原因是因为InputDevices.GetDevicesWithCharacteristics仅在 function 内部有效,例如void startvoid update 我发现,如果您将任何 function 之外的一些代码放在内部,您的void start方法就可以正常工作。

using UnityEngine.Events;
using UnityEngine.XR;

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

public class controlerInputScript : MonoBehaviour
{
    GameObject gameObjectToMove;
    public int scale = 3;

    InputDevice leftController;

    // Start is called before the first frame update
    void Start()
    {
        var leftHandedControllers = new List<UnityEngine.XR.InputDevice>();
        InputDeviceCharacteristics leftcontrollerdesiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Left | UnityEngine.XR.InputDeviceCharacteristics.Controller, leftHandedControllers;
        InputDevices.GetDevicesWithCharacteristics(XR.InputDeviceCharacteristics leftcontrollerdesiredCharacteristics, List < InputDevice > leftHandedControllers);


        // var rightHandedControllers = new List<UnityEngine.XR.InputDevice>();
        // var rightcontrollerdesiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Right | UnityEngine.XR.InputDeviceCharacteristics.Controller;
        // UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(rightcontrollerdesiredCharacteristics, rightHandedControllers);


        // if (leftHandedControllers > 1) || (rightHandedControllers >1){
        //     Debug.Log("DEBUG: ERROR: More than one Left or Right handed controller detected!")
        // }

        //assign Controlllers
        InputDevice leftController = leftHandedControllers[0];
        //InputDevice rightController = rightHandedControllers[0];
    }

    // // Update is called once per frame
    void Update()
    {
        Vector3 p = this.transform.position;
        float leftTriggerState;
        if (leftController.TryGetFeatureValue(CommonUsages.trigger, out leftTriggerState))
        {
            p.y = scale * leftTriggerState;
            gameObjectToMove.transform.position = p;
        }

        //     float rightGripState;
        //     if (rightController.TryGetFeatureValue(CommonUsages.grip, out rightGripState)){
        //         p.x = scale* rightGripState;
        //         gameObjectToMove.transform.position = p;
        //     }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM