Voice Detection
VoiceReactController provides the core voice detection system with configurable sensitivity presets and gameplay-friendly features.
Detection Presets
VoiceReact includes three built-in sensitivity presets that cover most use cases.
Very Sensitive
Best for horror games where enemies should react to quiet sounds.
voiceController.SetDetectionPreset(DetectionPreset.VerySensitive);Characteristics:
- Detects whispers and quiet speech easily
- Start threshold: -25 dB
- Lower noise margin (2 dB)
- Shorter silence duration (0.6s)
Use Cases:
- Horror games (monsters hear everything)
- Tense stealth scenarios
- Forced silence mechanics
Balanced (Default)
Good default for most gameplay scenarios.
voiceController.SetDetectionPreset(DetectionPreset.Balanced);Characteristics:
- Reliable detection without false positives
- Start threshold: -20 dB
- Moderate noise margin (3 dB)
- Standard silence duration (0.8s)
Use Cases:
- General gameplay
- Stealth games with moderate difficulty
- Voice-controlled interactions
Less Sensitive
Best for noisy environments or when you want players to speak clearly.
voiceController.SetDetectionPreset(DetectionPreset.LessSensitive);Characteristics:
- Requires louder, clearer speech
- Start threshold: -15 dB
- Higher noise margin (5 dB)
- Longer silence duration (1.0s)
Use Cases:
- Noisy environments (trade shows, arcades)
- Competitive multiplayer (reduce false positives)
- Voice commands that need clarity
Detection Properties
Check if Player is Speaking
if (voiceController.IsPlayerSpeaking)
{
Debug.Log("Player is currently speaking");
}Get Current Volume
// Raw decibel level (-60 to 0 dB)
float db = voiceController.CurrentDB;
// Normalized volume (0-1 for UI meters)
float volume = voiceController.NormalizedVolume;Speech Duration
// How long has the current speech lasted?
float duration = voiceController.SpeechDuration;
// How long since they last spoke?
float timeSinceSpeech = voiceController.TimeSinceLastSpeech;Noise Floor
Background noise level automatically tracked:
float noiseFloor = voiceController.NoiseFloorDB;
Debug.Log($"Background noise: {noiseFloor} dB");Detection Cooldown
Prevent spam by requiring silence between detections:
[SerializeField] private VoiceReactController voiceController;
void Start()
{
// Require 2 seconds of silence between detections
voiceController.detectionCooldown = 2f;
}Or set cooldown dynamically:
void OnSpeechEnd(float duration)
{
// Force cooldown after speech
voiceController.SetDetectionCooldown(3f);
}Check cooldown status:
if (voiceController.IsInCooldown)
{
Debug.Log("Detection is in cooldown");
}Silence Detection
Check if player has been silent for a specific duration:
void Update()
{
// Reward for staying quiet
if (voiceController.HasBeenSilentFor(10f))
{
Debug.Log("Player has been silent for 10 seconds!");
AwardStealthBonus();
}
}Volume Normalization
Enable normalized volume for UI displays:
voiceController.enableVolumeNormalization = true;
void Update()
{
// Get 0-1 value perfect for UI sliders/meters
float normalizedVolume = voiceController.GetNormalizedVolume();
volumeMeter.fillAmount = normalizedVolume;
}Normalization automatically tracks min/max volume during the session and scales current volume to 0-1 range.
Custom Configuration
For full control, use Custom preset and configure manually:
voiceController.SetDetectionPreset(DetectionPreset.Custom);
// Access low-level MicrophoneInputHandler for custom thresholds
var handler = voiceController.GetComponent<MicrophoneInputHandler>();
// Create custom settings
var customSettings = new VoiceDetectionSettings
{
startThresholdDB = -22f,
endThresholdDB = -32f,
minAmplitudeThreshold = 0.003f,
significantAmplitudeThreshold = 0.06f,
noiseMarginDB = 3.5f,
minPercentageNonZero = 0.05f,
isConfigured = true
};
handler.ApplyDetectionSettings(customSettings);Understanding Detection Parameters
Start Threshold (dB)
Volume level required to start detecting speech.
- Lower = more sensitive (detects quieter sounds)
- Higher = less sensitive (requires louder speech)
- Range: -60 to 0 dB
End Threshold (dB)
Volume level below which detection ends.
- Should always be lower than start threshold
- Creates hysteresis to prevent flickering on/off
- Range: -60 to 0 dB
Noise Margin (dB)
Additional buffer above background noise floor.
- Higher = more immunity to noise
- Lower = more sensitive but risk false positives
- Range: 1-10 dB
Amplitude Thresholds
Min Amplitude Threshold: Minimum audio amplitude to be considered above noise.
- Range: 0.001 to 0.01
- Lower = more sensitive
Significant Amplitude Threshold: Amplitude required for valid speech detection.
- Range: 0.01 to 0.5
- Higher = requires clearer speech
Common Patterns
Dynamic Difficulty
Adjust sensitivity based on game difficulty:
public void SetDifficulty(int level)
{
switch (level)
{
case 1: // Easy
voiceController.SetDetectionPreset(DetectionPreset.LessSensitive);
break;
case 2: // Normal
voiceController.SetDetectionPreset(DetectionPreset.Balanced);
break;
case 3: // Hard
voiceController.SetDetectionPreset(DetectionPreset.VerySensitive);
break;
}
}Contextual Sensitivity
Change detection based on game state:
void OnEnterStealthZone()
{
// In stealth areas, be more sensitive
voiceController.SetDetectionPreset(DetectionPreset.VerySensitive);
}
void OnEnterSafeZone()
{
// In safe areas, less sensitive
voiceController.SetDetectionPreset(DetectionPreset.LessSensitive);
}Volume-Based Mechanics
React to different volume levels:
void Update()
{
if (voiceController.IsPlayerSpeaking)
{
float db = voiceController.CurrentDB;
if (db > -15f)
{
// Shout (very loud)
TriggerShoutMechanic();
}
else if (db > -25f)
{
// Normal speech
TriggerNormalMechanic();
}
else
{
// Whisper (quiet)
TriggerWhisperMechanic();
}
}
}UI Volume Meter
Display real-time volume in UI:
using UnityEngine.UI;
public class VolumeMeterUI : MonoBehaviour
{
[SerializeField] private VoiceReactController voiceController;
[SerializeField] private Image fillImage;
[SerializeField] private Color quietColor = Color.green;
[SerializeField] private Color loudColor = Color.red;
void Update()
{
// Get normalized volume (0-1)
float volume = voiceController.GetNormalizedVolume();
// Update fill amount
fillImage.fillAmount = volume;
// Color based on volume
fillImage.color = Color.Lerp(quietColor, loudColor, volume);
}
}Debugging
Enable debug mode to see real-time detection data:
var handler = voiceController.GetComponent<MicrophoneInputHandler>();
handler.EnableDebugMode(true);This shows:
- Current dB level
- Noise floor
- Detection thresholds
- Audio waveform visualization
- Detection state