Time Scaling

Summary

Use Time.TimeScale to adjust server physics simulation times.

Description

Reactor server scripts can set the value of Time.TimeScale to change the rate of physics simulations and even pause physics updates. The default physics time step is 16.666ms which is then mulitplied by Time.TimeScale when advancing the physics simulation. The time simulated on each frame is tracked and synced to connected clients.

The ksRoomType component has a property named 'Apply Server Time Scale' which can be used to couple the server timescale time with the Unity timescale. Enable this property if you want the Unity time scale to match the synced server time scale.

The following example code defines an RPC handler which will increase or decrease the time scale by a constant factor. Add this RPC method to a ServerRoomScript and invoke Room.CallRPC(0, true) or Room.CallRPC(0, false) in a ClientRoomScript to change the server time scaling.

ServerRoomScript RPC Handler

[ksRPC(0)]
public void ChangeTimeScale(ksIServerPlayer player, bool increase)
{
    if (increase && Time.TimeScale < 10.0f)
    {
        Time.TimeScale *= 1.25f;
        ksLog.Debug("Time scale = " + Time.TimeScale);
        return;
    }

    if (!increase && m_spawnRate > 0.1f)
    {
        Time.TimeScale /= 1.25f;
        ksLog.Debug("Time scale = " + Time.TimeScale);
        return;
    }
}

ClientRoomScript RPC Call

public void Update()
{
    // Increase the server time scale when 1 is pressed
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        Room.CallRPC(0, true);
    }

    // Decrease the server time scale when 2 is pressed
    if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        Room.CallRPC(0, false);
    }
}