Sync simulation time
Use the Clock static class to get a unified simulation time source across Unity's default timeline, deterministic stepped simulation, and distributed rendering.
Get the current simulation time
Read Clock.Now to get the current simulation time in seconds, regardless of which clock mode is active.
double simulationTime = Clock.Now;
Clock.Now returns the time in seconds recorded at the beginning of the current Update or FixedUpdate frame. When called from within a FixedUpdate, it returns the fixed-update start time. When called from Update, it returns the frame start time.
float delta = Clock.deltaTime; // equivalent to Clock.DeltaTimeInSeconds
Understand clock modes
Clock determines the mode automatically at runtime. You don't typically set it manually.
| Mode | When active | Time source |
|---|---|---|
UnityUnscaled |
No TimeStepApplier component found in the scene (default). |
Time.timeAsDouble / Time.fixedTimeAsDouble |
FoundationClock |
A TimeStepApplier component is present and enabled. |
TimeStepApplier.TimeAtFrameStart / ElapsedFixedUpdateTime |
ExternalClock |
Distributed rendering render node (set automatically). | Values pushed via Clock.SetExternalTime. |
Force FoundationClock mode
If you need precise, stepped simulation time (for example, when recording deterministic replays), call:
Clock.EnsureSimulatedTime();
This creates a TimeStepApplier instance if one does not already exist and switches the mode to FoundationClock.
Enable stepped simulation time with TimeStepApplier
TimeStepApplier is a MonoBehaviour that accumulates simulated time independently of Unity's Time.timeAsDouble. Clock discovers it automatically; add it to any GameObject in your scene to activate FoundationClock mode.
Note: The source doesn't fully specify the scheduling and stepping behavior of
TimeStepApplierandTimeStepScheduler. Refer to the component's Inspector fields for configuration details.
Sync time across distributed rendering nodes
When com.unity.simulationpro.distributedrendering is present and distribution is enabled, render nodes receive their clock from the scene server via DistributedRenderingEvents. The distributed rendering integration calls Clock.SetExternalTime internally. You should not normally need to call it directly.
// Called internally by distributed rendering:
Clock.SetExternalTime(timeAsDouble, fixedTimeAsDouble, unscaledDeltaTime);
Using Clock.Now in sensors and publishers
Read Clock.Now at the start of each FixedUpdate (or Update) to timestamp messages consistently:
void FixedUpdate()
{
double stamp = Clock.Now;
var msg = new ImuMsg();
msg.header.stamp = ClockHelpers.ConstructTimeMsg(stamp);
publisher.Publish(msg);
}
Handle clock resets on scene change
The clock mode resets to Unset whenever the active scene changes. Clock re-detects it on the next access to Clock.Now. This ensures Clock correctly picks up a TimeStepApplier newly added to the next scene.