From 6b2f6486abbc9de1162d70fa1cc89e41eb7c789f Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Thu, 24 Sep 2026 10:34:48 -0500 Subject: [PATCH 1/8] fix Fixing issue where a `NetworkObject` with nested `NetworkTransform` components using different `NetworkTransform.AuthorityMode` settings stop updating non-authority instances, because the first authority instance removes the entire `NetworkObject` from the update registration. --- .../Runtime/Components/NetworkTransform.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs b/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs index 46b7ef24e5..5f5e89ccbd 100644 --- a/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs +++ b/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs @@ -3715,6 +3715,28 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState() m_ScaleInterpolator.ResetTo(transform.parent, transform.localScale, serverTime); } + /// + /// Determines whether any on this is non-authority. + /// + /// + /// Authority is derived the same way derives it rather than read from + /// , so the result does not depend upon the order in which the nested + /// components are initialized. + /// + private bool HasNonAuthorityNetworkTransform() + { + var networkTransforms = NetworkObject.NetworkTransforms; + for (int i = 0; i < networkTransforms.Count; i++) + { + var networkTransform = networkTransforms[i]; + if (!(networkTransform.IsServerAuthoritative() ? networkTransform.IsServer : networkTransform.IsOwner)) + { + return true; + } + } + return false; + } + /// /// The internal initialization method to allow for internal API adjustments /// @@ -3777,8 +3799,13 @@ private void InternalInitialization(bool isOwnershipChange = false) if (CanCommitToTransform) { - // Make sure authority doesn't get added to updates (no need to do this on the authority side) - m_CachedNetworkManager.NetworkTransformRegistration(NetworkObject, forUpdate, false); + // Make sure authority doesn't get added to updates (no need to do this on the authority side), but the + // registration is per-NetworkObject while the authority motion model is per-NetworkTransform. Nested + // instances with the inverted authority mode still need the update. + if (!HasNonAuthorityNetworkTransform()) + { + m_CachedNetworkManager.NetworkTransformRegistration(NetworkObject, forUpdate, false); + } if (UseHalfFloatPrecision) { m_HalfPositionState = new NetworkDeltaPosition(currentPosition, m_CachedNetworkManager.ServerTime.Tick, math.bool3(SyncPositionX, SyncPositionY, SyncPositionZ)); From e98ed18b91cfcce2e668c37b069a37ef75e049e2 Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Thu, 24 Sep 2026 10:35:05 -0500 Subject: [PATCH 2/8] test The tests that validate this fix. --- .../NetworkTransformMixedAuthorityTests.cs | 92 +++++++++++++------ 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs index 760e916bda..ab6ef475e5 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Text; +using NUnit.Framework; using Unity.Netcode.Components; using Unity.Netcode.TestHelpers.Runtime; using UnityEngine; @@ -7,6 +8,10 @@ namespace Unity.Netcode.RuntimeTests { + [TestFixture(HostOrServer.Host, NetworkTransform.AuthorityModes.Server)] + [TestFixture(HostOrServer.Host, NetworkTransform.AuthorityModes.Owner)] + [TestFixture(HostOrServer.Server, NetworkTransform.AuthorityModes.Server)] + [TestFixture(HostOrServer.Server, NetworkTransform.AuthorityModes.Owner)] internal class NetworkTransformMixedAuthorityTests : IntegrationTestWithApproximation { private const float k_MotionMagnitude = 5.5f; @@ -14,64 +19,75 @@ internal class NetworkTransformMixedAuthorityTests : IntegrationTestWithApproxim protected override int NumberOfClients => 2; + private readonly NetworkTransform.AuthorityModes m_RootAuthorityMode; + private readonly NetworkTransform.AuthorityModes m_ChildAuthorityMode; + private StringBuilder m_ErrorMsg = new StringBuilder(); + public NetworkTransformMixedAuthorityTests(HostOrServer hostOrServer, NetworkTransform.AuthorityModes rootAuthorityMode) : base(hostOrServer) + { + m_RootAuthorityMode = rootAuthorityMode; + m_ChildAuthorityMode = rootAuthorityMode == NetworkTransform.AuthorityModes.Server ? NetworkTransform.AuthorityModes.Owner : NetworkTransform.AuthorityModes.Server; + } + protected override void OnCreatePlayerPrefab() { - m_PlayerPrefab.AddComponent(); + m_PlayerPrefab.AddComponent().AuthorityMode = m_RootAuthorityMode; var childGameObject = new GameObject(); childGameObject.transform.parent = m_PlayerPrefab.transform; var childNetworkTransform = childGameObject.AddComponent(); - childNetworkTransform.AuthorityMode = NetworkTransform.AuthorityModes.Owner; + childNetworkTransform.AuthorityMode = m_ChildAuthorityMode; childNetworkTransform.InLocalSpace = true; base.OnCreatePlayerPrefab(); } + /// + /// Returns the instance of 's player object that has authority over a + /// using the authority mode. + /// + private NetworkObject GetAuthorityInstance(NetworkManager player, NetworkTransform.AuthorityModes authorityMode) + { + var authority = authorityMode == NetworkTransform.AuthorityModes.Server ? m_ServerNetworkManager : player; + return authority.SpawnManager.SpawnedObjects[player.LocalClient.PlayerObject.NetworkObjectId]; + } + private void MovePlayers() { - foreach (var networkManager in m_NetworkManagers) + foreach (var networkManager in m_ClientNetworkManagers) { var direction = GetRandomVector3(-1.0f, 1.0f); - var playerObject = networkManager.LocalClient.PlayerObject; - var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; - // Server authoritative - var serverPlayerClone = m_ServerNetworkManager.SpawnManager.SpawnedObjects[playerObjectId]; - serverPlayerClone.transform.position += direction * k_MotionMagnitude; - // Owner authoritative - var childTransform = networkManager.LocalClient.PlayerObject.transform.GetChild(0); - childTransform.localPosition += direction * k_MotionMagnitude; + GetAuthorityInstance(networkManager, m_RootAuthorityMode).transform.position += direction * k_MotionMagnitude; + GetAuthorityInstance(networkManager, m_ChildAuthorityMode).transform.GetChild(0).localPosition += direction * k_MotionMagnitude; } } private bool AllInstancePositionsMatch() { m_ErrorMsg.Clear(); - foreach (var networkManager in m_NetworkManagers) + foreach (var networkManager in m_ClientNetworkManagers) { - var playerObject = networkManager.LocalClient.PlayerObject; var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; - var serverRootPosition = m_ServerNetworkManager.SpawnManager.SpawnedObjects[playerObjectId].transform.position; - var ownerChildPosition = networkManager.LocalClient.PlayerObject.transform.GetChild(0).localPosition; + var authorityRootPosition = GetAuthorityInstance(networkManager, m_RootAuthorityMode).transform.position; + var authorityChildPosition = GetAuthorityInstance(networkManager, m_ChildAuthorityMode).transform.GetChild(0).localPosition; + + // The authority instances are compared too, as an instance with authority over one nested + // NetworkTransform is still non-authority for the other. foreach (var client in m_NetworkManagers) { - if (client == networkManager) - { - continue; - } var playerClone = client.SpawnManager.SpawnedObjects[playerObjectId]; var cloneRootPosition = playerClone.transform.position; var cloneChildPosition = playerClone.transform.GetChild(0).localPosition; - if (!Approximately(serverRootPosition, cloneRootPosition)) + if (!Approximately(authorityRootPosition, cloneRootPosition)) { - m_ErrorMsg.AppendLine($"[{playerObject.name}][{playerClone.name}] Root mismatch ({GetVector3Values(serverRootPosition)})({GetVector3Values(cloneRootPosition)})!"); + m_ErrorMsg.AppendLine($"[Client-{client.LocalClientId}][{playerClone.name}] Root mismatch ({GetVector3Values(authorityRootPosition)})({GetVector3Values(cloneRootPosition)})!"); } - if (!Approximately(ownerChildPosition, cloneChildPosition)) + if (!Approximately(authorityChildPosition, cloneChildPosition)) { - m_ErrorMsg.AppendLine($"[{playerObject.name}][{playerClone.name}] Child mismatch ({GetVector3Values(ownerChildPosition)})({GetVector3Values(cloneChildPosition)})!"); + m_ErrorMsg.AppendLine($"[Client-{client.LocalClientId}][{playerClone.name}] Child mismatch ({GetVector3Values(authorityChildPosition)})({GetVector3Values(cloneChildPosition)})!"); } } } @@ -81,8 +97,8 @@ private bool AllInstancePositionsMatch() /// /// Client-Server Only /// Validates that mixed authority is working properly - /// Root -- Server Authoritative - /// |--Child -- Owner Authoritative + /// Root -- Server or Owner authoritative + /// |--Child -- The inverse of the root's authority mode /// [UnityTest] public IEnumerator MixedAuthorityTest() @@ -91,7 +107,31 @@ public IEnumerator MixedAuthorityTest() { MovePlayers(); yield return WaitForConditionOrTimeOut(AllInstancePositionsMatch); - AssertOnTimeout($"Transforms failed to synchronize!"); + AssertOnTimeout($"Transforms failed to synchronize!\n{m_ErrorMsg}"); + } + } + + /// + /// The update registration is per-NetworkObject while the authority motion model is per-NetworkTransform, + /// so an instance stays registered for as long as any one of its nested NetworkTransform components is + /// non-authority. + /// + [Test] + public void MixedAuthorityUpdateRegistration() + { + foreach (var networkManager in m_ClientNetworkManagers) + { + var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; + foreach (var client in m_NetworkManagers) + { + var playerClone = client.SpawnManager.SpawnedObjects[playerObjectId]; + var hasNonAuthority = false; + foreach (var networkTransform in playerClone.NetworkTransforms) + { + hasNonAuthority |= !networkTransform.CanCommitToTransform; + } + Assert.AreEqual(hasNonAuthority, client.NetworkTransformUpdate.ContainsKey(playerObjectId), $"[Client-{client.LocalClientId}][{playerClone.name}] Unexpected update registration!"); + } } } } From ff196a34bc569151090db35de00764712c570fa7 Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Thu, 24 Sep 2026 10:44:14 -0500 Subject: [PATCH 3/8] update Adding changelog entry --- com.unity.netcode.gameobjects/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 7247324fe1..32630a17e8 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -22,6 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed +- Issue with mixed authority nested `NetworkTransform` instances can stop child/nested `NetworkTransform` instances from updating due to a parent (root or otherwise) `NetworkTransform` that is the authority instance will remove the `NetworkObject` completely from the non-authority update group causing non-authority instances to never update their state (interpolating or not) on the authority side. (#4169) ### Security From bef6418aa2c02f6d6d79ba4bd280bf8e2d8bb3ca Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Thu, 24 Sep 2026 10:51:18 -0500 Subject: [PATCH 4/8] style Updating comments for clarity --- .../Runtime/Components/NetworkTransform.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs b/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs index 5f5e89ccbd..fe085e4670 100644 --- a/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs +++ b/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs @@ -3716,13 +3716,15 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState() } /// - /// Determines whether any on this is non-authority. + /// Returns whether any on this is non-authority. /// - /// - /// Authority is derived the same way derives it rather than read from - /// , so the result does not depend upon the order in which the nested - /// components are initialized. - /// + + private void Thing() { } + + /// + /// Determines if this has any instances that are non-authority. + /// + /// true if a non-authority NetworkTransform exists on this NetworkObject and false if there are none. private bool HasNonAuthorityNetworkTransform() { var networkTransforms = NetworkObject.NetworkTransforms; @@ -3799,9 +3801,8 @@ private void InternalInitialization(bool isOwnershipChange = false) if (CanCommitToTransform) { - // Make sure authority doesn't get added to updates (no need to do this on the authority side), but the - // registration is per-NetworkObject while the authority motion model is per-NetworkTransform. Nested - // instances with the inverted authority mode still need the update. + // If there are no non-authority NetworkTransform instances on this NetworkObject, then remove this instance from the NetworkManager's update list. + // Otherwise, we need to keep it registered for updates so the non-authority instances will process their received state updates and apply them to the transform. if (!HasNonAuthorityNetworkTransform()) { m_CachedNetworkManager.NetworkTransformRegistration(NetworkObject, forUpdate, false); From 1bef9547bb7da5ee779d0e433187d441940664a9 Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Thu, 24 Sep 2026 14:21:27 -0500 Subject: [PATCH 5/8] fix Addressing the u-pr bot's findings. --- .../Runtime/Components/NetworkTransform.cs | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs b/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs index fe085e4670..9ac503a926 100644 --- a/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs +++ b/com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs @@ -3716,21 +3716,29 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState() } /// - /// Returns whether any on this is non-authority. - /// - - private void Thing() { } - - /// - /// Determines if this has any instances that are non-authority. + /// Determines if this has any instances that are non-authority and are updated during the same update stage. /// + /// + /// See to better understand how the parameter is used to determine which update stage to check for non-authority instances. + /// + /// true to check the instances updated during the standard update and false to check the instances updated during the fixed update. /// true if a non-authority NetworkTransform exists on this NetworkObject and false if there are none. - private bool HasNonAuthorityNetworkTransform() + private bool HasNonAuthorityNetworkTransform(bool forUpdate) { var networkTransforms = NetworkObject.NetworkTransforms; for (int i = 0; i < networkTransforms.Count; i++) { var networkTransform = networkTransforms[i]; +#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D + // If the update stages don't match, then skip this instance. + // Reference: + // forUpdate is true for the standard update and false for the fixed update. + // m_UseRigidbodyForMotion is false for the standard update and true for the fixed update. + if (forUpdate == networkTransform.m_UseRigidbodyForMotion) + { + continue; + } +#endif if (!(networkTransform.IsServerAuthoritative() ? networkTransform.IsServer : networkTransform.IsOwner)) { return true; @@ -3801,9 +3809,9 @@ private void InternalInitialization(bool isOwnershipChange = false) if (CanCommitToTransform) { - // If there are no non-authority NetworkTransform instances on this NetworkObject, then remove this instance from the NetworkManager's update list. + // If there are no non-authority NetworkTransform instances on this NetworkObject using this update, then remove this instance from the NetworkManager's update list. // Otherwise, we need to keep it registered for updates so the non-authority instances will process their received state updates and apply them to the transform. - if (!HasNonAuthorityNetworkTransform()) + if (!HasNonAuthorityNetworkTransform(forUpdate)) { m_CachedNetworkManager.NetworkTransformRegistration(NetworkObject, forUpdate, false); } From 8920574314af8c8bd7d97234456c148d970b9b42 Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Thu, 24 Sep 2026 14:21:57 -0500 Subject: [PATCH 6/8] test Adding a test to validate the, now fixed, gap u-pr bot found. --- .../NetworkTransformMixedMotionModelTests.cs | 63 +++++++++++++++++++ ...workTransformMixedMotionModelTests.cs.meta | 2 + 2 files changed, 65 insertions(+) create mode 100644 com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs create mode 100644 com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs.meta diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs new file mode 100644 index 0000000000..bb4b0cf5ee --- /dev/null +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs @@ -0,0 +1,63 @@ +#if COM_UNITY_MODULES_PHYSICS +using System.Collections; +using NUnit.Framework; +using Unity.Netcode.Components; +using Unity.Netcode.TestHelpers.Runtime; +using UnityEngine; +using UnityEngine.TestTools; + +namespace Unity.Netcode.RuntimeTests +{ + internal class NetworkTransformMixedMotionModelTests : NetcodeIntegrationTest + { + protected override int NumberOfClients => 2; + + private GameObject m_MixedMotionModelPrefab; + + protected override void OnServerAndClientsCreated() + { + m_MixedMotionModelPrefab = CreateNetworkObjectPrefab("MixedMotionModel"); + + // The root is owner authoritative and driven by the rigidbody, which places it in the fixed update registration + var rootNetworkTransform = m_MixedMotionModelPrefab.AddComponent(); + rootNetworkTransform.AuthorityMode = NetworkTransform.AuthorityModes.Owner; + var rigidbody = m_MixedMotionModelPrefab.AddComponent(); + rigidbody.useGravity = false; + rigidbody.detectCollisions = false; + m_MixedMotionModelPrefab.AddComponent().UseRigidBodyForMotion = true; + + // The nested child is server authoritative and driven by the transform, which places it in the update registration + var childGameObject = new GameObject(); + childGameObject.transform.parent = m_MixedMotionModelPrefab.transform; + var childNetworkTransform = childGameObject.AddComponent(); + childNetworkTransform.AuthorityMode = NetworkTransform.AuthorityModes.Server; + childNetworkTransform.InLocalSpace = true; + + base.OnServerAndClientsCreated(); + } + + /// + /// A NetworkObject that mixes both the authority motion model and the rigidbody motion model has each nested + /// NetworkTransform registered under a different update. Gaining authority over the instance in one update + /// should not leave it registered for the other. + /// + [UnityTest] + public IEnumerator UpdateRegistrationFollowsMotionModel() + { + var instance = SpawnObject(m_MixedMotionModelPrefab, m_ServerNetworkManager).GetComponent(); + yield return WaitForSpawnedOnAllOrTimeOut(instance); + AssertOnTimeout($"Failed to spawn {instance.name} on all clients!"); + + var newOwner = m_ClientNetworkManagers[0]; + instance.ChangeOwnership(newOwner.LocalClientId); + yield return WaitForConditionOrTimeOut(() => newOwner.SpawnManager.SpawnedObjects[instance.NetworkObjectId].OwnerClientId == newOwner.LocalClientId); + AssertOnTimeout($"Client-{newOwner.LocalClientId} never gained ownership of {instance.name}!"); + + // The new owner is the authority for the rigidbody driven root, so nothing on this instance needs the fixed + // update any longer. The server authoritative child still needs the standard update. + Assert.False(newOwner.NetworkTransformFixedUpdate.ContainsKey(instance.NetworkObjectId), $"Client-{newOwner.LocalClientId} is still registered for the fixed update!"); + Assert.True(newOwner.NetworkTransformUpdate.ContainsKey(instance.NetworkObjectId), $"Client-{newOwner.LocalClientId} is not registered for the update!"); + } + } +} +#endif diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs.meta b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs.meta new file mode 100644 index 0000000000..8b1683109d --- /dev/null +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b12b5cdba06a3814fbe6cf7c4df188f7 \ No newline at end of file From 674b7d21b06e19dad058b414244bb1eace3f1963 Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Thu, 24 Sep 2026 15:36:55 -0500 Subject: [PATCH 7/8] update Applying u-pr's suggestion to this test. --- .../NetworkTransform/NetworkTransformMixedMotionModelTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs index bb4b0cf5ee..25ec9894eb 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedMotionModelTests.cs @@ -49,6 +49,10 @@ public IEnumerator UpdateRegistrationFollowsMotionModel() AssertOnTimeout($"Failed to spawn {instance.name} on all clients!"); var newOwner = m_ClientNetworkManagers[0]; + + // Establish the baseline before ownership is transferred, otherwise the check below would still pass if this instance was never registered for the fixed update to begin with. + Assert.True(newOwner.NetworkTransformFixedUpdate.ContainsKey(instance.NetworkObjectId), $"Client-{newOwner.LocalClientId} should initially be registered for the fixed update!"); + instance.ChangeOwnership(newOwner.LocalClientId); yield return WaitForConditionOrTimeOut(() => newOwner.SpawnManager.SpawnedObjects[instance.NetworkObjectId].OwnerClientId == newOwner.LocalClientId); AssertOnTimeout($"Client-{newOwner.LocalClientId} never gained ownership of {instance.name}!"); From 5791e63ac3a976a01504ef22a303754913d7a99b Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Wed, 30 Sep 2026 12:41:28 -0500 Subject: [PATCH 8/8] test: collapse the mixed authority fixtures and add GetManagersInstance The root authority mode moved from a TestFixture argument into an array walked inside the test, which takes the fixture count from 4 to 2 and the case count from 8 to 2. Only HostOrServer still needs a session per value. That required moving the nested NetworkTransform components off the player prefab and onto two prefabs spawned and despawned per case, so the owner is now an explicit client rather than every player in turn. NetcodeIntegrationTest gains GetManagersInstance for resolving a NetworkObject relative to a NetworkManager, which asserts rather than throwing KeyNotFoundException when the instance is missing. --- .../NetworkTransformMixedAuthorityTests.cs | 150 +++++++++--------- .../TestHelpers/NetcodeIntegrationTest.cs | 23 +++ 2 files changed, 100 insertions(+), 73 deletions(-) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs index ab6ef475e5..3875e4cc98 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformMixedAuthorityTests.cs @@ -8,10 +8,8 @@ namespace Unity.Netcode.RuntimeTests { - [TestFixture(HostOrServer.Host, NetworkTransform.AuthorityModes.Server)] - [TestFixture(HostOrServer.Host, NetworkTransform.AuthorityModes.Owner)] - [TestFixture(HostOrServer.Server, NetworkTransform.AuthorityModes.Server)] - [TestFixture(HostOrServer.Server, NetworkTransform.AuthorityModes.Owner)] + [TestFixture(HostOrServer.Host)] + [TestFixture(HostOrServer.Server)] internal class NetworkTransformMixedAuthorityTests : IntegrationTestWithApproximation { private const float k_MotionMagnitude = 5.5f; @@ -19,76 +17,79 @@ internal class NetworkTransformMixedAuthorityTests : IntegrationTestWithApproxim protected override int NumberOfClients => 2; - private readonly NetworkTransform.AuthorityModes m_RootAuthorityMode; - private readonly NetworkTransform.AuthorityModes m_ChildAuthorityMode; + /// + /// The root's authority mode for each case. The nested child uses the inverse. + /// + private static readonly NetworkTransform.AuthorityModes[] k_RootAuthorityModes = + { + NetworkTransform.AuthorityModes.Server, + NetworkTransform.AuthorityModes.Owner, + }; + + private GameObject[] m_MixedAuthorityPrefabs; private StringBuilder m_ErrorMsg = new StringBuilder(); - public NetworkTransformMixedAuthorityTests(HostOrServer hostOrServer, NetworkTransform.AuthorityModes rootAuthorityMode) : base(hostOrServer) + public NetworkTransformMixedAuthorityTests(HostOrServer hostOrServer) : base(hostOrServer) { - m_RootAuthorityMode = rootAuthorityMode; - m_ChildAuthorityMode = rootAuthorityMode == NetworkTransform.AuthorityModes.Server ? NetworkTransform.AuthorityModes.Owner : NetworkTransform.AuthorityModes.Server; } - protected override void OnCreatePlayerPrefab() + protected override void OnServerAndClientsCreated() { - m_PlayerPrefab.AddComponent().AuthorityMode = m_RootAuthorityMode; + m_MixedAuthorityPrefabs = new GameObject[k_RootAuthorityModes.Length]; + for (int i = 0; i < k_RootAuthorityModes.Length; i++) + { + var rootAuthorityMode = k_RootAuthorityModes[i]; + var prefab = CreateNetworkObjectPrefab($"MixedAuthority-{rootAuthorityMode}Root"); + prefab.AddComponent().AuthorityMode = rootAuthorityMode; + + var childGameObject = new GameObject(); + childGameObject.transform.parent = prefab.transform; + var childNetworkTransform = childGameObject.AddComponent(); + childNetworkTransform.AuthorityMode = InverseOf(rootAuthorityMode); + childNetworkTransform.InLocalSpace = true; - var childGameObject = new GameObject(); - childGameObject.transform.parent = m_PlayerPrefab.transform; - var childNetworkTransform = childGameObject.AddComponent(); - childNetworkTransform.AuthorityMode = m_ChildAuthorityMode; - childNetworkTransform.InLocalSpace = true; + m_MixedAuthorityPrefabs[i] = prefab; + } - base.OnCreatePlayerPrefab(); + base.OnServerAndClientsCreated(); } - /// - /// Returns the instance of 's player object that has authority over a - /// using the authority mode. - /// - private NetworkObject GetAuthorityInstance(NetworkManager player, NetworkTransform.AuthorityModes authorityMode) + private static NetworkTransform.AuthorityModes InverseOf(NetworkTransform.AuthorityModes authorityMode) { - var authority = authorityMode == NetworkTransform.AuthorityModes.Server ? m_ServerNetworkManager : player; - return authority.SpawnManager.SpawnedObjects[player.LocalClient.PlayerObject.NetworkObjectId]; + return authorityMode == NetworkTransform.AuthorityModes.Server ? NetworkTransform.AuthorityModes.Owner : NetworkTransform.AuthorityModes.Server; } - private void MovePlayers() + /// + /// Returns the instance with authority over a set to the given authority mode. + /// + private NetworkObject GetAuthorityInstance(NetworkObject instance, NetworkManager owner, NetworkTransform.AuthorityModes authorityMode) { - foreach (var networkManager in m_ClientNetworkManagers) - { - var direction = GetRandomVector3(-1.0f, 1.0f); - GetAuthorityInstance(networkManager, m_RootAuthorityMode).transform.position += direction * k_MotionMagnitude; - GetAuthorityInstance(networkManager, m_ChildAuthorityMode).transform.GetChild(0).localPosition += direction * k_MotionMagnitude; - } + return GetManagersInstance(authorityMode == NetworkTransform.AuthorityModes.Server ? m_ServerNetworkManager : owner, instance); } - private bool AllInstancePositionsMatch() + private bool AllInstancePositionsMatch(NetworkObject instance, NetworkManager owner, NetworkTransform.AuthorityModes rootAuthorityMode) { m_ErrorMsg.Clear(); - foreach (var networkManager in m_ClientNetworkManagers) + var authorityRootPosition = GetAuthorityInstance(instance, owner, rootAuthorityMode).transform.position; + var authorityChildPosition = GetAuthorityInstance(instance, owner, InverseOf(rootAuthorityMode)).transform.GetChild(0).localPosition; + + // The authority instances are compared too. An instance with authority over one nested + // NetworkTransform is still non-authority for the other. + foreach (var networkManager in m_NetworkManagers) { - var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; - var authorityRootPosition = GetAuthorityInstance(networkManager, m_RootAuthorityMode).transform.position; - var authorityChildPosition = GetAuthorityInstance(networkManager, m_ChildAuthorityMode).transform.GetChild(0).localPosition; + var clone = GetManagersInstance(networkManager, instance); + var cloneRootPosition = clone.transform.position; + var cloneChildPosition = clone.transform.GetChild(0).localPosition; - // The authority instances are compared too, as an instance with authority over one nested - // NetworkTransform is still non-authority for the other. - foreach (var client in m_NetworkManagers) + if (!Approximately(authorityRootPosition, cloneRootPosition)) { - var playerClone = client.SpawnManager.SpawnedObjects[playerObjectId]; - var cloneRootPosition = playerClone.transform.position; - var cloneChildPosition = playerClone.transform.GetChild(0).localPosition; - - if (!Approximately(authorityRootPosition, cloneRootPosition)) - { - m_ErrorMsg.AppendLine($"[Client-{client.LocalClientId}][{playerClone.name}] Root mismatch ({GetVector3Values(authorityRootPosition)})({GetVector3Values(cloneRootPosition)})!"); - } + m_ErrorMsg.AppendLine($"[{rootAuthorityMode}Root][Client-{networkManager.LocalClientId}] Root mismatch ({GetVector3Values(authorityRootPosition)})({GetVector3Values(cloneRootPosition)})!"); + } - if (!Approximately(authorityChildPosition, cloneChildPosition)) - { - m_ErrorMsg.AppendLine($"[Client-{client.LocalClientId}][{playerClone.name}] Child mismatch ({GetVector3Values(authorityChildPosition)})({GetVector3Values(cloneChildPosition)})!"); - } + if (!Approximately(authorityChildPosition, cloneChildPosition)) + { + m_ErrorMsg.AppendLine($"[{rootAuthorityMode}Root][Client-{networkManager.LocalClientId}] Child mismatch ({GetVector3Values(authorityChildPosition)})({GetVector3Values(cloneChildPosition)})!"); } } return m_ErrorMsg.Length == 0; @@ -96,42 +97,45 @@ private bool AllInstancePositionsMatch() /// /// Client-Server Only - /// Validates that mixed authority is working properly + /// Validates that mixed authority is working properly for both arrangements: /// Root -- Server or Owner authoritative /// |--Child -- The inverse of the root's authority mode /// [UnityTest] public IEnumerator MixedAuthorityTest() { - for (int i = 0; i < k_Iterations; i++) + // A client owns the instance so the owner authoritative half is never also the server. + var owner = m_ClientNetworkManagers[0]; + for (int i = 0; i < k_RootAuthorityModes.Length; i++) { - MovePlayers(); - yield return WaitForConditionOrTimeOut(AllInstancePositionsMatch); - AssertOnTimeout($"Transforms failed to synchronize!\n{m_ErrorMsg}"); - } - } + var rootAuthorityMode = k_RootAuthorityModes[i]; + var instance = SpawnObject(m_MixedAuthorityPrefabs[i], owner).GetComponent(); + yield return WaitForSpawnedOnAllOrTimeOut(instance); + AssertOnTimeout($"[{rootAuthorityMode}Root] Failed to spawn {instance.name} on all clients!"); - /// - /// The update registration is per-NetworkObject while the authority motion model is per-NetworkTransform, - /// so an instance stays registered for as long as any one of its nested NetworkTransform components is - /// non-authority. - /// - [Test] - public void MixedAuthorityUpdateRegistration() - { - foreach (var networkManager in m_ClientNetworkManagers) - { - var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; - foreach (var client in m_NetworkManagers) + // An instance stays registered for updates while any of its nested NetworkTransform components is non-authority. + foreach (var networkManager in m_NetworkManagers) { - var playerClone = client.SpawnManager.SpawnedObjects[playerObjectId]; + var clone = GetManagersInstance(networkManager, instance); var hasNonAuthority = false; - foreach (var networkTransform in playerClone.NetworkTransforms) + foreach (var networkTransform in clone.NetworkTransforms) { hasNonAuthority |= !networkTransform.CanCommitToTransform; } - Assert.AreEqual(hasNonAuthority, client.NetworkTransformUpdate.ContainsKey(playerObjectId), $"[Client-{client.LocalClientId}][{playerClone.name}] Unexpected update registration!"); + Assert.AreEqual(hasNonAuthority, networkManager.NetworkTransformUpdate.ContainsKey(instance.NetworkObjectId), $"[{rootAuthorityMode}Root][Client-{networkManager.LocalClientId}] Unexpected update registration!"); + } + + for (int iteration = 0; iteration < k_Iterations; iteration++) + { + var direction = GetRandomVector3(-1.0f, 1.0f); + GetAuthorityInstance(instance, owner, rootAuthorityMode).transform.position += direction * k_MotionMagnitude; + GetAuthorityInstance(instance, owner, InverseOf(rootAuthorityMode)).transform.GetChild(0).localPosition += direction * k_MotionMagnitude; + + yield return WaitForConditionOrTimeOut(() => AllInstancePositionsMatch(instance, owner, rootAuthorityMode)); + AssertOnTimeout($"[{rootAuthorityMode}Root] Transforms failed to synchronize!\n{m_ErrorMsg}"); } + + instance.Despawn(); } } } diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs b/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs index 5b2f5a986b..5c6505b52b 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs @@ -322,6 +322,29 @@ protected NetworkManager GetNonAuthorityNetworkManager(int nonAuthorityIndex) return null; } + /// + /// Gets the relative instance of the with the given identifier. + /// + /// The to get the relative instance from + /// The identifier of the wanted + /// The relative instance + protected NetworkObject GetManagersInstance(NetworkManager networkManager, ulong networkObjectId) + { + Assert.True(networkManager.SpawnManager.SpawnedObjects.ContainsKey(networkObjectId), $"Client-{networkManager.LocalClientId} has no spawned {nameof(NetworkObject)} with an identifier of {networkObjectId}!"); + return networkManager.SpawnManager.SpawnedObjects[networkObjectId]; + } + + /// + /// Gets the relative instance of the given . + /// + /// The to get the relative instance from + /// Any instance of the wanted + /// The relative instance + protected NetworkObject GetManagersInstance(NetworkManager networkManager, NetworkObject networkObject) + { + return GetManagersInstance(networkManager, networkObject.NetworkObjectId); + } + /// /// Contains each client relative set of player NetworkObject instances /// [Client Relative set of player instances][The player instance ClientId][The player instance's NetworkObject]