简体   繁体   中英

Unity XR VR Issue with Inputs and GetDeviceWithCharacteristics syntax possible?

I had to comment out a bunch of my code but it came down to this with 2 errors. The issue seems to be with the line that uses 'GetDevicesWithCharacteristics' because that is where the errors point too but i checked the documentation and it seems good. I also checked the line above and i'm not sure if the ', lefthandedcontrollers;' that i added after the fact to follow the documentation(which wasn't actually the problem).

I'm inexperienced so please forgive me if this is a simple mistake.


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

The Errors were:

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

Thanks for any help in advance!

I'm talking about those lines

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

in the second line, are you sure about this

, leftHandedControllers;

because this syntax is incorrect and does not have any semantics also.

In the third line, what are you trying to do in the parameters! you are redefining the previous variables in the function parameters!

I believe your code should be like:

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

The reason why your code is still throwing an error is because InputDevices.GetDevicesWithCharacteristics only works when inside a function like void start or void update . I found that if you place some of the code that's outside any function on the inside your void start method works perfectly fine.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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