Skip to main content
Version: 0.4.1

High five

When one of your hands touches another player's hand, your controllers buzz and a label counts your high fives. Each device checks its own player's hands with GetHandPosition, so the script needs no synced variables. Put it on any object in your world and give it a TextMeshProUGUI label. reach sets how close two hands must be (0.15 m by default). In VR the hand is where the controller is; on desktop and phones it is where the avatar's hand hangs. See Players.

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

/// <summary>
/// High fives: when one of the local player's hands touches another player's hand, both controllers of the local
/// player buzz and the label counts it. Every device checks its own player's hands, so it needs no networking. Put it
/// on any object and give it a TextMeshPro label. Shows <c>GetHandPosition</c> and <c>StageTimeInput.PlayHaptics</c>.
/// </summary>
public class HighFive : StageTimeBehaviour
{
public TMP_Text label;
[Tooltip("How close two hands must be to count (metres).")]
public float reach = 0.15f;

private StageTimePlayer[] players = new StageTimePlayer[0];
private int highFives;
private float cooldownUntil;

private void Update()
{
if (Time.time < cooldownUntil) return;
StageTimePlayer me = Networking.LocalPlayer;
if (me == null) return;

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

Vector3 myLeft = me.GetHandPosition(STInputHand.Left);
Vector3 myRight = me.GetHandPosition(STInputHand.Right);
for (int i = 0; i < players.Length; i++)
{
StageTimePlayer other = players[i];
if (other == null || other.isLocal) continue;
Vector3 left = other.GetHandPosition(STInputHand.Left);
Vector3 right = other.GetHandPosition(STInputHand.Right);
if (Touching(myLeft, left) || Touching(myLeft, right) || Touching(myRight, left) || Touching(myRight, right))
{
highFives++;
cooldownUntil = Time.time + 1f;
StageTimeInput.PlayHaptics(STInputHand.Left, 0.8f, 0.1f);
StageTimeInput.PlayHaptics(STInputHand.Right, 0.8f, 0.1f);
if (label != null) label.text = "High five with " + other.displayName + "!\nTotal: " + highFives;
return;
}
}
}

private bool Touching(Vector3 a, Vector3 b) => (a - b).sqrMagnitude <= reach * reach;
}