Skip to main content
Version: 0.4.1

Quiet zone

A library, a back row or a backstage area: anyone standing in the zone is heard more quietly by everyone, while they still hear the room normally. Put the script on an object at the centre of the zone's floor, set size and gain (0.3 is about a third as loud), and add a TextMeshProUGUI label if you like. Every device works out who is inside from where players stand, so the script needs no synced variables. See Voice.

QuietZone.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;

/// <summary>
/// A quiet zone, like a library or the back of a theatre: anyone standing in it is heard more quietly by everyone, and
/// they hear the room normally. Put it on the zone's centre object and set <c>size</c> and <c>gain</c> (0.3 = about a
/// third as loud, 0 = silent). Every device works out who is inside from where players stand, so it needs no networking.
/// Shows <c>SetVoiceGain</c> and <c>GetVoiceGain</c>. For a fully private room use a voice channel instead (see the
/// Private booth example).
/// </summary>
public class QuietZone : StageTimeBehaviour
{
public TMP_Text label;
public Vector3 size = new Vector3(4f, 3f, 4f);
[Range(0f, 1f)] public float gain = 0.3f;

private StageTimePlayer[] players = new StageTimePlayer[0];
private float nextCheck;

private void Update()
{
if (Time.time < nextCheck) return;
nextCheck = Time.time + 0.25f;

int count = StageTimePlayer.GetPlayerCount();
if (players.Length != count) players = new StageTimePlayer[count];
StageTimePlayer.GetPlayers(players);

int inside = 0;
for (int i = 0; i < players.Length; i++)
{
StageTimePlayer player = players[i];
if (player == null) continue;
bool isInside = IsInside(player.GetPosition());
if (isInside) inside++;
float wanted = isInside ? gain : 1f;
if (player.GetVoiceGain() != wanted) player.SetVoiceGain(wanted);
}

if (label != null) label.text = "Quiet zone\nInside: " + inside;
}

private bool IsInside(Vector3 position)
{
Vector3 local = transform.InverseTransformPoint(position);
return Mathf.Abs(local.x) <= size.x * 0.5f && Mathf.Abs(local.z) <= size.z * 0.5f && local.y >= -0.5f && local.y <= size.y;
}

// Everyone sounds normal again when the zone goes away (the world unloading resets this too).
private void OnDisable()
{
for (int i = 0; i < players.Length; i++)
if (players[i] != null) players[i].SetVoiceGain(1f);
}
}