In this lesson, you will learn how to shift the "right to control" a networked entity from the server to a specific client to enable seamless, player-driven interaction with world objects.
The Concept of Ownership
In a networked environment, "Ownership" determines which peer is responsible for the state updates of a specific NetworkObject. By default, the server usually owns all objects it spawns. However, for interactive entities like a vehicle, a portable tool, or a player character, you often need to transfer this ownership to a specific client. Once ownership is transferred, that client becomes the authoritative source for that object's interactions, reducing the overhead of sending every input request back to the server.
Implementing Authority Transfers
To transfer ownership, you must request a change through the NetworkObject component. When a client wants to "pick up" an object, they request ownership from the server. The server then validates this request—ensuring, for example, that the player is actually close enough to the object—before approving the transfer.
Consider this logic for a player interacting with an interactable object:
public void TryTakeOwnership(NetworkObject targetObject)
{
// The server validates if the player is allowed to take the object
if (IsServer)
{
targetObject.ChangeOwnership(OwnerClientId);
}
else
{
// Clients request the server to initiate the change
RequestOwnershipServerRpc();
}
}
[ServerRpc]
private void RequestOwnershipServerRpc(ServerRpcParams rpcParams = default)
{
// Validate logic (e.g., distance check) here
myNetworkObject.ChangeOwnership(rpcParams.Receive.SenderClientId);
}
Why Ownership Matters
When a client owns an object, they can perform actions on it—like toggling a switch or moving an object—without waiting for a round-trip confirmation from the server for every frame of input. This makes interactions feel snappy and responsive. Once the player drops the object or exits the interaction range, you simply transfer ownership back to the server or the next player. This granular control is essential for preventing "ghosting" effects or input conflicts where two players might attempt to control the same interactable object simultaneously.
Key Takeaway: Ownership transfer shifts the responsibility of state authority to a specific client, enabling responsive, player-driven interaction while maintaining server-side validation to ensure game integrity.