Search Unity

Question Vr code error

Discussion in 'VR' started by wetdrip, Jan 28, 2023.

  1. wetdrip

    wetdrip

    Joined:
    Jan 26, 2023
    Posts:
    1
    I am following a tutorial on how to setup vr hads in unity and I recieve this error when trying to run.

    Assets\HandPresense.cs(94,6): error CS1513: } expected

    This is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR;

    public class HandPresence : MonoBehaviour
    {
    public bool showController = false;
    public InputDeviceCharacteristics controllerCharacteristics;
    public List<GameObject> controllerPrefabs;
    public GameObject handModelPrefab;

    private InputDevice targetDevice;
    private GameObject spawnedController;
    private GameObject spawnedHandModel;
    private Animator handAnimator;

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

    void TryInitialize()
    {
    List<InputDevice> devices = new List<InputDevice>();

    InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);

    foreach (var item in devices)
    {
    Debug.Log(item.name + item.characteristics);
    }

    if (devices.Count > 0)
    {
    targetDevice = devices[0];
    GameObject prefab = controllerPrefabs.Find(controller => controller.name == targetDevice.name);
    if (prefab)
    {
    spawnedController = Instantiate(prefab, transform);
    }
    else
    {
    Debug.LogError("Error");
    spawnedController = Instantiate(controllerPrefabs[0], transform);
    }

    spawnedHandModel = Instantiate(handModelPrefab, transform);
    handAnimator = spawnedHandModel.GetComponent<Animator>();
    }
    }
    void UpdateHandAnimation()
    {
    if (targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue))
    {
    handAnimator.SetFloat("Trigger", triggerValue);
    }
    else
    {
    handAnimator.SetFloat("Trigger", 0);
    }
    if (targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue))
    {
    handAnimator.SetFloat("Grip", gripValue);
    }
    else
    {
    handAnimator.SetFloat("Grip", 0);
    }
    }

    // Update is called once per frame
    void Update()
    {
    if (!targetDevice.isValid)
    {
    TryInitialize();
    }
    else
    {
    if (showController)
    {
    spawnedHandModel.SetActive(false);
    spawnedController.SetActive(true);
    }
    else
    {
    spawnedHandModel.SetActive(true);
    spawnedController.SetActive(false);
    UpdateHandAnimation();
    }
    }
    }
     
  2. nilagard

    nilagard

    Joined:
    Jan 13, 2017
    Posts:
    77
    While I know what your issue is, I will not give you the answer. Not directly. It looks as you are trying to code something without knowing simple syntax/coding basics. You should take some C# (coding) courses on how to structure and familiarize yourself with syntax and setups. Simply explained: You are trying to bite off something that which is way bigger that can fit in your mouth. Diving straight into gamedev without any form of coding knowledge will screw you over and make you disgruntled, big time. You are starting in the wrong end my friend!