Skip to content
This repository was archived by the owner on Jul 23, 2025. It is now read-only.

Commit 173eb71

Browse files
authored
New page for PlayerObjects (#1336)
1 parent 91f681d commit 173eb71

File tree

4 files changed

+134
-75
lines changed

4 files changed

+134
-75
lines changed

docs/basics/networkobject.md

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Netcode for GameObjects' high level components, [the RPC system](../advanced-top
1010
1. NetworkObject
1111
2. [`NetworkBehaviour`](networkbehaviour.md)
1212

13-
NetworkObjects require the use of specialized [`NetworkObjectReference`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkObjectReference.html) structures before you can serialize and use them with RPCs and `NetworkVariable`s.
13+
NetworkObjects require the use of specialized [`NetworkObjectReference`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkObjectReference.html) structures before you can serialize and use them with RPCs and `NetworkVariable`s
14+
15+
Netcode for GameObjects also has [PlayerObjects](playerobjects.md), an optional feature that you can use to assign a NetworkObject to a specific client.
1416

1517
## Using NetworkObjects
1618

@@ -34,6 +36,8 @@ You can avoid execution order issues in any NetworkBehaviour component scripts t
3436

3537
Either the server (default) or any connected and approved client owns each NetworkObject. By default, Netcode for GameObjects is [server-authoritative](../terms-concepts/client-server.md), which means only the server can spawn and despawn NetworkObjects, but you can also build [distributed authority](../terms-concepts/distributed-authority.md) games where clients have the authority to spawn and despawn NetworkObjects as well.
3638

39+
If you're creating a client-server game and you want a client to control more than one NetworkObject, use the following ownership methods.
40+
3741
The default `NetworkObject.Spawn` method assumes server-side ownership:
3842

3943
```csharp
@@ -68,71 +72,6 @@ When you want to despawn and destroy the owner but you don't want to destroy a s
6872

6973
:::
7074

71-
## Player NetworkObjects
72-
73-
Player objects are an optional feature in Netcode for GameObjects that you can use to assign a networked object to a specific client. A client can only have at most one player object.
74-
75-
If you want a client to control more than one NetworkObject, use the ownership methods described above under the ownership section.
76-
77-
If you want to be able to assign a unique player prefab on a per-client connection basis, use client [connection approval](connection-approval.md).
78-
79-
### Creating a PlayerObject
80-
81-
Netcode for GameObjects can spawn a default PlayerObject for you. If you enable **Create Player Prefab** (true) in the NetworkManager and assign a valid prefab, then Netcode for GameObjects spawns a unique instance of the designated player prefab for each connected and approved client.
82-
83-
To manually spawn an object as PlayerObject, use the following method:
84-
85-
```csharp
86-
GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
87-
```
88-
89-
If the player already had a prefab instance assigned, then the client owns the NetworkObject of that prefab instance unless there's additional server-side specific user code that removes or changes the ownership.
90-
91-
### Defining defaults for PlayerObjects
92-
93-
If you're using `UnityEngine.InputSystem.PlayerInput` or `UnityEngine.PhysicsModule.CharacterController` components on your player prefab(s), you should disable them by default and only enable them for the local client's PlayerObject. Otherwise, you may get events from the most recently instantiated player prefab instance, even if it isn't the local client instance.
94-
95-
You can disable these components in the **Inspector** view on the prefab itself, or disable them during `Awake` in one of your `NetworkBehaviour` components. Then you can enable the components only on the owner's instance using code like the example below:
96-
97-
```
98-
PlayerInput m_PlayerInput;
99-
private void Awake()
100-
{
101-
m_PlayerInput = GetComponent<PlayerInput>();
102-
m_PlayerInput.enabled = false;
103-
}
104-
105-
public override void OnNetworkSpawn()
106-
{
107-
m_PlayerInput.enabled = IsOwner;
108-
base.OnNetworkSpawn();
109-
}
110-
111-
public override void OnNetworkDespawn()
112-
{
113-
m_PlayerInput.enabled = false;
114-
base.OnNetworkDespawn();
115-
}
116-
```
117-
118-
### Finding PlayerObjects
119-
120-
To find a PlayerObject for a specific client ID, you can use the following methods:
121-
122-
Within a NetworkBehaviour, you can check the local `NetworkManager.LocalClient` to get the local PlayerObjects:
123-
124-
```csharp
125-
NetworkManager.LocalClient.PlayerObject
126-
```
127-
128-
Conversely, on the server-side, if you need to get the PlayerObject instance for a specific client, you can use the following:
129-
130-
```csharp
131-
NetworkManager.Singleton.ConnectedClients[clientId].PlayerObject;
132-
```
133-
134-
To find your own player object just pass `NetworkManager.Singleton.LocalClientId` as the `clientId` in the sample above.
135-
13675
## Network prefabs
13776

13877
Network prefabs are registered to a `NetworkPrefabsList` object (a type of `ScriptableObject`). By default, a default prefabs list containing every network prefab in your project.
@@ -178,3 +117,9 @@ Similar to `NetworkObject.ActiveSceneSynchronization`, this property automatical
178117
:::info
179118
`NetworkObject.ActiveSceneSynchronization` can be used with `NetworkObject.SceneMigrationSynchronization` as long as you take into consideration that if you migrate a NetworkObject into a non-active scene via `SceneManager.MoveGameObjectToScene` and then later change the active scene, then the NetworkObject instance will be automatically migrated to the newly set active scene.
180119
:::
120+
121+
## Additional resources
122+
123+
- [PlayerObjects and player prefabs](playerobjects.md)
124+
- [NetworkBehaviour](networkbehaviour.md)
125+
- [NetworkVariable](networkvariable.md)

docs/basics/playerobjects.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
id: playerobjects
3+
title: PlayerObjects and player prefabs
4+
---
5+
6+
PlayerObjects are an optional feature in Netcode for GameObjects that you can use to assign a [NetworkObject](networkobject.md) to a specific client. A client can only have one PlayerObject.
7+
8+
PlayerObjects are instantiated by reference to a player prefab, which defines the characteristics of the PlayerObject.
9+
10+
## Defining defaults for player prefabs
11+
12+
If you're using `UnityEngine.InputSystem.PlayerInput` or `UnityEngine.PhysicsModule.CharacterController` components on your player prefab(s), you should disable them by default and only enable them for the local client's PlayerObject. Otherwise, you may get events from the most recently instantiated player prefab instance, even if it isn't the local client instance.
13+
14+
You can disable these components in the **Inspector** view on the prefab itself, or disable them during `Awake` in one of your `NetworkBehaviour` components. Then you can enable the components only on the owner's instance using code like the example below:
15+
16+
```
17+
PlayerInput m_PlayerInput;
18+
private void Awake()
19+
{
20+
m_PlayerInput = GetComponent<PlayerInput>();
21+
m_PlayerInput.enabled = false;
22+
}
23+
24+
public override void OnNetworkSpawn()
25+
{
26+
m_PlayerInput.enabled = IsOwner;
27+
base.OnNetworkSpawn();
28+
}
29+
30+
public override void OnNetworkDespawn()
31+
{
32+
m_PlayerInput.enabled = false;
33+
base.OnNetworkDespawn();
34+
}
35+
```
36+
37+
## Spawning PlayerObjects
38+
39+
## Session-mode agnostic methods
40+
41+
Netcode for GameObjects can spawn a default PlayerObject for you. If you enable **Create Player Prefab** in the [NetworkManager](../components/networkmanager.md) and assign a valid prefab, then Netcode for GameObjects spawns a unique instance of the designated player prefab for each connected and approved client, referred to as the PlayerObject.
42+
43+
To manually spawn an object as PlayerObject, use the following method:
44+
45+
```csharp
46+
GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
47+
```
48+
49+
If the player already had a prefab instance assigned, then the client owns the NetworkObject of that prefab instance unless there's additional server-side specific user code that removes or changes the ownership.
50+
51+
Alternatively, you can choose not to spawn anything immediately after a client connects and instead use a [NetworkBehaviour component](networkbehaviour.md) to handle avatar/initial player prefab selection. This NetworkBehaviour component could be configured by the server or initiating session owner, or be associated with an [in-scene](scenemanagement/inscene-placed-networkobjects.md) or [dynamically spawned](object-spawning.md#dynamically-spawned-network-prefabs) NetworkObject, as suits the needs of your project.
52+
53+
### Client-server contexts only
54+
55+
In addition to the [session-mode agnostic spawning methods](#session-mode-agnostic-methods) above, you can assign a unique player prefab on a per-client connection basis using a client [connection approval process](connection-approval.md) when in [client-server contexts](../terms-concepts/client-server.md).
56+
57+
### Distributed authority contexts only
58+
59+
In addition to the [session-mode agnostic spawning methods](#session-mode-agnostic-methods) above, you can use the [`OnFetchLocalPlayerPrefabToSpawn`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkManager.html#Unity_Netcode_NetworkManager_OnFetchLocalPlayerPrefabToSpawn) method to assign a unique player prefab on a per-client basis when in [distributed authority contexts](../terms-concepts/distributed-authority.md).
60+
61+
To use `OnFetchLocalPlayerPrefabToSpawn` in your project, assign a callback handler to `OnFetchLocalPlayerPrefabToSpawn` and whatever the client script returns is what will be spawned for that client. Ensure that the prefab being spawned is in a NetworkPrefabList [registered with the NetworkManager](object-spawning.md#registering-a-network-prefab).
62+
63+
If you don't assign a callback handler to `OnFetchLocalPlayerPrefabToSpawn`, then the default behaviour is to return the `NetworkConfig.PlayerPrefab` (or null if neither are set).
64+
65+
:::note `AutoSpawnPlayerPrefabClientSide` required
66+
For `OnFetchLocalPlayerPrefabToSpawn` to work, [`AutoSpawnPlayerPrefabClientSide`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkManager.html#Unity_Netcode_NetworkManager_AutoSpawnPlayerPrefabClientSide) must be enabled.
67+
:::
68+
69+
## PlayerObject spawning timeline
70+
71+
When using automatic methods of PlayerObject spawning (such as enabling **Create Player Prefab** in the [NetworkManager](../components/networkmanager.md)), PlayerObjects are spawned at different times depending on whether you have [scene management](scenemanagement/scene-management-overview.md) enabled.
72+
73+
- When scene management is disabled, PlayerObjects are spawned when a joining client's connection is approved.
74+
- When scene management is enabled, PlayerObjects are spawned when a joining client finishes initial synchronization.
75+
76+
If you choose not to automatically spawn a PlayerObject when a client joins, then the timing of when a PlayerObject is spawned is up to you based on your own implemented code.
77+
78+
## Finding PlayerObjects
79+
80+
To find a PlayerObject for a specific client ID, you can use the following methods:
81+
82+
Within a NetworkBehaviour, you can check the local `NetworkManager.LocalClient` to get the local PlayerObjects:
83+
84+
```csharp
85+
NetworkManager.LocalClient.PlayerObject
86+
```
87+
88+
Conversely, on the server-side, if you need to get the PlayerObject instance for a specific client, you can use the following:
89+
90+
```csharp
91+
NetworkManager.Singleton.ConnectedClients[clientId].PlayerObject;
92+
```
93+
94+
To find your own player object just pass `NetworkManager.Singleton.LocalClientId` as the `clientId` in the sample above.
95+
96+
## Additional resources
97+
98+
- [NetworkObject](networkobject.md)
99+
- [NetworkManager](../components/networkmanager.md)
100+
- [Distributed authority topologies](../terms-concepts/distributed-authority.md)
101+
- [Client-server topologies](../terms-concepts/client-server.md)
102+
- [Object spawning](objectspawning.md)

docs/components/networkmanager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The `NetworkManager` is a required Netcode for GameObjects component that has al
88
### `NetworkManager` Inspector properties
99

1010
- **LogLevel**: Sets the network logging level
11-
- **PlayerPrefab**: When a prefab is assigned, the prefab will be instantiated as the player object and assigned to the newly connected and authorized client. For more information about player prefabs, refer to [Player NetworkObjects](../basics/networkobject.md#player-networkobjects).
11+
- **PlayerPrefab**: When a prefab is assigned, the prefab will be instantiated as the PlayerObject and assigned to the newly connected and authorized client. For more information about player prefabs, refer to [PlayerObjects and player prefabs](../basics/playerobjects.md).
1212
- **NetworkPrefabs**: Where you register your network prefabs. You can also create a single network prefab override per registered network prefab here.
1313
- **Protocol Version**: Set this value to help distinguish between builds when the most current build has new assets that can cause issues with older builds connecting.
1414
- **Network Transport**: Where your network specific settings and transport type is set. This field accepts any INetworkTransport implementation. However, unless you have unique transport specific needs UnityTransport is the recommended transport to use with Netcode for GameObjects.

sidebars.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,26 @@ module.exports = {
123123
"type": "category",
124124
"label": "Networking components",
125125
"items": [
126-
{
127-
"type": "doc",
128-
"id": "basics/networkobject"
129-
},
130-
{
131-
"type": "doc",
132-
"id": "advanced-topics/networkobject-parenting"
133-
},
126+
{
127+
"collapsed": true,
128+
"type": "category",
129+
"label": "NetworkObject",
130+
"items": [
131+
{
132+
"type": "doc",
133+
"id": "basics/networkobject"
134+
},
135+
{
136+
"type": "doc",
137+
"id": "basics/playerobjects"
138+
},
139+
{
140+
"type": "doc",
141+
"id": "advanced-topics/networkobject-parenting"
142+
},
143+
144+
]
145+
},
134146
{
135147
"collapsed": true,
136148
"type": "category",

0 commit comments

Comments
 (0)