NetworkTime and Ticks
#LocalTime and ServerTime
Why are there two different time values and which one should be used?
Netcode for GameObjects (Netcode) uses a star topology. That means all communications happen between the clients and the server/host and never between clients directly. Messages take time to transmit over the network. That's why
NetworkVariable won't happen immediately on other machines.
NetworkTime allows to use time while considering those transmission delays.
LocalTimeon a client is ahead of the server. If a server RPC is sent at
LocalTimefrom a client it will roughly arrive at
ServerTimeon the server.
ServerTimeon clients is behind the server. If a client RPC is sent at
ServerTimefrom the server to clients it will roughly arrive at
ServerTimeon the clients.
- Use for player objects with client authority.
- Use if just a general time value is needed.
- For player objects with server authority (For example, by sending inputs to the server via RPCs)
- In sync with position updates of
NetworkObjectswhere the client isn't authoritative over the transform.
- For everything on non client controlled
#Example 1: Using network time to synchronize environments
Many games have environmental objects which move in a fixed pattern. By using network time these objects can be moved without having to synchronize their positions with a
For instance the following code can be used to create a moving elevator platform for a client authoritative game:
#Example 2: Using network time to create a synced event
Most of the time aligning an effect precisely to time isn't needed. But in some cases for important effects or gameplay events it can help to improve consistency especially for clients with bad network connections.
Some components such as
NetworkTransform add additional buffering. When trying to align an RPC event like in this example, an additional delay would need to be added.
Network ticks are run at a fixed rate. The 'Tick Rate' field on the
NetworkManager can be used to set the tick rate.
What does changing the network tick affect? Changes to
NetworkVariables aren't sent immediately. Instead during each network tick changes to
NetworkVariables are collected and sent out to other peers.
To run custom code once per network tick (before
NetworkVariable changes are collected) the
Tick event on the
NetworkTickSystem can be used.
FixedUpdate or physics in your game, set the network tick rate to the same rate as the fixed update rate. The
FixedUpdate rate can be changed in
Edit > Project Settings > Time > Fixed Timestep
Network FixedTime can be used to get a time value representing the time during a network tick. This works similar to
Time.fixedTime represents the time during the
Network time values are calculated using double precisions. This allows time to stay accurate on long running servers. For game servers which run sessions for a long time (multiple hours or days) don't convert this value in a float and always use doubles for time related calculations.
For games with short play sessions casting the time to float is safe or
TimeAsFloat can be used.
The properties of the
NetworkTimeSystem should be left untouched on the server/host. Changing the values on the client is sufficient to change the behavior of the time system.
The way network time gets calculated can be configured in the
NetworkTimeSystem if needed. See the API docs (TODO LINK) for information about the properties which can be modified. All properties can be safely adjusted at runtime. For instance buffer values can be increased for a player with a bad connection.