Spin the bottle
Click the bottle to spin it for everyone. The spinner picks a random end angle with Random.Range and syncs only
the start angle, the end angle and the network tick it started on. Every player
works out the bottle's angle from the room clock each frame, so they all see the same spin, nothing is sent while it
turns, and late joiners see where it stopped.
Put the script on the bottle (it needs a collider to be clicked) and set Bottle to the transform that turns.
SpinBottle.cs
using StageTime.SDK;
using UnityEngine;
/// <summary>
/// Spin the bottle. Click the bottle to spin it for everyone: the spinner picks a random end angle, and every player
/// eases the bottle from the start angle to the end angle on the room clock, so they all see the same spin and late
/// joiners see where it stopped. Nothing is sent while it turns.
/// </summary>
public class SpinBottle : StageTimeBehaviour
{
public Transform bottle;
public float spinSeconds = 4f;
[Synced] private float fromAngle;
[Synced] private float toAngle;
[Synced] private int startTick;
private void Update()
{
if (bottle != null) bottle.localRotation = Quaternion.Euler(0f, CurrentAngle(), 0f);
}
public override void Interact()
{
if (Progress() < 1f) return; // let it finish first
Networking.SetOwner(Networking.LocalPlayer, gameObject);
fromAngle = toAngle % 360f;
toAngle = fromAngle + Random.Range(3, 6) * 360f + Random.Range(0f, 360f);
startTick = Networking.GetServerTick();
}
private float Progress()
{
double elapsed = Networking.GetServerTimeInSeconds() - (double)startTick / Networking.GetTickRate();
return Mathf.Clamp01((float)elapsed / spinSeconds);
}
private float CurrentAngle()
{
float t = 1f - Progress();
return Mathf.Lerp(fromAngle, toAngle, 1f - t * t * t); // ease out: fast, then slowing to a stop
}
}