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