jeudi 9 avril 2020

Unity speech recogniser, how to use in conjunction with an if statement

I am creating a voice controlled game. One game I'm creating is a "shoot the targets" cannon game which will fire a cannon ball when the word "shoot" is said. I am trying to use Unity's speech recogniser in conjunction with an if statement in order to move the Slider UI's fill area with my voice. However, my initial method is drawing a few errors such as "Cannot convert Method group 'Up' to non-delegate type 'bool'. Did you intent to invoke the method?"

(This is my first proper time using StackOverflow, sorry if I haven't explained well)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Windows.Speech; 
using UnityEngine;
using UnityEngine.UI; 

public class SliderControls : MonoBehaviour
{
    //Voice Controls Setup 
    private KeywordRecognizer keywordRecognizer;
    private Dictionary<string, Action> actions = new Dictionary<string, Action>();
    float sliderValue; 

    // Start is called before the first frame update
    void Start()
    {
        actions.Add("Up", Up);
        actions.Add("Down", Down);

        keywordRecognizer = new KeywordRecognizer(actions.Keys.ToArray());
        keywordRecognizer.OnPhraseRecognized += Recognization;
        keywordRecognizer.Start();   
    }

   private void Recognization(PhraseRecognizedEventArgs speech)
   {
        Debug.Log(speech.text);
        actions[speech.text].Invoke(); 
   }

   private void Up()
    {
        ControlGUI(); 
    }

    private void Down()
    {
        ControlGUI(); 
    }

    public void ControlGUI ()
    {
        if (Up)
        {
            sliderValue = sliderValue + 0.1F; 
        }
        else if(Down)
        {
            sliderValue = sliderValue - 0.1F; 
        }

        sliderValue = GUI.HorizontalSlider(new Rect(25F, 25F, 600F, 30F), sliderValue, 0.0F, 10.0F);
        return;
    }


}

Aucun commentaire:

Enregistrer un commentaire