Use instruments as game controllers!
This Unity interface allows you to map audio inputs to keyboard buttons - play a pitch from the mapped range on your instrument to trigger the corresponding key press.
The interface currently supports GetKey
, GetKeyDown
and GetKeyUp
events for all keyboard buttons.
Direct button presses (i.e., using the keyboard rather than a mapped instrument) will still be detected.
UnityPitchControl was tested on Unity version 4.6.2f1.
This project is based on Realtime C# Pitch Tracker.
- Copy the contents of Assets into the Assets folder of your project
- In Unity, click Pitch Input > Edit Key Mappings to open the editor GUI
- Select the desired audio input device
- Add key mappings as desired
- mappings can be removed using the '-' buttons
- Click Save Mappings
- In your game code, replace calls to
Input.GetKey
,Input.GetKeyDown
andInput.GetKeyUp
withUnityPitchControl.Input.InputManager.GetKey
,UnityPitchControl.Input.InputManager.GetKeyDown
andUnityPitchControl.Input.InputManager.GetKeyUp
, respectively - Before running your project, ensure your audio device is connected and switched on
The following note mappings trigger:
- the 't' key when a pitch between 80 and 120 is played
- the 'u' key when a pitch between 130 and 140 is played
- the 'a' key when a pitch between 145 and 155 is played
by using input from a guitar attached using a Rocksmith USB Guitar Adapter.
These keypresses may be detected programmatically using the following code:
if (UnityMidiControl.Input.InputManager.GetKeyDown("t")) {
Debug.Log("'x' down");
}
if (UnityMidiControl.Input.InputManager.GetKeyDown("u")) {
Debug.Log("'d' down");
}
if (UnityMidiControl.Input.InputManager.GetKeyDown("a")) {
Debug.Log("'a' down");
}
Using key codes rather than string arguments will also work:
if (UnityMidiControl.Input.InputManager.GetKeyUp(KeyCode.T)) {
Debug.Log("'x' up");
}
if (UnityMidiControl.Input.InputManager.GetKeyUp(KeyCode.U)) {
Debug.Log("'d' up");
}
if (UnityMidiControl.Input.InputManager.GetKeyUp(KeyCode.A)) {
Debug.Log("'a' up");
}