You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: com.unity.netcode.gameobjects/Documentation~/advanced-topics/network-prefab-handler.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,9 +74,9 @@ To un-register a prefab handler, you can [invoke the `NetworkManager.PrefabHandl
74
74
75
75
## Object spawning with prefab handlers
76
76
77
-
Once a prefab handler is registered, Netcode for GameObjects automatically uses the defined `Initialize` and `Destroy` methods to manage the object lifecycle. [Spawn the network prefab as usual](../basics/object-spawning.md#spawning-a-network-prefab-overview) and the `Initialize` method will be called on whichever handler is registered with the spawned network prefab.
77
+
Once a prefab handler is registered, Netcode for GameObjects automatically uses the defined `Initialize` and `Destroy` methods to manage the object lifecycle. [Spawn the network prefab as usual](../basics/object-spawning.md#spawn-a-network-prefab) and the `Initialize` method will be called on whichever handler is registered with the spawned network prefab.
78
78
79
-
Note that the `Initialize` method is only called on non-authority clients. To customize network prefab behavior on the authority, you can use [prefab overrides](../basics/object-spawning.md#taking-prefab-overrides-into-consideration).
79
+
Note that the `Initialize` method is only called on non-authority clients. To customize network prefab behavior on the authority, you can use [prefab overrides](../basics/object-spawning.md#consider-prefab-overrides).
80
80
81
81
### Object spawning with custom data
82
82
@@ -255,4 +255,4 @@ When it comes to including instantiation data, you should be cautious about incl
Copy file name to clipboardExpand all lines: com.unity.netcode.gameobjects/Documentation~/advanced-topics/networktime-ticks.md
+49-37Lines changed: 49 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,16 @@
1
-
# NetworkTime and ticks
1
+
# Network time and ticks
2
2
3
-
## LocalTime and ServerTime
3
+
Understand how Netcode for GameObjects calculates network time, and when to use local time or server time.
4
4
5
-
Why are there two different time values and which one should be used?
5
+
## Local time and server time
6
6
7
-
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 `RPCs` and `NetworkVariable`won't happen immediately on other machines. `NetworkTime`allows to use time while considering those transmission delays.
7
+
Netcode for GameObjects uses a star topology. That means all communications happen between the clients and the server or host, and never between clients directly. Messages take time to transmit over the network, so RPCs and `NetworkVariable`updates don't take effect immediately on other machines. Use `NetworkTime` to work with time while accounting for these transmission delays.
8
8
9
-
-`LocalTime` on a client is ahead of the server. If a server RPC is sent at `LocalTime` from a client it will roughly arrive at `ServerTime` on the server.
10
-
-`ServerTime` on clients is behind the server. If a client RPC is sent at `ServerTime` from the server to clients it will roughly arrive at `ServerTime` on the clients.
9
+
-`LocalTime` on a client is ahead of the server. It's the client's estimate of what the server clock reads right now: the last server time the client received, plus half the round trip time (RTT) to account for that message's own travel, plus a one-tick buffer.
10
+
-`ServerTime` on clients is behind the server. If the server sends a client RPC at `ServerTime`, the RPC arrives at roughly `ServerTime` on the clients.
11
+
12
+
> [!NOTE]
13
+
> `LocalTime` leads the server clock by a fixed one tick, and that lead doesn't scale with latency. A message a client sends at `LocalTime` therefore reaches the server exactly as the server clock reaches the same value only when the RTT is about two ticks, which is roughly 67 ms at the default tick rate of 30. On faster connections the message arrives before that point, and on slower connections after it. Don't use `LocalTime` to predict which server tick processes a given message. For the measured latency in ticks, use `NetworkTimeSystem.TickLatency`, which is based on the full RTT. To give outgoing messages more lead, increase `NetworkTimeSystem.LocalBufferSec`, as described in [Configure the network time system](#configure-the-network-time-system).
11
14
12
15
```mermaid
13
16
sequenceDiagram
@@ -16,29 +19,31 @@ sequenceDiagram
16
19
participant Receiver as Client ServerTime
17
20
Note over Owner: Send message to server at LocalTime.
18
21
Owner->>Server: Delay when sending message
19
-
Note over Server: Message arrives at ServerTime.
22
+
Note over Server: Message arrives near LocalTime, offset by half RTT minus one tick.
20
23
Note over Server: On server: ServerTime == LocalTime.
21
24
Note over Server: Send message to clients at LocalTime.
22
25
Server->>Receiver: Delay when sending message
23
26
Note over Receiver: Message arrives at ServerTime.
24
27
```
25
28
26
-
`LocalTime`
27
-
- Use for player objects with client authority.
28
-
- Use if just a general time value is needed.
29
+
Use `LocalTime` in the following cases:
30
+
31
+
- For player objects with client authority.
32
+
- For a general time value.
33
+
34
+
Use `ServerTime` in the following cases:
29
35
30
-
`ServerTime`:
31
-
- For player objects with server authority (For example, by sending inputs to the server via RPCs)
32
-
- In sync with position updates of NetworkTransform for all `NetworkObjects` where the client isn't authoritative over the transform.
33
-
- For everything on non client controlled `NetworkObjects`.
36
+
- For player objects with server authority, for example by sending inputs to the server through RPCs.
37
+
- To stay in sync with position updates of the `NetworkTransform` component for all `NetworkObject` instances where the client isn't authoritative over the transform.
38
+
- For everything on `NetworkObject` instances that the client doesn't control.
34
39
35
-
## Examples
40
+
## Network time examples
36
41
37
-
### Example 1: Using network time to synchronize environments
42
+
### Synchronize environments with network time
38
43
39
-
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 NetworkTransform.
44
+
Many games have environmental objects that move in a fixed pattern. Use network time to move these objects without synchronizing their positions with a `NetworkTransform` component.
40
45
41
-
For instance the following code can be used to create a moving elevator platform for a clientauthoritative game:
46
+
For example, the following code creates a moving elevator platform for a client-authoritative game:
42
47
43
48
```csharp
44
49
usingUnity.Netcode;
@@ -55,9 +60,9 @@ public class MovingPlatform : MonoBehaviour
55
60
}
56
61
```
57
62
58
-
### Example 2: Using network time to create a synced event
63
+
### Create a synced event with network time
59
64
60
-
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.
65
+
You don't usually need to align an effect precisely to time. However, for important effects or gameplay events, precise alignment improves consistency, especially for clients with poor network connections.
61
66
62
67
```csharp
63
68
usingSystem.Collections;
@@ -131,16 +136,17 @@ sequenceDiagram
131
136
```
132
137
133
138
> [!NOTE]
134
-
> 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.
139
+
> Some components, such as `NetworkTransform`, add additional buffering. When you align an RPC event as in this example, add an extra delay.
135
140
136
-
## Network Ticks
141
+
## Network ticks
137
142
138
-
Network ticks are run at a fixed rate. The 'Tick Rate' field on the NetworkManager can be used to set the tick rate.
143
+
Network ticks run at a fixed rate. To set the tick rate, use the **Tick Rate** field on the NetworkManager component.
139
144
140
-
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.
145
+
Changing the network tick rate affects when Netcode for GameObjects sends `NetworkVariable` changes. It doesn't send them immediately. Instead, it collects the changes during each network tick and sends them to other peers.
141
146
142
-
To run custom code once per network tick (before `NetworkVariable` changes are collected) the `Tick` event on the `NetworkTickSystem` can be used.
143
-
```cs
147
+
To run custom code once per network tick, before Netcode for GameObjects collects `NetworkVariable` changes, subscribe to the `Tick` event on the `NetworkTickSystem`.
148
+
149
+
```csharp
144
150
publicoverridevoidOnNetworkSpawn()
145
151
{
146
152
NetworkManager.NetworkTickSystem.Tick+=Tick;
@@ -158,31 +164,37 @@ public override void OnNetworkDespawn() // don't forget to unsubscribe
158
164
```
159
165
160
166
> [!NOTE]
161
-
> When using `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 > TimeFixed Timestep`.
167
+
> When you use `FixedUpdate` or physics in your game, set the network tick rate to the same rate as the fixed update rate. To change the `FixedUpdate` rate, go to **Edit** > **Project Settings** > **Time** and set **Fixed Timestep**.
162
168
163
-
## Network FixedTime
169
+
## Network fixed time
164
170
165
-
`Network FixedTime`can be used to get a time value representing the time during a network tick. This works similar to `FixedUpdate` where `Time.fixedTime` represents the time during the `FixedUpdate`.
171
+
Use `FixedTime` to get a time value that represents the time during a network tick. This works in the same way as `FixedUpdate`, where `Time.fixedTime` represents the time during the `FixedUpdate`.
166
172
167
-
```cs
173
+
```csharp
168
174
publicvoidUpdate()
169
175
{
170
176
doubletime=NetworkManager.Singleton.LocalTime.Time; // time during this Update
171
177
doublefixedTime=NetworkManager.Singleton.LocalTime.FixedTime; // time during the previous network tick
172
178
}
173
179
```
174
180
175
-
## NetworkTime Precision
181
+
## Network time precision
182
+
183
+
Netcode for GameObjects calculates network time values as double-precision floating-point values. This keeps time accurate on long-running servers. If your game server runs sessions for a long time, such as multiple hours or days, don't convert this value to a float. Always use doubles for time-related calculations.
176
184
177
-
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.
185
+
For games with short play sessions, you can safely cast the time to a float or use `TimeAsFloat`.
178
186
179
-
For games with short play sessions casting the time to float is safe or `TimeAsFloat` can be used.
187
+
## Configure the network time system
180
188
181
-
## NetworkTimeSystem Configuration
189
+
To change how Netcode for GameObjects calculates network time, configure the `NetworkTimeSystem`. Refer to [`NetworkTimeSystem`](xref:Unity.Netcode.GameObjects.Timing.NetworkTimeSystem) for information about the properties you can modify. You can safely adjust all properties at runtime. For example, increase the buffer values for a client with a poor connection.
182
190
183
191
> [!NOTE]
184
-
> 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.
192
+
> Don't change the properties of the `NetworkTimeSystem` on the server or host. To change the behavior of the time system, change the values on the client instead.
185
193
186
-
The way network time gets calculated can be configured in the `NetworkTimeSystem` if needed. Refer to the [API docs](xref:Unity.Netcode.GameObjects.Timing.NetworkTimeSystem) 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.
194
+
## Additional resources
187
195
188
-
<!-- On page code -->
196
+
-[`NetworkTimeSystem` API reference](xref:Unity.Netcode.GameObjects.Timing.NetworkTimeSystem)
0 commit comments