I’m working on a feature where players are able to pick up a dolly and move boxes around with it.
This works fine as a host:
But has a very delayed feel as a client:
This is the code I’m using on the dolly object.
[ServerRpc(RequireOwnership = false)]
private void UseServerRpc(ServerRpcParams rpcParams = default)
{
if (IsBeingUsed)
{
Debug.LogWarning($"Player {_player.Value.AsPlayer()} tried to use the dolly, but it is already being used by {_player.Value.AsPlayer()}");
return;
}
var player = rpcParams.Receive.GetPlayerController();
_player.Value = player.gameObject;
NetworkObject.ChangeOwnership(player.NetworkClientId);
NetworkObject.TrySetParent(player.gameObject);
GetComponent<Rigidbody>().isKinematic = true;
transform.SetPositionAndRotation(player.DollyTransform.position, player.DollyTransform.rotation);
}
[ServerRpc]
private void StopUsingServerRpc(ServerRpcParams rpcParams = default)
{
if (!IsBeingUsed)
{
Debug.LogWarning($"Player {_player.Value.AsPlayer()} tried to stop using the dolly, but it is not being used");
return;
}
_player.MakeInvalid();
GetComponent<Rigidbody>().isKinematic = false;
NetworkObject.TryRemoveParent();
}
Would love to hear what I’m doing wrong, and/or even hearing alternative approaches on how to do this