Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ClientDriven initial position sync fix on owning clients #85

Merged
merged 3 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ PrefabInstance:
propertyPath: GroundLayers.m_Bits
value: 640
objectReference: {fileID: 0}
- target: {fileID: 4416926081852918491, guid: 64dce48905ffd9b4293e595fa6941544, type: 3}
propertyPath: m_Enabled
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4416926081852918493, guid: 64dce48905ffd9b4293e595fa6941544, type: 3}
propertyPath: m_Actions
value:
Expand Down
14 changes: 5 additions & 9 deletions Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void Awake()
// ghost clients.
m_ThirdPersonController.enabled = false;
m_CapsuleCollider.enabled = false;
m_CharacterController.enabled = false;
}

public override void OnNetworkSpawn()
Expand All @@ -63,6 +64,10 @@ public override void OnNetworkSpawn()
// player input is only enabled on owning players
m_PlayerInput.enabled = true;
m_ThirdPersonController.enabled = true;

// see the note inside ServerPlayerMove why this step is also necessary for synchronizing initial player
// position on owning clients
m_CharacterController.enabled = true;

var cinemachineVirtualCamera = FindObjectOfType<CinemachineVirtualCamera>();
cinemachineVirtualCamera.Follow = m_CameraFollow;
Expand Down Expand Up @@ -99,13 +104,4 @@ void OnPickUp()
}
}
}

[ClientRpc]
public void SetSpawnClientRpc(Vector3 position, ClientRpcParams clientRpcParams = default)
{
m_CharacterController.enabled = false;
transform.position = position;
m_CharacterController.enabled = true;
gameObject.SetActive(true);
}
}
19 changes: 12 additions & 7 deletions Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
[DefaultExecutionOrder(0)] // before client component
public class ServerPlayerMove : NetworkBehaviour
{
[SerializeField]
ClientPlayerMove m_ClientPlayerMove;

public NetworkVariable<bool> isObjectPickedUp = new NetworkVariable<bool>();

NetworkObject m_PickedUpObject;
Expand All @@ -23,24 +20,32 @@ public class ServerPlayerMove : NetworkBehaviour
// DOC START HERE
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!IsServer)
{
enabled = false;
return;
}

OnServerSpawnPlayer();

base.OnNetworkSpawn();
}

void OnServerSpawnPlayer()
{
// this is done server side, so we have a single source of truth for our spawn point list
var spawnPoint = ServerPlayerSpawnPoints.Instance.ConsumeNextSpawnPoint();
var spawnPosition = spawnPoint ? spawnPoint.transform.position : Vector3.zero;
// using client RPC since ClientNetworkTransform can only be modified by owner (which is client side)
m_ClientPlayerMove.SetSpawnClientRpc(spawnPosition,
new ClientRpcParams() { Send = new ClientRpcSendParams() { TargetClientIds = new []{OwnerClientId}}});
transform.position = spawnPosition;

// A note specific to owner authority:
// Side Note: Specific to Owner Authoritative
// Setting the position works as and can be set in OnNetworkSpawn server-side unless there is a
// CharacterController that is enabled by default on the authoritative side. With CharacterController, it
// needs to be disabled by default (i.e. in Awake), the server applies the position (OnNetworkSpawn), and then
// the owner of the NetworkObject should enable CharacterController during OnNetworkSpawn. Otherwise,
// CharacterController will initialize itself with the initial position (before synchronization) and updates the
// transform after synchronization with the initial position, thus overwriting the synchronized position.
}

[ServerRpc]
Expand Down