Forecast race
Two balls in two lanes, launched at the same moment by one button. Both have an ST Object Sync, but only the left one has Forecast on. The player who pressed the button owns both balls and sees them side by side; everyone else sees the forecast ball close to where it really is and the other one trailing behind it (about a metre at 7.5 m/s in our tests).
RaceStart picks a launch time one second ahead on the room clock (Networking.GetServerTimeInSeconds) and calls
Launch on each ball. A RaceBall takes ownership if it needs to, waits for OnOwnershipTransferred (only the owner
can move its physics), puts the ball back at its start and sets its velocity when the room clock reaches that time.
Set up each ball with a Rigidbody, a collider, an ST Object Sync and RaceBall (and an ST Pickup if players should
throw it too). Put RaceStart on a button and drag the balls into Balls.
using StageTime.SDK;
using TMPro;
/// <summary>
/// A start button for a race between balls: click it and every <see cref="RaceBall"/> in <c>balls</c> goes back to
/// its start and launches at the same room time, <c>countdown</c> seconds later. Give the balls different Object
/// Sync settings (Forecast on and off) to compare how other players see them. Shows calling other scripts with values.
/// </summary>
public class RaceStart : StageTimeBehaviour
{
public RaceBall[] balls;
public TMP_Text label;
public float countdown = 1f;
[Synced, MaxLength(40)] private string lastStarter = "";
public override void Interact()
{
double time = Networking.GetServerTimeInSeconds() + countdown;
foreach (RaceBall ball in balls)
if (ball != null) ball.Launch(time);
Networking.SetOwner(Networking.LocalPlayer, gameObject);
lastStarter = Networking.LocalPlayer.displayName;
Show();
}
public override void OnDeserialization()
{
Show();
}
private void Start()
{
Show();
}
private void Show()
{
if (label == null) return;
label.text = lastStarter == "" ? "Click to race" : "Started by " + lastStarter + "\nClick to race again";
}
}
using StageTime.SDK;
using UnityEngine;
/// <summary>
/// A ball that a <see cref="RaceStart"/> button launches. Put it on a ball with a Rigidbody and an Object Sync (and a
/// Pickup, so players can also throw it). <c>Launch</c> takes ownership, puts the ball back at its start and, at the
/// room time it was given, sets its velocity on the owner's device; everyone else follows through the Object Sync.
/// Shows <c>OnOwnershipTransferred</c>, the room clock and setting a Rigidbody's velocity.
/// </summary>
public class RaceBall : StageTimeBehaviour
{
/// <summary>Launch velocity, in world space.</summary>
public Vector3 launchVelocity = new Vector3(0f, 3f, 8f);
private STObjectSync sync;
private Rigidbody body;
private double fireAt = -1d;
private bool waitingForOwnership;
private void Start()
{
sync = GetComponent<STObjectSync>();
body = GetComponent<Rigidbody>();
}
/// <summary>Launches the ball at room time <paramref name="time"/> (from Networking.GetServerTimeInSeconds).</summary>
public void Launch(double time)
{
fireAt = time;
if (Networking.IsOwner(gameObject) && !waitingForOwnership)
{
sync.Respawn();
return;
}
// The physics only moves on the owner's device: wait until ownership has arrived.
waitingForOwnership = true;
Networking.SetOwner(Networking.LocalPlayer, gameObject);
}
public override void OnOwnershipTransferred(StageTimePlayer player)
{
if (!waitingForOwnership) return;
waitingForOwnership = false;
if (player.isLocal) sync.Respawn();
else fireAt = -1d; // someone else took it first
}
private void Update()
{
if (fireAt < 0d || waitingForOwnership || Networking.GetServerTimeInSeconds() < fireAt) return;
fireAt = -1d;
if (!Networking.IsOwner(gameObject)) return;
body.linearVelocity = launchVelocity;
body.angularVelocity = Vector3.zero;
}
}