Skip to main content
Version: 0.4.1

Area scanner

Press the scanner to list the objects and players near it, nearest first, and what is straight ahead of it. Put it on an object with a collider and a TextMeshPro label. Its own collider is in the overlap results too, so the script skips it; keep that collider out of the cast ahead (for example below it), or the cast reports the scanner itself. See Physics queries.

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

/// <summary>
/// A scanner: press it to list the objects and players within <c>radius</c> metres, nearest first, and what is straight
/// ahead of it (a cast 0.25 m wide along its forward axis, so keep its own collider out of that path). Shows
/// <c>STPhysics.OverlapSphere</c>, <c>OverlapPlayers</c> and <c>TrySphereCast</c>.
/// </summary>
public class AreaScanner : StageTimeBehaviour
{
public TMP_Text label;
public float radius = 3f;

private GameObject[] found = new GameObject[16];
private StageTimePlayer[] nearby = new StageTimePlayer[16];

public override void Interact()
{
Scan();
}

public void Scan()
{
Vector3 center = transform.position;
int count = STPhysics.OverlapSphere(center, radius, found);
int players = STPhysics.OverlapPlayers(center, radius, nearby);
// The scanner's own collider (it needs one to be clicked) is in the results too: skip it.
int objects = 0;
GameObject nearest = null;
for (int i = 0; i < count; i++)
{
if (found[i] == gameObject) continue;
if (nearest == null) nearest = found[i];
objects++;
}
string text = objects + " objects, " + players + " players";
if (nearest != null) text += "\nNearest: " + nearest.name;
if (STPhysics.TrySphereCast(center, 0.25f, transform.forward, 20f, out STRaycastHit hit))
text += "\nAhead: " + hit.gameObject.name + " at " + hit.distance.ToString("0.0") + " m";
else
text += "\nAhead: nothing";
if (label != null) label.text = text;
}
}