|
| 1 | +# Hybrid objects (GhostObjects) |
| 2 | + |
| 3 | +Understand how to use GhostObjects from Unity Netcode to support client-side prediction. |
| 4 | + |
| 5 | +GhostObjects are a feature of [Unity Netcode](https://docs.unity3d.com/Packages/com.unity.netcode@latest) that let you build networked gameplay with GameObjects and MonoBehaviours, while Unity Netcode handles replication, prediction, and interpolation for you. By adding a GhostObject component to the root of a GameObject prefab, you can use Unity Netcode to synchronize the transform of the GameObject and its children, and optionally synchronize the state of its Rigidbody component. You can also use GhostObjects to implement client-side prediction for your networked gameplay. |
| 6 | + |
| 7 | +A hybrid object is a GameObject that has both a [NetworkObject](components/core/networkobject.md) component and a [GhostObject](https://docs.unity3d.com/Packages/com.unity.netcode@latest?subfolder=/manual/ghostobject/ghostobject.html) component. Unity Netcode synchronizes the transform of a hybrid object as part of its snapshot, which makes the object compatible with prediction in Unity Netcode. Netcode for GameObjects continues to synchronize the data in your [`NetworkBehaviour`](xref:Unity.Netcode.NetworkBehaviour)s, such as [`NetworkVariable`](networkvariables-landing.md)s and RPCs. |
| 8 | + |
| 9 | +When the network prefabs list contains a hybrid object, Netcode for GameObjects uses Unity Netcode to drive the connection, and Unity Netcode drives the physics system. The **Parent Replication** setting on the GhostObject component controls how you set the parent GameObject of a hybrid object. Hybrid objects have some [limitations](#hybrid-object-limitations), including incompatibility with the distributed authority topology, in-scene placed objects, and object pooling. |
| 10 | + |
| 11 | +To create a hybrid object, add a GhostObject component to a NetworkObject prefab, or a NetworkObject component to a GhostObject prefab. |
| 12 | + |
| 13 | +## How hybrid objects synchronize data |
| 14 | + |
| 15 | +A hybrid object synchronizes its data through two separate systems: |
| 16 | + |
| 17 | +- Unity Netcode synchronizes the transform of the hybrid object in its snapshot. If you enable **Replicate Rigidbody** on the **GhostObject** component, Unity Netcode also synchronizes the state of the Rigidbody component. Unity enables this option by default. |
| 18 | +- Netcode for GameObjects synchronizes the data in the hybrid object's `NetworkBehaviour`s, including `NetworkVariable`s, RPCs, and spawn data. Netcode for GameObjects sends this data separately from the Unity Netcode snapshot. |
| 19 | + |
| 20 | +Because Netcode for GameObjects data isn't part of the snapshot, Unity Netcode doesn't roll back or replay `NetworkVariable`s, RPCs, or spawn data as part of its prediction loop. To use prediction on a hybrid object, write your prediction code in `GhostBehaviour`s. Prediction code in `GhostBehaviour`s on hybrid objects works the same way as on any other GhostObject. |
| 21 | + |
| 22 | +When you convert a NetworkObject into a hybrid object, Netcode for GameObjects sets the GhostObject's supported ghost modes to interpolated only. To use prediction, change the `SupportedGhostModes` and `DefaultGhostMode` settings on the **GhostObject** component to include a predicted mode. For more information about ghost modes, refer to the [Unity Netcode documentation](https://docs.unity3d.com/Packages/com.unity.netcode@latest). |
| 23 | + |
| 24 | +### Effect on existing transform and physics components |
| 25 | + |
| 26 | +When you convert a NetworkObject into a hybrid object, any existing [NetworkTransform](components/helper/networktransform.md) and [NetworkRigidbody](components/helper/networkrigidbody.md) components stay on the prefab so that you don't lose their settings. However, they no longer synchronize data: |
| 27 | + |
| 28 | +- A NetworkTransform component on a hybrid object has no effect, because Unity Netcode always synchronizes the transform. |
| 29 | +- A NetworkRigidbody component on a hybrid object has no effect when you enable **Replicate Rigidbody** on the **GhostObject** component. |
| 30 | + |
| 31 | +If you convert the hybrid object back into a NetworkObject, these components resume synchronizing data with their existing settings. |
| 32 | + |
| 33 | +## How Netcode for GameObjects uses Unity Netcode |
| 34 | + |
| 35 | +When the network prefabs list on the [NetworkManager](components/core/networkmanager.md) contains a hybrid object, Netcode for GameObjects configures Unity Netcode and uses it to drive the connection. This changes how Netcode for GameObjects handles snapshot size and physics. |
| 36 | + |
| 37 | +### Hybrid object requirements |
| 38 | + |
| 39 | +Hybrid objects have the following requirements: |
| 40 | + |
| 41 | +- A global `NetcodeConfig` asset. Unity Netcode creates this asset by default. If for some reason no global `NetcodeConfig` asset exists, the session fails to start and Netcode for GameObjects logs an error. |
| 42 | +- The `HostWorldModeSelection` setting of the global `NetcodeConfig` asset set to `SingleWorld`. If you use any other host world mode, the session fails to start and Netcode for GameObjects logs an error. |
| 43 | + |
| 44 | +### Snapshot size |
| 45 | + |
| 46 | +By default, Netcode for GameObjects configures the Unity Netcode snapshot size for you. To configure the snapshot size yourself in the `NetcodeConfig` asset, disable **Auto Configure Snapshot Size** in the **Hybrid Settings** section of the **NetworkManager** component. |
| 47 | + |
| 48 | +### Physics simulation rate |
| 49 | + |
| 50 | +When the network prefabs list contains a hybrid object, Unity Netcode drives the physics system. The physics system runs at the rate set in **Tick Rate** in the **Network Settings** section of the **NetworkManager** component. Unity Netcode uses this value instead of the **Simulation Tick Rate** value in the `ClientServerTickRate` settings of the `NetcodeConfig` asset. |
| 51 | + |
| 52 | +For more information about tick rates in Netcode for GameObjects, refer to [Ticks and update rates](learn/ticks-and-update-rates.md). |
| 53 | + |
| 54 | +## Parent replication |
| 55 | + |
| 56 | +The **Parent Replication** setting on the **GhostObject** component controls which package synchronizes the parent GameObject of a hybrid object. |
| 57 | + |
| 58 | +Unity disables **Parent Replication** by default. When you disable it, Netcode for GameObjects synchronizes parenting through its [NetworkObject parenting](advanced-topics/networkobject-parenting.md) methods, with the following behavior: |
| 59 | + |
| 60 | +- Setting a parent with `worldPositionStays` set to `false` works as expected. |
| 61 | +- Setting a parent with `worldPositionStays` set to `true` has no effect, and Netcode for GameObjects logs an error. |
| 62 | + |
| 63 | +When you enable **Parent Replication**, Unity Netcode synchronizes parenting, and you set the parent GameObject of a hybrid object in the same way as for any other GameObject. The NetworkObject parenting methods, such as [`NetworkObject.TrySetParent`](xref:Unity.Netcode.NetworkObject.TrySetParent*), have no effect, and Netcode for GameObjects logs an error if you call them. |
| 64 | + |
| 65 | +## Hybrid object limitations |
| 66 | + |
| 67 | +Hybrid objects have the following limitations: |
| 68 | + |
| 69 | +- Distributed authority: Hybrid objects aren't compatible with the [distributed authority](terms-concepts/distributed-authority.md) topology. If the network prefabs list contains a hybrid object, a distributed authority session fails to start. |
| 70 | +- In-scene placed objects: [In-scene placed NetworkObjects](basics/scenemanagement/inscene-placed-networkobjects.md) can't be hybrid objects. |
| 71 | +- Object pooling: Hybrid objects aren't compatible with `INetworkPrefabInstanceHandler`, so you can't use [object pooling](advanced-topics/object-pooling.md) with hybrid objects. |
| 72 | +- Client authority: Unity Netcode doesn't support the client authoritative motion model. You can convert a NetworkObject that uses an owner-authoritative NetworkTransform into a hybrid object, but the owner no longer controls its transform, because the NetworkTransform component stops synchronizing data. To move the converted object, use server-authoritative motion or prediction instead. `NetworkVariable`s on hybrid objects can still use owner write permissions. |
| 73 | + |
| 74 | +## Create and remove hybrid objects |
| 75 | + |
| 76 | +To create a hybrid object, add the missing component to an existing network prefab: |
| 77 | + |
| 78 | +- To convert a NetworkObject prefab into a hybrid object, select **Add Component** in the prefab's **Inspector** window and add a **GhostObject** component. |
| 79 | +- To convert a GhostObject prefab into a hybrid object, select **Add Component** in the prefab's **Inspector** window and add a **NetworkObject** component. |
| 80 | + |
| 81 | +To convert a hybrid object back into a prefab that uses only one package, remove one of its components: |
| 82 | + |
| 83 | +- To keep only Netcode for GameObjects synchronization, remove the **GhostObject** component. |
| 84 | +- To keep only Unity Netcode synchronization, remove the **NetworkObject** component. |
| 85 | + |
| 86 | +> [!NOTE] |
| 87 | +> Unity might prevent you from removing a **GhostObject** or **NetworkObject** component while the GameObject has `GhostBehaviour` or `NetworkBehaviour` components that depend on it. Remove the dependent components first. |
| 88 | +
|
| 89 | +## Additional resources |
| 90 | + |
| 91 | +- [GhostObjects in Unity Netcode](https://docs.unity3d.com/Packages/com.unity.netcode@latest?subfolder=/manual/ghostobject/ghostobject.html) |
| 92 | +- [Unity Netcode documentation](https://docs.unity3d.com/Packages/com.unity.netcode@latest) |
| 93 | +- [NetworkObject](components/core/networkobject.md) |
| 94 | +- [NetworkObject parenting](advanced-topics/networkobject-parenting.md) |
| 95 | +- [Physics](advanced-topics/physics.md) |
0 commit comments