|
| 1 | +using System.Collections; |
| 2 | +using Unity.Netcode.Components; |
| 3 | +using Unity.Netcode.TestHelpers.Runtime; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.TestTools; |
| 6 | + |
| 7 | +namespace Unity.Netcode.RuntimeTests |
| 8 | +{ |
| 9 | + internal class NetworkTransformParentingTests : IntegrationTestWithApproximation |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// A NetworkBehaviour that moves in space. |
| 13 | + /// When spawned on the client, an RPC is sent to the server to spawn a player object for that client. |
| 14 | + /// The server parents the player object to the spawner object. This gives a moving parent object and a non-moving child object. |
| 15 | + /// The child object should always be at {0,0,0} local space, while the parent object moves around. |
| 16 | + /// This NetworkBehaviour tests that parenting to a moving object works as expected. |
| 17 | + /// </summary> |
| 18 | + internal class PlayerSpawner : NetworkBehaviour |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Prefab for the player |
| 22 | + /// </summary> |
| 23 | + public NetworkObject PlayerPrefab; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The server side NetworkObject that was spawned when the client connected. |
| 27 | + /// </summary> |
| 28 | + public NetworkObject SpawnedPlayer; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Represents the different movement states of the PlayerSpawner during the test lifecycle. |
| 32 | + /// </summary> |
| 33 | + public enum MoveState |
| 34 | + { |
| 35 | + // Initial state, PlayerSpawner will move without counting frames |
| 36 | + NotStarted, |
| 37 | + // The player object has been spawned, start counting frames |
| 38 | + PlayerSpawned, |
| 39 | + // We have moved far enough to test location |
| 40 | + ReachedPeak, |
| 41 | + } |
| 42 | + public MoveState State = MoveState.NotStarted; |
| 43 | + |
| 44 | + // A count of the number of updates since the player object was spawned. |
| 45 | + private int m_Count; |
| 46 | + |
| 47 | + // Movement offsets and targets. |
| 48 | + private const float k_PositionOffset = 5.0f; |
| 49 | + private const float k_RotationOffset = 25.0f; |
| 50 | + private readonly Vector3 m_PositionTarget = Vector3.one * k_PositionOffset * 10; |
| 51 | + private readonly Vector3 m_RotationTarget = Vector3.one * k_RotationOffset * 10; |
| 52 | + |
| 53 | + private void Update() |
| 54 | + { |
| 55 | + if (!IsServer) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + transform.position = Vector3.Lerp(transform.position, m_PositionTarget, Time.deltaTime * 2); |
| 61 | + var rotation = transform.rotation; |
| 62 | + rotation.eulerAngles = Vector3.Slerp(rotation.eulerAngles, m_RotationTarget, Time.deltaTime * 2); |
| 63 | + transform.rotation = rotation; |
| 64 | + |
| 65 | + if (State != MoveState.PlayerSpawned) |
| 66 | + { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Move self for some time after player object is spawned |
| 71 | + // This ensures the parent object is moving throughout the spawn process. |
| 72 | + m_Count++; |
| 73 | + if (m_Count > 10) |
| 74 | + { |
| 75 | + // Mark PlayerSpawner as having moved far enough to test. |
| 76 | + State = MoveState.ReachedPeak; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public override void OnNetworkSpawn() |
| 81 | + { |
| 82 | + if (IsOwner) |
| 83 | + { |
| 84 | + // Owner initialises PlayerSpawner movement on spawn |
| 85 | + transform.position = Vector3.one * k_PositionOffset; |
| 86 | + var rotation = transform.rotation; |
| 87 | + rotation.eulerAngles = Vector3.one * k_RotationOffset; |
| 88 | + transform.rotation = rotation; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + // When spawned on a client, send the RPC to spawn the player object |
| 93 | + // Using an RPC ensures the PlayerSpawner is moving for the entire spawning of the player object. |
| 94 | + RequestPlayerObjectSpawnServerRpc(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// A ServerRpc that requests the server to spawn a player object for the client that invoked this RPC. |
| 100 | + /// </summary> |
| 101 | + /// <param name="rpcParams">Parameters for the ServerRpc, including the sender's client ID.</param> |
| 102 | + [ServerRpc(RequireOwnership = false)] |
| 103 | + private void RequestPlayerObjectSpawnServerRpc(ServerRpcParams rpcParams = default) |
| 104 | + { |
| 105 | + SpawnedPlayer = Instantiate(PlayerPrefab); |
| 106 | + SpawnedPlayer.SpawnAsPlayerObject(rpcParams.Receive.SenderClientId); |
| 107 | + SpawnedPlayer.TrySetParent(NetworkObject, false); |
| 108 | + State = MoveState.PlayerSpawned; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Client Authoritative NetworkTransform |
| 113 | + internal class ClientNetworkTransform : NetworkTransform |
| 114 | + { |
| 115 | + protected override bool OnIsServerAuthoritative() |
| 116 | + { |
| 117 | + return false; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Don't start with any clients, we will manually spawn a client inside the test |
| 122 | + protected override int NumberOfClients => 0; |
| 123 | + |
| 124 | + // Parent prefab with moving PlayerSpawner which will spawn the childPrefab |
| 125 | + private GameObject m_PlayerSpawnerPrefab; |
| 126 | + |
| 127 | + // Client and server instances |
| 128 | + private PlayerSpawner m_ServerPlayerSpawner; |
| 129 | + private NetworkObject m_NewClientPlayer; |
| 130 | + |
| 131 | + protected override void OnServerAndClientsCreated() |
| 132 | + { |
| 133 | + m_PlayerSpawnerPrefab = CreateNetworkObjectPrefab("Parent"); |
| 134 | + var parentPlayerSpawner = m_PlayerSpawnerPrefab.AddComponent<PlayerSpawner>(); |
| 135 | + m_PlayerSpawnerPrefab.AddComponent<NetworkTransform>(); |
| 136 | + |
| 137 | + var playerPrefab = CreateNetworkObjectPrefab("Child"); |
| 138 | + var childNetworkTransform = playerPrefab.AddComponent<ClientNetworkTransform>(); |
| 139 | + childNetworkTransform.InLocalSpace = true; |
| 140 | + |
| 141 | + parentPlayerSpawner.PlayerPrefab = playerPrefab.GetComponent<NetworkObject>(); |
| 142 | + |
| 143 | + base.OnServerAndClientsCreated(); |
| 144 | + } |
| 145 | + |
| 146 | + private bool NewPlayerObjectSpawned() |
| 147 | + { |
| 148 | + return m_ServerPlayerSpawner.SpawnedPlayer && |
| 149 | + m_ClientNetworkManagers[0].SpawnManager.SpawnedObjects.ContainsKey(m_ServerPlayerSpawner.SpawnedPlayer.NetworkObjectId); |
| 150 | + } |
| 151 | + |
| 152 | + private bool HasServerInstanceReachedPeakPoint() |
| 153 | + { |
| 154 | + VerboseDebug($"Client Local: {m_NewClientPlayer.transform.localPosition} Server Local: {m_ServerPlayerSpawner.SpawnedPlayer.transform.localPosition}"); |
| 155 | + return m_ServerPlayerSpawner.State == PlayerSpawner.MoveState.ReachedPeak; |
| 156 | + } |
| 157 | + |
| 158 | + private bool ServerClientPositionMatches() |
| 159 | + { |
| 160 | + return Approximately(m_NewClientPlayer.transform.localPosition, m_ServerPlayerSpawner.SpawnedPlayer.transform.localPosition) && |
| 161 | + Approximately(m_NewClientPlayer.transform.position, m_ServerPlayerSpawner.SpawnedPlayer.transform.position); |
| 162 | + } |
| 163 | + |
| 164 | + [UnityTest] |
| 165 | + public IEnumerator TestParentedPlayerUsingLocalSpace() |
| 166 | + { |
| 167 | + // Spawn the PlayerSpawner object and save the instantiated component |
| 168 | + // The PlayerSpawner object will start moving. |
| 169 | + m_ServerPlayerSpawner = SpawnObject(m_PlayerSpawnerPrefab, m_ServerNetworkManager).GetComponent<PlayerSpawner>(); |
| 170 | + |
| 171 | + // Create a new client and connect to the server |
| 172 | + // The client will prompt the server to spawn a player object and parent it to the PlayerSpawner object. |
| 173 | + yield return CreateAndStartNewClient(); |
| 174 | + |
| 175 | + yield return WaitForConditionOrTimeOut(NewPlayerObjectSpawned); |
| 176 | + AssertOnTimeout($"Client did not spawn new player object!"); |
| 177 | + |
| 178 | + // Save the spawned player object |
| 179 | + m_NewClientPlayer = m_ClientNetworkManagers[0].SpawnManager.SpawnedObjects[m_ServerPlayerSpawner.SpawnedPlayer.NetworkObjectId]; |
| 180 | + |
| 181 | + // Let the parent PlayerSpawner move for several ticks to get an offset |
| 182 | + yield return WaitForConditionOrTimeOut(HasServerInstanceReachedPeakPoint); |
| 183 | + AssertOnTimeout($"Server instance never reached peak point!"); |
| 184 | + |
| 185 | + // Check that the client and server local positions match (they should both be at {0,0,0} local space) |
| 186 | + yield return WaitForConditionOrTimeOut(ServerClientPositionMatches); |
| 187 | + AssertOnTimeout($"Client local position {m_NewClientPlayer.transform.localPosition} does not match" + |
| 188 | + $" server local position {m_ServerPlayerSpawner.SpawnedPlayer.transform.localPosition}"); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments