From 4550bd8847acf79528e2ff6f68e1e06f4125cddd Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Tue, 13 Dec 2022 13:08:32 -0500 Subject: [PATCH 1/3] removing clientrpc for setting position on spawn, disabling charactercontroller on clients --- .../Assets/Prefabs/PlayerArmature_Networked.prefab | 4 ++++ Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs | 11 ++--------- Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs | 10 +++------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Basic/ClientDriven/Assets/Prefabs/PlayerArmature_Networked.prefab b/Basic/ClientDriven/Assets/Prefabs/PlayerArmature_Networked.prefab index e1c8d36be..bdfab96bf 100644 --- a/Basic/ClientDriven/Assets/Prefabs/PlayerArmature_Networked.prefab +++ b/Basic/ClientDriven/Assets/Prefabs/PlayerArmature_Networked.prefab @@ -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: diff --git a/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs b/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs index 516ba6445..fe1c5111e 100644 --- a/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs +++ b/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs @@ -45,6 +45,7 @@ void Awake() // ghost clients. m_ThirdPersonController.enabled = false; m_CapsuleCollider.enabled = false; + m_CharacterController.enabled = false; } public override void OnNetworkSpawn() @@ -63,6 +64,7 @@ public override void OnNetworkSpawn() // player input is only enabled on owning players m_PlayerInput.enabled = true; m_ThirdPersonController.enabled = true; + m_CharacterController.enabled = true; var cinemachineVirtualCamera = FindObjectOfType(); cinemachineVirtualCamera.Follow = m_CameraFollow; @@ -99,13 +101,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); - } } diff --git a/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs b/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs index 04853763e..790617d5e 100644 --- a/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs +++ b/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs @@ -10,9 +10,6 @@ [DefaultExecutionOrder(0)] // before client component public class ServerPlayerMove : NetworkBehaviour { - [SerializeField] - ClientPlayerMove m_ClientPlayerMove; - public NetworkVariable isObjectPickedUp = new NetworkVariable(); NetworkObject m_PickedUpObject; @@ -23,7 +20,6 @@ public class ServerPlayerMove : NetworkBehaviour // DOC START HERE public override void OnNetworkSpawn() { - base.OnNetworkSpawn(); if (!IsServer) { enabled = false; @@ -31,6 +27,8 @@ public override void OnNetworkSpawn() } OnServerSpawnPlayer(); + + base.OnNetworkSpawn(); } void OnServerSpawnPlayer() @@ -38,9 +36,7 @@ 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; } [ServerRpc] From f58f9311e9259f29bf8e94353c45ff75f17ca130 Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Tue, 13 Dec 2022 15:01:49 -0500 Subject: [PATCH 2/3] adding more detailed comments --- Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs | 3 +++ Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs b/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs index fe1c5111e..e8c9706e6 100644 --- a/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs +++ b/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs @@ -64,6 +64,9 @@ 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(); diff --git a/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs b/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs index 790617d5e..94d68aba5 100644 --- a/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs +++ b/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs @@ -37,6 +37,15 @@ void OnServerSpawnPlayer() var spawnPoint = ServerPlayerSpawnPoints.Instance.ConsumeNextSpawnPoint(); var spawnPosition = spawnPoint ? spawnPoint.transform.position : Vector3.zero; 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] From c705a67b5b20337de93f10b53c56e327bf2bbc8a Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Tue, 13 Dec 2022 15:21:34 -0500 Subject: [PATCH 3/3] changelog addition --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 162716231..ff8d30ffa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - Upgrade to Netcode for GameObjects v1.2.0 & cleaned up in-scene NetworkVariables (#78) - Ingredient spawn position offset (#81) - In-game UI backgrounds (#82) +- Initial position sync fix on owning clients (#85) ### 2DSpaceShooter