Tick-synced cube
Every player computes the same angle from the shared clock, so cubes line up exactly without sending anything while they spin. The label shows the tick data behind it. See Time and ticks.
TickRotatingCube.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// A cube that spins in step on every client. Instead of syncing its rotation, it syncs the network tick and angle
/// it last started (or stopped) at; every client works out the same angle from the shared room clock. Nothing is
/// sent while it spins, and late joiners are in phase straight away. Click the cube or the button to toggle.
/// </summary>
public class TickRotatingCube : StageTimeBehaviour
{
public float degreesPerSecond = 90f;
public TMP_Text label;
[Synced] private bool spinning = true;
[Synced] private float baseAngle; // angle when it last started or stopped
[Synced] private int startTick; // network tick when it last started or stopped
[Synced] private int toggles;
private void Update()
{
float angle = CurrentAngle();
transform.localRotation = Quaternion.Euler(0f, angle, 0f);
ShowLabel(angle);
}
public override void Interact()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
baseAngle = CurrentAngle(); // freeze or resume from where it is now
startTick = Networking.GetServerTick();
spinning = !spinning;
toggles++;
}
/// <summary>Seconds of room time since <see cref="startTick"/>.</summary>
private double ElapsedSeconds()
{
double startSeconds = (double)startTick / Networking.GetTickRate();
return Networking.GetServerTimeInSeconds() - startSeconds;
}
private float CurrentAngle()
{
if (!spinning) return baseAngle;
double angle = (baseAngle + ElapsedSeconds() * degreesPerSecond) % 360.0;
return (float)angle;
}
private void ShowLabel(float angle)
{
if (label == null) return;
int tick = Networking.GetServerTick();
StageTimePlayer owner = Networking.GetOwner(gameObject);
string ownerName = owner != null ? owner.displayName : "-";
label.text = $"Tick {tick} @ {Networking.GetTickRate()} Hz\n"
+ $"Server time {Networking.GetServerTimeInSeconds():0.000} s\n"
+ $"Start tick {startTick} (+{tick - startTick} ticks, {ElapsedSeconds():0.000} s)\n"
+ $"Angle {angle:0.00}° from {baseAngle:0.00}°\n"
+ $"{(spinning ? "Spinning" : "Stopped")} | toggles {toggles} | owner {ownerName}";
}
}