playMaker

Author Topic: speech recognition  (Read 923 times)

fylian

  • Playmaker Newbie
  • *
  • Posts: 8
speech recognition
« on: July 22, 2019, 10:37:40 AM »
Hi, I'm trying to use a simple script to spawn projectiles with speech recognition. I want to right click mouse button and then activate the speech recognition. I've tried to use an invoke method action. It activates the script, but it shoots the first projectile even if I don't say a word; after the first projectile it works. Here's the speech recognition script, can someone tell me where I'm wrong?

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
using System.Linq;
using System.Collections.Generic;

public class SpeechRecognizer : MonoBehaviour
{   
    // Reference to projectile prefab to shoot
    public GameObject projectile;
    public float power = 10.0f;

    // Reference to AudioClip to play
    public AudioClip shootSFX;

    KeywordRecognizer keywordRecognizer;
    Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();

    void Start()
    {
        //Initialize stuff

        keywords.Add("fire", () =>
        {
            GoCalled();
        });

        keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());
        keywordRecognizer.OnPhraseRecognized += KeywordRecognizerOnPhraseRecognized;
        keywordRecognizer.Start();


    }

    void KeywordRecognizerOnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        System.Action keywordAction;

        if (keywords.TryGetValue(args.text, out keywordAction))
        {
            keywordAction.Invoke();
        }
    }

    void GoCalled()
    {
        print("Fire!!!");

        if (projectile)
        {
            // Instantiante projectile at the camera + 1 meter forward with camera rotation
            GameObject newProjectile = Instantiate(projectile, transform.position + transform.forward, transform.rotation) as GameObject;

            // if the projectile does not have a rigidbody component, add one
            if (!newProjectile.GetComponent<Rigidbody>())
            {
                newProjectile.AddComponent<Rigidbody>();
            }
            // Apply force to the newProjectile's Rigidbody component if it has one
            newProjectile.GetComponent<Rigidbody>().AddForce(transform.forward * power, ForceMode.VelocityChange);

            // play sound effect if set
            if (shootSFX)
            {
                if (newProjectile.GetComponent<AudioSource>())
                { // the projectile has an AudioSource component
                  // play the sound clip through the AudioSource component on the gameobject.
                  // note: The audio will travel with the gameobject.
                    newProjectile.GetComponent<AudioSource>().PlayOneShot(shootSFX);
                }
                else
                {
                    // dynamically create a new gameObject with an AudioSource
                    // this automatically destroys itself once the audio is done
                    AudioSource.PlayClipAtPoint(shootSFX, newProjectile.transform.position);
                }
            }
        }
    }
}
« Last Edit: July 22, 2019, 10:39:30 AM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: speech recognition
« Reply #1 on: July 22, 2019, 11:36:01 AM »
Hi.
This script is Activated on start.
Maybe you can place the script on a child object and enable/disable the object.

or use 'Activate Component' (Ecosystem) and activate the component when needed.

I also noticed a 'speech recognition' package on the Ecosystem

it holds a proxy component and on a Phrase recognized you can send a event to a fsm.
You then to bullet and sound from within playmaker.

You can also use 'Activate Component' on this component to activate/deactivate.