Skip to main content
Version: 0.4.1

Beach ball

A big, floaty ball you hit with your hands, for volleyball over a net. Every device checks its own player's hands (GetHandPosition) each frame. A hand moving fast into the ball hits it: the controller buzzes, the player takes ownership and, once ownership has arrived (OnOwnershipTransferred), sets the ball's velocity from the hand's speed plus some lift. The player who hit it last simulates it, and Forecast shows everyone else where it really is, so they can hit it back. On desktop, click the ball to bump it.

Put it on a sphere with a Rigidbody (low mass and some drag make it float), a Sphere Collider and an ST Object Sync with Forecast on. Set Radius to the collider's radius. If it flies more than Max Distance from its start, it comes back.

BeachBall.cs
using StageTime.SDK;
using UnityEngine;

/// <summary>
/// A beach ball you hit with your hands. Put it on a ball with a Rigidbody, a Sphere Collider and an Object Sync (turn
/// on Forecast so a ball hit by someone else arrives where it really is). Every device checks its own player's hands:
/// a hand moving into the ball takes ownership, and the hit is applied on this device once ownership has arrived, so
/// the player who hit it last simulates it. Clicking it (desktop) bumps it up and away. Falls back to its start when it
/// leaves <c>maxDistance</c>. Shows <c>GetHandPosition</c>, <c>OnOwnershipTransferred</c> and Rigidbody velocity.
/// </summary>
public class BeachBall : StageTimeBehaviour
{
[Tooltip("Ball radius in metres (the Sphere Collider's world radius).")]
public float radius = 0.4f;
[Tooltip("Slowest hand speed that hits the ball (m/s).")]
public float minHandSpeed = 1f;
[Tooltip("Ball speed per unit of hand speed.")]
public float power = 1.3f;
[Tooltip("Extra upward speed added to every hit (m/s), so hits float.")]
public float lift = 2.5f;
[Tooltip("The ball goes back to its start when it gets this far from it (metres).")]
public float maxDistance = 25f;

private STObjectSync sync;
private Rigidbody body;
private Vector3 start;
private Vector3 lastLeft, lastRight;
private bool haveHands;
private float cooldownUntil;
private Vector3 pendingVelocity;
private bool waitingForOwnership;

private void Start()
{
sync = GetComponent<STObjectSync>();
body = GetComponent<Rigidbody>();
start = transform.position;
}

private void Update()
{
StageTimePlayer me = Networking.LocalPlayer;
if (me == null) return;
Vector3 left = me.GetHandPosition(STInputHand.Left);
Vector3 right = me.GetHandPosition(STInputHand.Right);
float dt = Time.deltaTime;
if (haveHands && dt > 0f && Time.time >= cooldownUntil)
{
if (!TryHit(left, (left - lastLeft) / dt, STInputHand.Left))
TryHit(right, (right - lastRight) / dt, STInputHand.Right);
}
lastLeft = left;
lastRight = right;
haveHands = true;

if (Networking.IsOwner(gameObject) && !waitingForOwnership && (transform.position - start).sqrMagnitude > maxDistance * maxDistance)
sync.Respawn();
}

private bool TryHit(Vector3 hand, Vector3 handVelocity, STInputHand which)
{
Vector3 offset = transform.position - hand;
float reach = radius + 0.08f;
if (offset.sqrMagnitude > reach * reach) return false;
float speed = handVelocity.magnitude;
// Only a hand moving fast and towards the ball hits it.
if (speed < minHandSpeed || Vector3.Dot(handVelocity, offset) <= 0f) return false;
StageTimeInput.PlayHaptics(which, 0.6f, 0.06f);
Hit(handVelocity * power + Vector3.up * lift);
return true;
}

public override void Interact()
{
StageTimePlayer me = Networking.LocalPlayer;
if (me == null) return;
Vector3 away = transform.position - me.GetPosition();
away.y = 0f;
if (away.sqrMagnitude < 0.01f) away = Vector3.forward;
Hit(away.normalized * 2.5f + Vector3.up * 7f);
}

private void Hit(Vector3 velocity)
{
cooldownUntil = Time.time + 0.25f;
if (Networking.IsOwner(gameObject) && !waitingForOwnership)
{
body.linearVelocity = velocity;
return;
}
// Only the owner's device simulates the ball: apply the hit once ownership has arrived here.
pendingVelocity = velocity;
if (waitingForOwnership) return;
waitingForOwnership = true;
Networking.SetOwner(Networking.LocalPlayer, gameObject);
}

public override void OnOwnershipTransferred(StageTimePlayer player)
{
if (!waitingForOwnership) return;
waitingForOwnership = false;
if (player.isLocal) body.linearVelocity = pendingVelocity;
}
}