#if COM_UNITY_MODULES_PHYSICS using System.Collections; using NUnit.Framework; using Unity.Netcode.Components; using UnityEngine; using UnityEngine.TestTools; using Unity.Netcode.TestHelpers.Runtime; namespace Unity.Netcode.RuntimeTests { public class NetworkRigidbodyTest : NetcodeIntegrationTest { protected override int NumberOfClients => 1; protected override void OnCreatePlayerPrefab() { m_PlayerPrefab.AddComponent(); m_PlayerPrefab.AddComponent(); m_PlayerPrefab.AddComponent(); m_PlayerPrefab.GetComponent().interpolation = RigidbodyInterpolation.Interpolate; } /// /// Tests that a server can destroy a NetworkObject and that it gets despawned correctly. /// /// [UnityTest] public IEnumerator TestRigidbodyKinematicEnableDisable() { // This is the *SERVER VERSION* of the *CLIENT PLAYER* var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper(); yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId), m_ServerNetworkManager, serverClientPlayerResult); var serverPlayer = serverClientPlayerResult.Result.gameObject; // This is the *CLIENT VERSION* of the *CLIENT PLAYER* var clientClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper(); yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId), m_ClientNetworkManagers[0], clientClientPlayerResult); var clientPlayer = clientClientPlayerResult.Result.gameObject; Assert.IsNotNull(serverPlayer, "serverPlayer is not null"); Assert.IsNotNull(clientPlayer, "clientPlayer is not null"); yield return WaitForTicks(m_ServerNetworkManager, 3); // server rigidbody has authority and should not be kinematic Assert.True(serverPlayer.GetComponent().isKinematic == false, "serverPlayer kinematic"); Assert.AreEqual(RigidbodyInterpolation.Interpolate, serverPlayer.GetComponent().interpolation, "server equal interpolate"); // client rigidbody has no authority and should have a kinematic mode of true Assert.True(clientPlayer.GetComponent().isKinematic, "clientPlayer kinematic"); Assert.AreEqual(RigidbodyInterpolation.None, clientPlayer.GetComponent().interpolation, "client equal interpolate"); // despawn the server player (but keep it around on the server) serverPlayer.GetComponent().Despawn(false); yield return WaitForTicks(m_ServerNetworkManager, 3); // When despawned, we should always be kinematic (i.e. don't apply physics when despawned) Assert.IsTrue(serverPlayer.GetComponent().isKinematic == true, "serverPlayer second kinematic"); yield return WaitForTicks(m_ServerNetworkManager, 3); Assert.IsTrue(clientPlayer == null, "clientPlayer being null"); // safety check that object is actually despawned. } } } #endif // COM_UNITY_MODULES_PHYSICS