Skip to main content
Version: 0.4.1

Private booth

A soundproof booth on its own voice channel. While the local player is inside, only players in the same booth hear them, and their voice isn't sent to anyone outside at all. Players inside still hear the room. Put the script on an object at the centre of the booth's floor, set size, and give each booth its own channel (1 to 32). StageTime shows the player a banner with the channel number, and buzzes their controllers, when they go in and when they come out. To turn voices down instead of cutting them off, see the Quiet zone. See Voice channels.

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

/// <summary>
/// A soundproof booth that uses a voice channel: while the local player is inside, their voice goes only to the other
/// players in the booth, and voices from inside never reach anyone outside (they aren't even sent, which saves
/// bandwidth). Players inside still hear the room. Put it on the booth's centre object and set <c>size</c> and
/// <c>channel</c> (1 to 32, a different one per booth). StageTime shows the player a banner when they go in and come
/// out. Shows <c>StageTimeVoice.SetChannel</c> and <c>GetChannel</c>.
/// </summary>
public class PrivateBooth : StageTimeBehaviour
{
public TMP_Text label;
public Vector3 size = new Vector3(3f, 3f, 3f);
public int channel = 1;

private float nextCheck;

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

StageTimePlayer me = Networking.LocalPlayer;
if (me == null) return;
bool inside = IsInside(me.GetPosition());
int wanted = inside ? channel : 0;
// Refused when changed twice within half a second; the next check tries again.
if (StageTimeVoice.GetChannel() != wanted) StageTimeVoice.SetChannel(wanted);

if (label != null) label.text = inside ? "Booth " + channel + "\nOnly people in here can hear you" : "Booth " + channel;
}

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;
}

// Back to the room's voice when the booth goes away (the world unloading resets this too).
private void OnDisable()
{
if (StageTimeVoice.GetChannel() == channel) StageTimeVoice.SetChannel(0);
}
}