Next, a MeshRenderer variable is declared. Next, you declare a KeywordRecognizer to, well, recognize your words. The Codeīefore creating any voice commands, you will need the following using statements at the top of the script.įirst, you’ll need to define the Dictionary that will store the voice commands and what action they perform. Open the script in Visual Studio by double-clicking it in the Assets window. Now that the script is attached to the object, it’s time to make the code. Select Cube in the Hierarchy, then click and drag the VoiceControl script into the Inspector window underneath the Add Component button.įigure 11: Adding the VoiceControl script component. When finished, the Assets window will look like the below figure.įinally, attach the VoiceControl script to the Cube object. In the Assets window, right-click and choose Create->C# Script. Next, it will be time to make the script needed to implement voice commands. Once the sound assets have finished importing, you can either remove the Asset Store window you opened or simply switch over to the Scene window. You can deselect all the sounds you don’t desire if you wish, but to keep things simple, this example import all the sounds in the package.
The button should say Import after the download has completed.Īfter pressing Import, the Import Unity Package window will appear. After the download has been completed, you’ll need to press the same button to import the assets. On the next screen, you’ll need to select the Download button to download the assets. In the window that appears, search for Voices SFX and select the corresponding item by Little Robot Sound Factory in the drop-down menu that appears. At the top of the Unity window select Window->General->Asset Store.
You can import sounds from your computer if you wish, but in this example, the Asset Store will be used to acquire some free sounds. Of course, an Audio Source component would be rather useless without sounds. Search for Audio Source then select the component at the top of the list. In the Inspector window, click the Add Component button. Once the object has been created, you’ll need to add an Audio Source component to it. For this project, you can leave the object to the default Cube name. In the Hierarchy window click Create->3D Object->Cube. The first thing you’ll be doing is creating the Cube object needed for the project. Once everything is set up, click Create Project. This example images will be of a 3D project, but you can apply the same concepts in a 2D project as well. Give the project the name VoiceProject, then specify the project location. Upon starting Unity, you will need to create a new project. If what you say matches the phrase you define in code, then a user-defined function will be performed. Accomplishing this will require Unity to look for certain phrases, which you will define.
A single cube will be created, and you will be able to command the cube to change colors, spin in a certain direction, make a sound, and print a message to Unity’s Debug Log. You will, of course, need a mic to be able to run the project. In a moment you’ll be creating a project that will be controlled using nothing but your voice.
What if you were told that you can make your own voice-controlled experience? As long as you have Unity and a microphone to test the project, you can! Even now, with a little searching, you can easily find a game online that requires a microphone and your voice to play.
Games that could be played using your voice have existed since the late 90s with games like Hey You, Pikachu and Seaman being two notable examples from that time. You don’t see it very often, but voice commands are no stranger to the world of video games. Voice Commands in Unity - Simple Talk Skip to content