Skip to content

Commit 6dfdb78

Browse files
committed
Doc bugs + style pass
1 parent 297c8b0 commit 6dfdb78

6 files changed

Lines changed: 227 additions & 180 deletions

File tree

‎com.unity.netcode.gameobjects/Documentation~/advanced-topics/network-prefab-handler.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ To un-register a prefab handler, you can [invoke the `NetworkManager.PrefabHandl
7474

7575
## Object spawning with prefab handlers
7676

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.
7878

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).
8080

8181
### Object spawning with custom data
8282

@@ -255,4 +255,4 @@ When it comes to including instantiation data, you should be cautious about incl
255255
## Additional resources
256256

257257
- [Object pooling](./object-pooling.md)
258-
- [Authority prefab overrides](../basics/object-spawning.md#taking-prefab-overrides-into-consideration)
258+
- [Authority prefab overrides](../basics/object-spawning.md#consider-prefab-overrides)

‎com.unity.netcode.gameobjects/Documentation~/advanced-topics/networktime-ticks.md‎

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
# NetworkTime and ticks
1+
# Network time and ticks
22

3-
## LocalTime and ServerTime
3+
Understand how Netcode for GameObjects calculates network time, and when to use local time or server time.
44

5-
Why are there two different time values and which one should be used?
5+
## Local time and server time
66

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.
88

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).
1114
1215
```mermaid
1316
sequenceDiagram
@@ -16,29 +19,31 @@ sequenceDiagram
1619
participant Receiver as Client ServerTime
1720
Note over Owner: Send message to server at LocalTime.
1821
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.
2023
Note over Server: On server: ServerTime == LocalTime.
2124
Note over Server: Send message to clients at LocalTime.
2225
Server->>Receiver: Delay when sending message
2326
Note over Receiver: Message arrives at ServerTime.
2427
```
2528

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:
2935

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.
3439

35-
## Examples
40+
## Network time examples
3641

37-
### Example 1: Using network time to synchronize environments
42+
### Synchronize environments with network time
3843

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.
4045

41-
For instance the following code can be used to create a moving elevator platform for a client authoritative game:
46+
For example, the following code creates a moving elevator platform for a client-authoritative game:
4247

4348
```csharp
4449
using Unity.Netcode;
@@ -55,9 +60,9 @@ public class MovingPlatform : MonoBehaviour
5560
}
5661
```
5762

58-
### Example 2: Using network time to create a synced event
63+
### Create a synced event with network time
5964

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.
6166

6267
```csharp
6368
using System.Collections;
@@ -131,16 +136,17 @@ sequenceDiagram
131136
```
132137

133138
> [!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.
135140
136-
## Network Ticks
141+
## Network ticks
137142

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.
139144

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.
141146

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
144150
public override void OnNetworkSpawn()
145151
{
146152
NetworkManager.NetworkTickSystem.Tick += Tick;
@@ -158,31 +164,37 @@ public override void OnNetworkDespawn() // don't forget to unsubscribe
158164
```
159165

160166
> [!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 > Time Fixed 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**.
162168
163-
## Network FixedTime
169+
## Network fixed time
164170

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`.
166172

167-
```cs
173+
```csharp
168174
public void Update()
169175
{
170176
double time = NetworkManager.Singleton.LocalTime.Time; // time during this Update
171177
double fixedTime = NetworkManager.Singleton.LocalTime.FixedTime; // time during the previous network tick
172178
}
173179
```
174180

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.
176184

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`.
178186

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
180188

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.
182190

183191
> [!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.
185193
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
187195

188-
<!-- On page code -->
196+
- [`NetworkTimeSystem` API reference](xref:Unity.Netcode.GameObjects.Timing.NetworkTimeSystem)
197+
- [NetworkManager](../components/core/networkmanager.md)
198+
- [NetworkTransform](../components/helper/networktransform.md)
199+
- [NetworkVariable](../basics/networkvariable.md)
200+
- [Remote procedure calls (RPCs)](message-system/rpc.md)

0 commit comments

Comments
 (0)