Time
Knowing the current time on the server, which is shared between all users, allows a developer to easily synchronize events or animations without having to even send a message. It is however important to understand the different types of clock that are available.
Each type of clock is listed below, with a brief description. It is important to use the right clock when attempting to make use of the server time.
For most cases, the Relative Clock will be the preferred choice.
Clock | Description |
---|---|
Global Clock | Returns the time since the session started |
Relative Clock | When playing back a recording, this will return the Playback Clock. Otherwise it will return the Global Clock instead. |
Each of the clock values are only updated when a message is received (roughly every 0.2 seconds). If you tried to use any of these values to drive something such as an animation, you would get very choppy playback. To solve this problem, we have added ‘smoothed’ variants of each clock, where we use Unity’s unscaled time value to extrapolate, based on the time in which the last clock update was received.
Below is an example script which, when applied to a game object, will rotate in time with the server clock. This means that all users will see the object synchronized to the same rotation. Any newly joining users will also see it at the same rotation.
using ImmerseSDK.PlatformServices.Multiuser;
using UnityEngine;
/// <summary>
/// Rotates a transform using the ghost clock to determine it's rotation
/// </summary>
public class ClockDemo : MonoBehaviour
{
[Tooltip("How fast it should rotate")]
public float Speed = 10f;
[Tooltip("If this should used smoothed time")]
public bool Smoothed;
private void Update()
{
// Do not update when not connected
if (Networking.Status != ConnectionStatus.Connected)
{
return;
}
var time = Smoothed
? Networking.Client.Time.RelativeClockSmoothed
: Networking.Client.Time.RelativeClock;
transform.localEulerAngles = new Vector3(0, time * Speed, 0);
}
}