Paintball Range
A shooting range made only from SDK pieces: pick up a paintball marker, knock down the pop-up targets sliding along their tracks, paint the walls, and tag other players.
Get it from the StageTime-Worlds-Examples repository: copy the Worlds/PaintballRange folder into a Unity
project that has the StageTime World SDK, and open its scene.
| Piece | Built with |
|---|---|
| Markers | ST Pickup + ST Projectile Launcher, with an ST Object Pool of paintballs |
| Targets | An ST Hitbox on each board; moved by the room clock |
| Splats | One ST Object Pool per paint colour, spawned from OnProjectileHit on every device |
| Scoreboard | One script with synced arrays; only its owner counts hits |
The game
Every device hears every hit, so the markers and targets report hits to the game, and only the game's owner
counts them. A target that is down stays down until the server tick in upAtTick, which every device can compare
with the clock, so nobody has to send "stand up" later. The owner also frees a player's row when they leave.
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// Paintball Range rules and scoreboard. Every player's device hears every hit (OnProjectileHit), so the guns and
/// targets report hits here and only this object's owner counts them: a hit can never score twice. Knocking a target
/// down scores 1 point and it pops back up after a few seconds; hitting another player scores 2.
/// Synced: when each target pops back up (server ticks) and up to 16 player scores.
/// </summary>
public class PaintballGame : StageTimeBehaviour
{
public TMP_Text board;
[Tooltip("Seconds a target stays down after a hit.")]
public float downSeconds = 4f;
public int targetPoints = 1;
public int playerPoints = 2;
[Synced] private int[] upAtTick = new int[8];
[Synced] private int[] playerIds = new int[16]; // player id + 1; 0 = free slot
[Synced] private int[] points = new int[16];
[Synced] private int[] hits = new int[16];
private void Start()
{
Show();
}
/// <summary>True while target <paramref name="index"/> is knocked down (any device).</summary>
public bool IsDown(int index)
{
if (index < 0 || index >= upAtTick.Length) return false;
return Networking.GetServerTick() < upAtTick[index];
}
/// <summary>A target was hit. Counted on the owner's device only.</summary>
public void ReportTargetHit(int index, StageTimePlayer shooter)
{
if (!Networking.IsOwner(gameObject) || shooter == null || index < 0 || index >= upAtTick.Length || IsDown(index)) return;
upAtTick[index] = Networking.GetServerTick() + (int)(downSeconds * Networking.GetTickRate());
AddScore(shooter.playerId, targetPoints);
}
/// <summary>A player hit another player. Counted on the owner's device only.</summary>
public void ReportPlayerHit(StageTimePlayer shooter, StageTimePlayer victim)
{
if (!Networking.IsOwner(gameObject) || shooter == null || victim == null || shooter.playerId == victim.playerId) return;
AddScore(shooter.playerId, playerPoints);
}
/// <summary>Reset button: clears every score and raises every target.</summary>
public void ResetScores()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
for (int i = 0; i < playerIds.Length; i++)
{
playerIds[i] = 0;
points[i] = 0;
hits[i] = 0;
}
for (int i = 0; i < upAtTick.Length; i++) upAtTick[i] = 0;
Show();
}
public override void OnDeserialization()
{
Show();
}
public override void OnPlayerLeft(StageTimePlayer player)
{
if (!Networking.IsOwner(gameObject)) return;
for (int i = 0; i < playerIds.Length; i++)
{
if (playerIds[i] != player.playerId + 1) continue;
playerIds[i] = 0;
points[i] = 0;
hits[i] = 0;
}
Show();
}
private void AddScore(int playerId, int amount)
{
int slot = -1;
for (int i = 0; i < playerIds.Length; i++)
{
if (playerIds[i] == playerId + 1) { slot = i; break; }
if (slot < 0 && playerIds[i] == 0) slot = i;
}
if (slot < 0) return; // 16 scorers already
playerIds[slot] = playerId + 1;
points[slot] += amount;
hits[slot] += 1;
Show();
}
private void Show()
{
if (board == null) return;
string text = "<b>PLAYER</b><pos=70%><b>HITS</b><pos=86%><b>PTS</b>\n";
int shown = 0;
// Highest score first: pick the best remaining slot each time (16 slots at most).
bool[] used = new bool[playerIds.Length];
for (int row = 0; row < playerIds.Length; row++)
{
int best = -1;
for (int i = 0; i < playerIds.Length; i++)
if (!used[i] && playerIds[i] != 0 && (best < 0 || points[i] > points[best])) best = i;
if (best < 0) break;
used[best] = true;
StageTimePlayer player = StageTimePlayer.GetPlayerById(playerIds[best] - 1);
string name = player != null ? player.displayName : "Player " + (playerIds[best] - 1);
text += name + "<pos=70%>" + hits[best] + "<pos=86%>" + points[best] + "\n";
shown++;
}
if (shown == 0) text += "\n<i>Grab a marker and hit the targets!</i>";
board.text = text;
}
}
The targets
A target's position comes from Networking.GetServerTimeInSeconds(), so it is in the same place on every device
without syncing anything per frame, and a player who joins late sees it in step at once. Each target in a row has a
different phase. While it is down, its hitbox colliders are off, so shots pass over it.
using StageTime.SDK;
using UnityEngine;
/// <summary>
/// A pop-up target that slides back and forth between two points. Its position comes from the room's clock, so every
/// player sees it in the same place without syncing anything per frame, and a late joiner is in step at once. A hit
/// on its STHitbox (the board) knocks it flat until PaintballGame raises it again.
/// </summary>
public class PaintballTarget : StageTimeBehaviour
{
public PaintballGame game;
[Tooltip("This target's number in PaintballGame (0 to 7).")]
public int index;
public Transform pointA;
public Transform pointB;
[Tooltip("Metres per second along the track.")]
public float speed = 1.2f;
[Tooltip("Seconds added to the clock, so targets on one track don't move together.")]
public float phase;
[Tooltip("The board, pivoted at its hinge: it tips back when the target is down.")]
public Transform board;
[Tooltip("The hitboxes, turned off while the target is down.")]
public Collider[] hitColliders;
[Tooltip("Degrees the board tips back (about its local X axis) when it is down.")]
public float downAngle = 85f;
[Tooltip("Optional: plays when the target goes down.")]
public AudioSource downAudio;
private float angle;
private bool collidersOn = true;
private void Update()
{
if (pointA != null && pointB != null)
{
float length = Vector3.Distance(pointA.position, pointB.position);
float t = 0f;
if (length > 0.01f)
{
float travelled = (float)((Networking.GetServerTimeInSeconds() + phase) * speed / length);
// Ping-pong along the track with eased ends.
t = Mathf.SmoothStep(0f, 1f, Mathf.PingPong(travelled, 1f));
}
transform.position = Vector3.Lerp(pointA.position, pointB.position, t);
}
bool down = game != null && game.IsDown(index);
angle = Mathf.MoveTowards(angle, down ? downAngle : 0f, (down ? 600f : 240f) * Time.deltaTime);
if (board != null) board.localRotation = Quaternion.Euler(angle, 0f, 0f);
if (down == collidersOn)
{
collidersOn = !down;
for (int i = 0; i < hitColliders.Length; i++) hitColliders[i].enabled = collidersOn;
if (down && downAudio != null) downAudio.Play();
}
}
public override void OnProjectileHit(STProjectileHit hit)
{
if (game != null) game.ReportTargetHit(index, hit.shooter);
}
}
The markers
The holder's device fires; holding the trigger keeps firing at the launcher's cooldown. Splats go on the scenery
only (a splat on a moving target would float in the air), turned by an angle worked out from where the ball landed so
it matches on every device.
using StageTime.SDK;
using UnityEngine;
/// <summary>
/// A paintball marker: an STPickup with an STProjectileLauncher. Hold the trigger (VR) or the left mouse button
/// (desktop) to fire. Every hit leaves a splat from this gun's colour pool on walls and bunkers, and hits on other
/// players are reported to PaintballGame for scoring. Only the holder's device fires; every device draws the splats.
/// </summary>
public class PaintballGun : StageTimeBehaviour
{
public STProjectileLauncher launcher;
[Tooltip("Splats in this gun's paint colour.")]
public STObjectPool splats;
public PaintballGame game;
[Tooltip("Keep firing while the trigger is held.")]
public bool automatic = true;
private bool triggerHeld;
public override void OnPickupUseDown()
{
triggerHeld = true;
launcher.Fire();
}
public override void OnPickupUseUp()
{
triggerHeld = false;
}
public override void OnDrop()
{
triggerHeld = false;
}
private void Update()
{
if (triggerHeld && automatic && launcher.canFire) launcher.Fire();
}
public override void OnProjectileHit(STProjectileHit hit)
{
if (hit.player != null)
{
if (game != null) game.ReportPlayerHit(hit.shooter, hit.player);
return;
}
// Moving targets would leave floating splats behind; paint only the scenery.
if (hit.hitbox != null || splats == null) return;
Vector3 normal = hit.normal;
Vector3 point = hit.point;
// The same roll on every device: derived from where it landed.
float roll = Mathf.Repeat(point.x * 131f + point.y * 71f + point.z * 37f, 360f);
Quaternion rotation = Quaternion.LookRotation(-normal) * Quaternion.Euler(0f, 0f, roll);
splats.Spawn(point + normal * 0.004f, rotation);
}
}