Fist-Full-of-Shrimp / FFOS-Unity-VR-Template

This is a template project to help launch a project that is ready for VR/AR development right out of the box.
MIT License
6 stars 8 forks source link

Problem with hand being instantiated twice #3

Open umaruta4 opened 1 year ago

umaruta4 commented 1 year ago

When the hand being instantiated twice (for no reason), because i'm having that problem. The hand object is being spawned twice, but the previous one is not responsive, so it's going to be this weird buggy like this video here https://media.discordapp.net/attachments/390924344475254792/1146760688279236678/weird_bug.mp4

umaruta4 commented 1 year ago

I'm not too familiar with github thing, so I'm going to post my code here if any of you are having the same problem. This is in Hand.cs inside the Scripts/FFOSScripts

umaruta4 commented 1 year ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

//Hand is used to help animate
public class Hand : MonoBehaviour
{
    //Stores handPrefab to be Instantiated
    public GameObject handPrefab;

    //Allows for hiding of hand prefab if set to true
    public bool hideHandOnSelect = false;

    //Stores what kind of characteristics we're looking for with our Input Device when we search for it later
    public InputDeviceCharacteristics inputDeviceCharacteristics;

    //Stores the InputDevice that we're Targeting once we find it in InitializeHand()
    private InputDevice _targetDevice;
    private Animator _handAnimator;
    private SkinnedMeshRenderer _handMesh;

    public void HideHandOnSelect()
    {
        if (hideHandOnSelect)
        {
            _handMesh.enabled = !_handMesh.enabled;
        }
    }
    private void Start()
    {
        InitializeHand();
    }

    private void InitializeHand()
    {
        List<InputDevice> devices = new List<InputDevice>();
        //Call InputDevices to see if it can find any devices with the characteristics we're looking for
        InputDevices.GetDevicesWithCharacteristics(inputDeviceCharacteristics, devices);

        //Our hands might not be active and so they will not be generated from the search.
        //We check if any devices are found here to avoid errors.
        if (devices.Count > 0)
        {

            _targetDevice = devices[0];

            // Making sure that it's valid before instantiating the hand. - umaruto 31/08/2023
            if(_targetDevice.isValid){
                GameObject spawnedHand = Instantiate(handPrefab, transform);
                _handAnimator = spawnedHand.GetComponent<Animator>();
                _handMesh = spawnedHand.GetComponentInChildren<SkinnedMeshRenderer>();
            }
        }
    }

    // Update is called once per frame
    private void Update()
    {
        //Since our target device might not register at the start of the scene, we continously check until one is found.
        if(!_targetDevice.isValid)
        {
            // When isValid went false again even though _handAnimator already been instantiated, destroy the hand. - umaruto 31/08/2023
            if(_handAnimator != null){
                DestroyHand();
            }
            InitializeHand();
        }
        else
        {
            UpdateHand();
        }
    }

    private void UpdateHand()
    {
        //This will get the value for our trigger from the target device and output a flaot into triggerValue
        if (_targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue))
        {
            _handAnimator.SetFloat("Trigger", triggerValue);
        }
        else
        {
            _handAnimator.SetFloat("Trigger", 0);
        }
        //This will get the value for our grip from the target device and output a flaot into gripValue
        if (_targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue))
        {
            _handAnimator.SetFloat("Grip", gripValue);
        }
        else
        {
            _handAnimator.SetFloat("Grip", 0);
        }
    }

    // Adding destroy hand just incase if hand went buggy. - umaruto 31/08/2023
    private void DestroyHand()
    {

        Destroy(_handAnimator.gameObject);

        // Reset references
        _handAnimator = null;
        _handMesh = null;
    }
}