VR Voice Recognition

Rushil Reddy

Note: This tutorial requires the use of the Unity 5.4 beta which can be found here:


using System;

using System.Text;

using UnityEngine;

using UnityEngine.Windows.Speech;

public class VoiceRecog : MonoBehaviour {


     private string[] m_Keywords;

     private KeywordRecognizer m_Recognizer;

     public GameObject Cube;

     public GameObject Sphere;

void Start() {

     m_Keywords = new string[2];

     m_Keywords[0] = "Cube";

     m_Keywords[1] = "Sphere";

     m_Recognizer = new KeywordRecognizer(m_Keywords);

     m_Recognizer.OnPhraseRecognized += OnPhraseRecognized;



private void OnPhraseRecognized(PhraseRecognizedEventArgs args) {

     StringBuilder builder = new StringBuilder();

     float newX = UnityEngine.Random.Range(-3, 3);

     float newZ = UnityEngine.Random.Range(-3, 3);

     if (args.text == m_Keywords[0]) {

          Instantiate(Cube, new Vector3(newX, newZ, 1), Quaternion.identity);


else {

          Instantiate(Sphere, new Vector3(newX, newZ, 1), Quaternion.identity);


     builder.AppendFormat("{0} ({1}){2}", args.text, args.confidence, Environment.NewLine);

     builder.AppendFormat("\tTimestamp: {0}{1}", args.phraseStartTime, Environment.NewLine);

     builder.AppendFormat("\tDuration: {0} seconds{1}", args.phraseDuration.TotalSeconds, Environment.NewLine);




Let's start by creating two prefabs in our scene. Create>3D object and choose a cube as well as a sphere. Now drag them to our project folder to create the associated prefabs. Create an empty game object and call it Voice. Now Add Component> New Script and call it VoiceRecog. Copy the following code into it:

if(args.confidence == ConfidenceLevel.High)

You should be able to spawn Cubes and Spheres with just your voice!

That’s it! Hit the play button and put on your headset to test it out.