Creating Game Logic with Cortana



As of now, i managed to launch game using START RACE command. Next i have to use Cortana for game logic. In gameplay, if player say left or right, Cortana should recognise speech and change car direction and speed. For that i have to create unity hook.

First, open up Unity and edit the CortanaInterop class. Once open we need to add the following static event code, which is the interface action for our speech recognition.

public static bool SpeechInProgress; public static event EventHandler SpeechRequested;public static void GetMeSomeVoice()
{
   
if (SpeechRequested != null && !SpeechInProgress)
    {
        SpeechRequested(
null, null);
    }
}



Next i need to initialize Speech Recognition for that switch over to the Windows project and the first thing i need to do is BUILD IT. This is simply because the Windows solution needs to compile the Interop code before it is made available in the OS project. Open up the SpeechHelper class and add the following property:

private static Windows.Media.SpeechRecognition.SpeechRecognizer speechRecognizer;private static IAsyncOperation<Windows.Media.SpeechRecognition.
SpeechRecognitionResult> recognitionOperation;


Also add StartRecognising method to SpeechHelper

public static async void StartRecognizing(object sender, EventArgs e)
        {
            CortanaInterop.SpeechInProgress = true;

            var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
            await speechRecognizer.CompileConstraintsAsync();
            Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeAsync();
            if (speechRecognitionResult.Status == Windows.Media.SpeechRecognition.SpeechRecognitionResultStatus.Success)
             {
            // Do something with the recognition result.
             CortanaInterop.CortanaText = speechRecognitionResult.Text;
            
            }
             else
             {
              CortanaInterop.CortanaText = "Sorry, could not hear you";

             }

            CortanaInterop.SpeechInProgress = false;
        }


Next we need to bind our Unity Interop event to our new StartRecognizing method in the SpeechHelper class. To do this, we need to add it to the code for the XAML page that the Unity game actually runs on, and by default this is the page called MainPage.xaml.cs. Open this file and locate the constructor MainPage() and add following at the end of the method:


 #region Speech Initialisation
        
            CortanaInterop.SpeechRequested += SpeechHelper.StartRecognizing;
        
#endregion

Now i can use GetMeVoice() Method to recognise Speech. Recognised speech will be stored to Cortana Text static variable.



In Cortana Rally Driver, whenever car gets closer to any curve it calls GetMeVoice method and listens to player command and if recognised speech matches the pace note of racing track car speed increases otherwise it decreases.

Comments

Popular Posts