说出语音命令时让玩家保持移动
Keep player moving when a voice command is said
我正在尝试使用 GrammarRecognizer 制作一个简单的语音命令游戏。我已经设法让玩家在说出相应的单词时向左或向右移动。
我试图让玩家连续朝一个方向移动,直到说出相反的词。
我试过使用 while 循环,但它似乎使程序崩溃。
如有任何帮助,我们将不胜感激。
请看下面的播放器控制器代码:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text; // for stringbuilder
using UnityEngine;
using UnityEngine.Windows.Speech; // grammar recogniser
public class PlayerScript : MonoBehaviour
{
private GrammarRecognizer gr;
public Rigidbody2D playerRb;
private void Start(){
playerRb = this.GetComponent<Rigidbody2D>();
gr = new GrammarRecognizer(Application.streamingAssetsPath + "/SimpleGrammar.xml", ConfidenceLevel.Medium);
gr.OnPhraseRecognized += GR_OnPhraseRecognized;
gr.Start();
Debug.Log("Grammer loaded and recogniser started");
}
private void GR_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
StringBuilder message = new StringBuilder();
SemanticMeaning[] meanings = args.semanticMeanings;
foreach(SemanticMeaning meaning in meanings)
{
string keyString = meaning.key.Trim();
//Get meanings
string valueString = meaning.values[0].Trim();
//output phrase
message.Append("Key: " + keyString + ", Value: " + valueString );
//if left is said
if(valueString == "left"){
//move left
transform.Translate(new Vector3(-200 * Time.deltaTime,0,0));
//transform.Translate(new Vector3(-200 * Time.deltaTime,0,0));
}
//if right is said
if(valueString == "right"){
//move right
transform.Translate(new Vector3(200 * Time.deltaTime,0,0));
}
}
Debug.Log(message);
}
private void OnApplicationQuit()
{
if(gr != null && gr.IsRunning)
{
gr.OnPhraseRecognized -= GR_OnPhraseRecognized;
gr.Stop();
}
}
}
请查看下面我的语法 XML 文件:
?xml version="1.0" encoding="utf-8" ?>
<grammar xml:lang="en" root="rightorleft"
tag-format="semantics/1.0" version="1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="rightorleft">
<one-of>
<item>
<ruleref uri="#right"/>
</item>
<item>
<ruleref uri="#left"/>
</item>
</one-of>
</rule>
<rule id="left">
<item>left</item>
</rule>
<rule id="right">
<item>right</item>
</rule>
</grammar>
好吧,而不是只做一次存储最后的结果并做例如
private enum Direction
{
Idle,
Left,
Right
}
private Direction currentDirection;
private void Update()
{
switch(currentDirection)
{
case Direction.Left:
transform.Translate(Vector3.right * -200 * Time.deltaTime);
break;
case Direction.Right:
transform.Translate(Vector3.right * 200 * Time.deltaTime);
break;
}
}
private void GR_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
var message = new StringBuilder();
var meanings = args.semanticMeanings;
foreach(var meaning in meanings)
{
var keyString = meaning.key.Trim();
var valueString = meaning.values[0].Trim();
message.Append($"Key: {keyString}, Value: {valueString}");
switch(valueString)
{
case "left":
currentDirection = Direction.Left;
break;
case "right":
currentDirection = Direction.Right;
break;
}
}
Debug.Log(message);
}
我正在尝试使用 GrammarRecognizer 制作一个简单的语音命令游戏。我已经设法让玩家在说出相应的单词时向左或向右移动。
我试图让玩家连续朝一个方向移动,直到说出相反的词。
我试过使用 while 循环,但它似乎使程序崩溃。
如有任何帮助,我们将不胜感激。
请看下面的播放器控制器代码:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text; // for stringbuilder
using UnityEngine;
using UnityEngine.Windows.Speech; // grammar recogniser
public class PlayerScript : MonoBehaviour
{
private GrammarRecognizer gr;
public Rigidbody2D playerRb;
private void Start(){
playerRb = this.GetComponent<Rigidbody2D>();
gr = new GrammarRecognizer(Application.streamingAssetsPath + "/SimpleGrammar.xml", ConfidenceLevel.Medium);
gr.OnPhraseRecognized += GR_OnPhraseRecognized;
gr.Start();
Debug.Log("Grammer loaded and recogniser started");
}
private void GR_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
StringBuilder message = new StringBuilder();
SemanticMeaning[] meanings = args.semanticMeanings;
foreach(SemanticMeaning meaning in meanings)
{
string keyString = meaning.key.Trim();
//Get meanings
string valueString = meaning.values[0].Trim();
//output phrase
message.Append("Key: " + keyString + ", Value: " + valueString );
//if left is said
if(valueString == "left"){
//move left
transform.Translate(new Vector3(-200 * Time.deltaTime,0,0));
//transform.Translate(new Vector3(-200 * Time.deltaTime,0,0));
}
//if right is said
if(valueString == "right"){
//move right
transform.Translate(new Vector3(200 * Time.deltaTime,0,0));
}
}
Debug.Log(message);
}
private void OnApplicationQuit()
{
if(gr != null && gr.IsRunning)
{
gr.OnPhraseRecognized -= GR_OnPhraseRecognized;
gr.Stop();
}
}
}
请查看下面我的语法 XML 文件:
?xml version="1.0" encoding="utf-8" ?>
<grammar xml:lang="en" root="rightorleft"
tag-format="semantics/1.0" version="1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="rightorleft">
<one-of>
<item>
<ruleref uri="#right"/>
</item>
<item>
<ruleref uri="#left"/>
</item>
</one-of>
</rule>
<rule id="left">
<item>left</item>
</rule>
<rule id="right">
<item>right</item>
</rule>
</grammar>
好吧,而不是只做一次存储最后的结果并做例如
private enum Direction
{
Idle,
Left,
Right
}
private Direction currentDirection;
private void Update()
{
switch(currentDirection)
{
case Direction.Left:
transform.Translate(Vector3.right * -200 * Time.deltaTime);
break;
case Direction.Right:
transform.Translate(Vector3.right * 200 * Time.deltaTime);
break;
}
}
private void GR_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
var message = new StringBuilder();
var meanings = args.semanticMeanings;
foreach(var meaning in meanings)
{
var keyString = meaning.key.Trim();
var valueString = meaning.values[0].Trim();
message.Append($"Key: {keyString}, Value: {valueString}");
switch(valueString)
{
case "left":
currentDirection = Direction.Left;
break;
case "right":
currentDirection = Direction.Right;
break;
}
}
Debug.Log(message);
}